Boost/OpenCV 错误:与调用 '(boost::_mfi::d m<void(cv::Mat*, cv::VideoCapture*), Recorder>)

Boost/OpenCV error: no match for call to '(boost::_mfi::dm<void(cv::Mat*, cv::VideoCapture*), Recorder>)

本文关键字:cv void lt Recorder VideoCapture gt Mat 错误 OpenCV 调用 mfi      更新时间:2023-10-16

我想使用OpenCV和Boost库实现"平滑"的视频录制。为此,我试图实现我在我的程序中发现的代码。我还不太熟悉Boost,我一直得到错误bind.hpp:313:错误:不匹配调用'(Boost::_mfi::dm) (cv::Mat*&, cv:: videoccapture *&)'打开:打开(f, 0) ([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;}

我的实现出了什么问题?同样,原始代码片段来自这里

问题,以及您的案例与您提供的链接之间的区别在于,您为线程函数使用了对象方法。具体来说:

boost::thread captureThread(&Recorder::captureFunc, &matRecorder, &webcamRecorder);

对象方法需要一个指向this的指针。由于在对象方法中创建线程,因此可以使用它的this指针:

boost::thread captureThread(&Recorder::captureFunc, this, &matRecorder, &webcamRecorder);

一般性建议:

    你不再需要线程的boost了。C++11在其标准库中有它。如果可以的话,我建议你使用它。你创建的线程变成了detached——它继续执行,但是你不能控制它。你可能想把它保存在某个地方,这样你可以以后join它。

将线程作为实例变量:

  1. 在类定义中声明线程:std::thread captureThread; .
  2. 在当前函数中初始化,并且移动到实例变量

    std::thread localCaptureThread(&Recorder::captureFunc, this, &matRecorder, &webcamRecorder);captureThread = std::move(localCaptureThread);