我想使用OpenCV和Boost库来实现“流畅”的视频录制。 为此,我正在尝试实现代码,我在我的程序中找到了here。我对Boost还不太熟悉,而且我一直收到错误 \ bind.hpp:313:错误:无法匹配调用'(boost :: _ mfi :: dm)(cv :: Mat *& ,cv :: VideoCapture *&)' unwrapper :: unwrap(f,0)(a [base_type :: a1_],[base_type :: a2 _]);
我的代码如下:
#include "recorder.h"
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "boost/thread.hpp"
using namespace cv;
using namespace std;
using namespace boost::posix_time;
Recorder::Recorder(){
webcamRecorder.open(1);
webcamRecorder.set(CV_CAP_PROP_FRAME_WIDTH, 1920);
webcamRecorder.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);
recordingCount=0;
filename = "F:/MyVideo";
ext=".avi";
hasStarted=false;
}
void Recorder::captureFunc(Mat *matRecorder, VideoCapture *webcamRecorder){
for(;;){
//capture from webcame to Mat frame
(*webcamRecorder) >> (*matRecorder);
resize(*matRecorder,matOut,Size(1280,720),0,0,INTER_LINEAR);
}
}
void Recorder::setup(){
if (!hasStarted){
this->start();
boost::thread captureThread(&Recorder::captureFunc, &matRecorder, &webcamRecorder);
}
}
void Recorder::run(){
cout << "++++++++recorder thread called+++" << endl;
theVideoWriter.open(filename+countAsString+ext,CV_FOURCC('L','A','G','S'), 30, Size(1280,720), true);
nextFrameTimestamp = microsec_clock::local_time();
currentFrameTimestamp = nextFrameTimestamp;
td = (currentFrameTimestamp - nextFrameTimestamp);
if ( theVideoWriter.isOpened() == false ){
cout << "ERROR: Failed to write the video" << endl;
}
if (recording){
while(recording){
hasStarted=true;
while(td.total_microseconds() < 1000000/30){
//determine current elapsed time
currentFrameTimestamp = microsec_clock::local_time();
td = (currentFrameTimestamp - nextFrameTimestamp);
}
// determine time at start of write
initialLoopTimestamp = microsec_clock::local_time();
theVideoWriter << matOut; // write video file
nextFrameTimestamp = nextFrameTimestamp + microsec(1000000/30);
td = (currentFrameTimestamp - nextFrameTimestamp);
finalLoopTimestamp = microsec_clock::local_time();
td1 = (finalLoopTimestamp - initialLoopTimestamp);
delayFound = td1.total_milliseconds();
cout << delayFound << endl;
}
}
hasStarted=false;
cout << "finished recording" << endl;
theVideoWriter.release();
recordingCount++;
countAsString = static_cast<ostringstream*>( &(ostringstream() << recordingCount) )->str();
}
void Recorder::setRecording(bool x){ recording = x;}
我的实施有什么问题?原始代码片段来自here
答案 0 :(得分:2)
问题以及您的案例与您提供的链接之间的区别在于您对线程函数使用了对象方法。具体做法是:
boost::thread captureThread(&Recorder::captureFunc, &matRecorder, &webcamRecorder);
对象方法需要this
的指针。由于您在对象方法中创建线程,因此可以使用其this
指针:
boost::thread captureThread(&Recorder::captureFunc, this, &matRecorder, &webcamRecorder);
一些一般性建议:
C++11
在标准库中有它。如果可以的话,我建议你使用它。detached
- 它会继续执行,但您无法控制它。你可能想把它保存在某个地方,所以你可以稍后再join
。将线程作为实例变量:
std::thread captureThread;
。在当前函数中初始化,移动到实例变量:
std :: thread localCaptureThread(&amp; Recorder :: captureFunc,this,&amp; matRecorder,&amp; webcamRecorder); captureThread = std :: move(localCaptureThread);