WindowsマシンとRaspberryPi(ARM、Debian Wheezy)の両方で、C++アプリでOpenCVを使用してWebカメラからフレームをキャプチャしています。問題はCPU使用率です。2秒ごとのようにフレームを処理するだけでよいので、リアルタイムのライブビューはありません。しかし、それを達成する方法は?どちらをお勧めしますか?
- 各フレームを取得しますが、一部のみを処理します。これは少し役立ちます。最新のフレームを取得しますが、このオプションは CPU 使用率に大きな影響を与えません (25% 未満)
- 各フレームを取得/処理しますが、スリープします: CPU 使用率に良い影響を与えますが、取得するフレームが古い (5 ~ 10 秒)
- 各サイクルで VideoCapture を作成/破棄する: VideoCapture が正しくクリーンアップされていても、いくつかのサイクルの後、アプリケーションがクラッシュします。
- 他のアイデアはありますか?
前もって感謝します
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <vector>
#include <unistd.h>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[])
{
cv::VideoCapture cap(0); //0=default, -1=any camera, 1..99=your camera
if(!cap.isOpened())
{
cout << "No camera detected" << endl;
return 0;
}
// set resolution & frame rate (FPS)
cap.set(CV_CAP_PROP_FRAME_WIDTH, 320);
cap.set(CV_CAP_PROP_FRAME_HEIGHT,240);
cap.set(CV_CAP_PROP_FPS, 5);
int i = 0;
cv::Mat frame;
for(;;)
{
if (!cap.grab())
continue;
// Version 1: dismiss frames
i++;
if (i % 50 != 0)
continue;
if( !cap.retrieve(frame) || frame.empty() )
continue;
// ToDo: manipulate your frame (image processing)
if(cv::waitKey(255) ==27)
break; // stop on ESC key
// Version 2: sleep
//sleep(1);
}
return 0;
}