OpenCV如何从相机读取单帧

时间:2020-01-24 11:10:39

标签: c++ qt opencv

我正在尝试从相机读取单帧,但是它挂起了我的程序,并使 imshow窗口没有响应,因此,任何想法如何读取单帧以便我的视觉模块都可以处理

这是我的代码:

 cv::VideoCapture VideoCapture;
 // Frame read from Camera will be stored in CamFrame MAT
 cv::Mat CamFrame;

// Open camera
VideoCapture.open(0);

if(!VideoCapture.isOpened())  // Check if we succeeded
{
    with for loop it not hang the imshow model  
  //   for (;;){

    // Read frame from Camera
    VideoCapture.read(this->CamFrame);
    // check if we succeeded
    if (this->CamFrame.empty()) {
        qDebug() <<  "ERROR! blank frame grabbed\n";
        break;
    }
    cv::imshow("live frame ",this->CamFrame);

     // }
}

1 个答案:

答案 0 :(得分:1)

您应该在imshow()之后使用waitKey()。如文档所述:

注意:此函数是HighGUI中唯一可以获取和 处理事件,因此需要定期为正常事件调用 除非在需要以下操作的环境中使用HighGUI,否则将进行处理 关心事件处理。

cv::VideoCapture VideoCapture;
 // Frame read from Camera will be stored in CamFrame MAT
 cv::Mat CamFrame;

// Open camera
VideoCapture.open(0);

if(!VideoCapture.isOpened())  // Check if we succeeded
{
    with for loop it not hang the imshow model  
  //   for (;;){

    // Read frame from Camera
    VideoCapture.read(this->CamFrame);
    // check if we succeeded
    if (this->CamFrame.empty()) {
        qDebug() <<  "ERROR! blank frame grabbed\n";
        break;
    }
    cv::imshow("live frame ",this->CamFrame);
    waitKey(0);

     // }
}

注意:如果要将imshow用于连续帧,则可以使用特定的waitKey()或while()来分配if大小写