并行编程打开cv c ++ namedWindow imshow问题

时间:2017-06-23 14:43:14

标签: c++ multithreading opencv

问题相对简单;我希望辅助线程控制相机,而主程序计算图像上的特征提取相关操作,然后当它完成时重新控制相机。问题在于,当辅助线程关闭/结束时,它还会关闭显示视频的窗口。如果我尝试重新打开一个名称相同的窗口没有任何反应,则不会显示任何内容! (我昨天开始编程)

#include <iostream>
#include <thread>
#include <opencv2\opencv.hpp>
#include <mutex>

std::mutex mu;
cv::VideoCapture videoCamera;

void shared_imshow(cv::Mat imagen, int id) {
    mu.lock();
    std::cout <<"id: "<<id << std::endl;
    cv::imshow("Shared Img",imagen);
    mu.unlock();
}

void playCam() {
    cv::VideoCapture videoCamera;
    cv::Mat imagen;
    videoCamera.open(0);
    int neko = 0;
    for (int i=0;i<150;++i)
    {
        videoCamera.read(imagen);
        //if (!imagen.data) { videoCamera.set(cv::CAP_PROP_POS_FRAMES, 0); videoCamera.read(imagen); }
        shared_imshow(imagen,0);
        if (cv::waitKey(30) == 99) break;
    }
    std::cout << "Break" << std::endl;
}

int main() {
    videoCamera.open(0);

    const clock_t begin_time2 = clock();
    std::thread t1(playCam);              // create secondary thread
    //   Feature extraction, homography etc.
    t1.join();                            // whait for the thread
    videoCamera.open(0);           // Reopen the camera (sice it is closed when the secondary thread ends)
    cv::namedWindow("Shared Img"); // Try to reopen a the closed window
    cv::Mat imagen2;
    for (;;)
    {
        videoCamera.read(imagen2);
        shared_imshow(imagen2,1);
        if (cv::waitKey(30) == 118) break;
    }

    cv::destroyAllWindows();
    system("pause");
}

0 个答案:

没有答案
相关问题