在线程之间解析 OpenCV 帧

parse OpenCV frame between threads

嘿,我在 Windows 上有一个简单的线程:

while (!MainThreadHasFinished) {
    cv::Mat frame = (cv::Mat)lpParameter;       //Casting try.
    cv::imshow("Camera image", (frame));        //Show Img in window
    printf("img printed");                      //print text to cmd

    if (cv::waitKey(1) == 27)                   // exit this loop when ESC was pressed
        break;
    }
    return 0;
}

问题是我做不到 cv::imshow,这似乎不是转换为 cv::Mat :)

我将线程创建为:

DWORD thread_ID;
HANDLE handle_NumberCruncher = CreateThread( 
    NULL,                   // default security attributes
    0,                      // use default stack size  
    NumberCruncher,         // thread function name
    &frame,                 // argument to thread function 
    0,                      // use default creation flags 
    &thread_ID);            // returns the thread identifier 

我在 MSDN 和 Stack 上看到了多个关于投射的参考,但没有关于图像的参考。

如果这不可能,那么一般的问题就是如何在线程之间解析数据。想法是可以在主线程中完成对图像的一些处理,然后将其解析为每个图像都不需要的一些更高级的东西。不使用全局变量。由于线程可能在不同的处理器上,举个例子:)

lpParameter 是一个 void* 指针。您必须首先将其转换为 cv::Mat 指针类型,即:

DWORD WINAPI NumberCruncher( LPVOID lpParameter )
{
   while (!MainThreadHasFinished) {
      cv::Mat* frame = (cv::Mat*)lpParameter;     //Casting try.
      printf("img printed");                      //print text to cmd

      // Display image in the main thread
      // ...

      Sleep(1);
   }
   return 0;
}

DWORD thread_ID;
HANDLE handle_NumberCruncher = CreateThread( 
    NULL,                   // default security attributes
    0,                      // use default stack size  
    NumberCruncher,         // thread function name
    &frame,                 // argument to thread function 
    0,                      // use default creation flags 
    &thread_ID);            // returns the thread identifier 

或者,如果线程不需要访问原始变量:

cv::Mat value = *(cv::Mat*)lpParameter;


p.s.: GUI 系统通常让您在屏幕上显示内容并在主线程上获取用户输入。尝试在主线程中使用 imshow & waitkey