Mat*_*ias 8 c++ webcam opencv video-capture raspberry-pi
我在我的Windows机器和RaspberryPi(ARM,Debian Wheezy)上使用C++应用程序中的OpenCV从Webcam捕获帧.问题是CPU使用率.我只需要每隔2秒处理一次帧 - 所以没有实时的实时视图.但是如何实现呢?你会建议哪一个?
提前致谢
#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;
}
Run Code Online (Sandbox Code Playgroud)
- 在每个循环中创建/销毁 VideoCapture:尚未测试
在 Windows 上(也可能在其他操作系统上)可能有点麻烦 - 创建 VideoCapture 后抓取的第一帧通常是黑色或灰色。第二帧应该没问题:)
其他想法:
- 修改想法 nr 2 - 睡眠后抓取 2 帧。第一帧可能是旧的,但第二帧应该是新的。它没有经过测试,通常我不确定,但很容易检查它。
- 最终在睡眠之后,您可能会在 while 循环中抓取帧(没有睡眠),直到您抓取同一帧两次(但可能很难实现,尤其是在 RasberryPi 上)。
| 归档时间: |
|
| 查看次数: |
3187 次 |
| 最近记录: |