c++ - 主函数跳过线程并且不加入以显示所需的输出

标签 c++ multithreading opencv

ProcessFrames 函数获取图像的一个象限,并对其应用 Canny 过滤器。但是创建的线程不会给出 Canny 检测到的图像。相反,我在 imshow() 中获取图像的颜色象限。

void ProcessFrames(Mat &image)
{
    Mat test = image;
    Canny(test, image, 5, 60, 3);
}

void main()
{
    Mat frame;
    Mat top_left, top_right, bot_left, bot_right;
    String filename = "C:\\Users\\operator\\Downloads\\MODFAM1080I.mpg";
    VideoCapture capture(filename);
    if (!capture.isOpened())
        throw "Error when reading video file!!";
    while (1)
    {
        capture >> frame;
        if (frame.empty())
        break;
        top_left = frame(Range(0, frame.rows / 2 - 1), Range(0, frame.cols / 2 - 1));
        top_right = frame(Range(0, frame.rows / 2 - 1), Range(frame.cols / 2, frame.cols - 1));
        bot_left = frame(Range(frame.rows / 2, frame.rows - 1), Range(0, frame.cols / 2 - 1));
        bot_right = frame(Range(frame.rows / 2, frame.rows - 1), Range(frame.cols / 2, frame.cols - 1));
        //Cropping the image into four quadrants to process it separately. 
        thread t1(ProcessFrames,top_left); //invoking a thread and passing first quadrant.
        t1.join(); //joing the thread with the main function
        imshow("Canny", top_left); // still shows color image of the first quadrant. Canny not reflected.
        cvWaitKey(10);
    }
}

最佳答案

std::thread 构造函数复制所有内容。 如果你想传递数据指针并且这个数据要在线程中改变,你必须用 std::ref() 包装它。

关于c++ - 主函数跳过线程并且不加入以显示所需的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44634117/

相关文章:

c++ - 返回对此的右值引用的正确方法

java - 0 超时的 future.get 行为

java - volatile 对象工作

c++ - QT OpenCV setMouseCallback "argument type does not match"

c++ - ndk 使用 opencv 构建

c++ - 多态性和指针数组 (C++)

c++ - 检索从中实例化类型的模板

c++ - 将 ipython qt-console 作为 qwidget 嵌入到 qt/c++ 应用程序中

android - 使用 Timer 从 android 做 http 请求

c++ - 如何将 OpenCV Mat 的所有像素设置为特定值?