C++ 使用boost和opencv进行多线程处理

C++ 使用boost和opencv进行多线程处理,c++,multithreading,opencv,boost-thread,C++,Multithreading,Opencv,Boost Thread,我已经写了一个程序来检测不同的物体,现在我正在开发一个跟踪模块来跟踪感兴趣的物体。由于检测速度不快,我希望每10-30帧通过一帧检测到的对象,并让跟踪模块使用camshift+kalman滤波来跟踪视频流中的对象,直到从检测模块接收到另一幅新图像,其中包含检测到的对象 一般来说,我对Boost和C++多线程技术都很陌生,我写了下面的代码,看看是否可以将帧从检测模块传递到跟踪模块。由于某种原因,在跟踪模块接收到两个IMAG后,一切都冻结了。你知道为什么吗?有没有更好的办法?谢谢 主线程检测模块可能

我已经写了一个程序来检测不同的物体,现在我正在开发一个跟踪模块来跟踪感兴趣的物体。由于检测速度不快,我希望每10-30帧通过一帧检测到的对象,并让跟踪模块使用camshift+kalman滤波来跟踪视频流中的对象,直到从检测模块接收到另一幅新图像,其中包含检测到的对象

一般来说,我对Boost和C++多线程技术都很陌生,我写了下面的代码,看看是否可以将帧从检测模块传递到跟踪模块。由于某种原因,在跟踪模块接收到两个IMAG后,一切都冻结了。你知道为什么吗?有没有更好的办法?谢谢

主线程检测模块可能以类似的方式工作

int main(int argc, char *argv[]){
    cout << "main started" << endl;

    Tracker tracker("Tracker");
    tracker.start(imread("/home/cosyco/Desktop/images/lambo1.jpeg", CV_LOAD_IMAGE_COLOR));

    boost::posix_time::seconds sleepTime(5);

    cout << "main starting sleep" << endl;
    boost::this_thread::sleep(sleepTime);
    cout << "main just woke up, switching image" << endl;
    tracker.resetImage(imread("/home/cosyco/Desktop/images/lambo2.jpeg", CV_LOAD_IMAGE_COLOR));
    cout << "main sleeping second time" << endl;
    boost::this_thread::sleep(sleepTime);
    cout << "main just woke up, switching image" << endl;
    tracker.resetImage(imread("/home/cosyco/Desktop/images/lambo3.jpeg", CV_LOAD_IMAGE_COLOR));
    cout << "main sleeping third time" << endl;
    boost::this_thread::sleep(sleepTime);
    cout << "main just woke up, switching image" << endl;
    tracker.resetImage(imread("/home/cosyco/Desktop/images/lambo4.jpeg", CV_LOAD_IMAGE_COLOR));
    tracker.join();



    cout << "main done" << endl;
    return 0;
}
跟踪器:

#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <opencv2/opencv.hpp>


using namespace std;
using namespace cv;

class Tracker{

private:
    string name;
    Mat trackerImage;
    boost::thread trackerThread;


public:
    Tracker (string newName){
        cout << "creating tracker : " << newName << "created" << endl;
        name = newName;
    }


    void track(Mat image){
        image.copyTo(trackerImage);
        informUpdate();
        // use this new image to generate a histogram for camshift on the next
        //few images in the video stream before a new image with the detected object is received


        for (; ; ){
            // cout << "tracking" << endl;
        }

    }

    void start(Mat image){
        cout << "in tracker's start" << endl;
        // trackerThread = boost::thread(&Tracker::track, this, trackLimit);
        trackerThread = boost::thread(&Tracker::track, this, image);
    }


    void join(){
        trackerThread.join();
    }


    void resetImage(Mat image){
        track(image);
    }


    void informUpdate(){
        cout << "\033[1;31m image updated \033[0m" << endl;
    }
};

你绝对确定这不是你的追踪区出的问题吗?@a-Jays-不过我现在在追踪区没做什么。现在它只是跟踪方法中的for循环。在这种方法中,我将对视频流中的每一帧进行camshift,直到我得到一个带有检测到的对象的新帧,以生成一个新的直方图,用于进一步的camshift-ing。for循环似乎不存在。在这种情况下,重置映像更新可能不会发生。所以,当track的一个实例已经运行时,您将再次调用它?@a-Jays-这很有意义。因此,如果我要连续接收来自实时提要的图像,并使用camshift根据直方图跟踪感兴趣的对象,比如说每10帧重新生成一个直方图,你知道我如何停止camshift过程中的任何位置,并在检测模块准备好新图像时重新校准直方图吗?类似于一个监听器,当检测模块准备好并取消我可能在的for循环时被触发。谢谢我能马上想到的一个丑陋的方法是在对象更新时设置/重置共享bool,并检查循环中该变量的状态。有更好的方法可以做到这一点,但我目前无法对此进行阐述。我可以,如果可以等的话。很抱歉