【问题标题】:Opencv how to read single frame from cameraOpencv如何从相机读取单帧
【发布时间】:2023-04-06 02:29:01
【问题描述】:

我正在尝试从相机读取单帧,但它挂起我的程序并使 imshow 窗口 没有响应,所以知道如何读取单帧以便我的视觉模块可以处理它 p>

这是我的代码:

 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);

     // }
}

【问题讨论】:

    标签: c++ qt opencv


    【解决方案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 情况

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-12
      • 1970-01-01
      • 1970-01-01
      • 2013-09-29
      • 2017-09-25
      • 2019-05-26
      • 1970-01-01
      相关资源
      最近更新 更多