首页 > 解决方案 > 为什么 VideoCapture 在成员函数中不起作用?

问题描述

我花了几个小时试图从具有同一类的成员函数的成员线程中读取 cv::VideoCapture 帧。创建、读取和 imshow() 的所有常用代码都在此成员函数中。

我认为问题出在线程中,但我制作了一些测试代码并在成员函数中找到了它。

该测试代码:

主.cpp:

#include "myclass.hpp"

int main(int argc, char *argv[])
{
    myclass m;
    m.run();

    return 0;
}

我的类.hpp

class myclass
{
public:
    myclass();
    virtual ~myclass();

    void run();
};

我的类.cpp

#include <opencv/cv.h>
#include <opencv2/opencv.hpp>
#include "myclass.hpp"

myclass::myclass()
{
}

myclass::~myclass()
{
}

void myclass::run()
{
    cv::VideoCapture capture(0);
    cv::Mat frame;

    while(true)
    {
        capture.read(frame);
        cv::imshow("TEST", frame);
    }
    capture.release();
}

编译正常,但不能正常工作。它显示空的“测试”窗口。

为什么 cv::VideoCapture::read(cv::Mat) 在 bember 函数中不起作用?

PS:opencv v3.4.2

标签: c++opencvpthreads

解决方案


根据有关imshow的参考资料

这个函数后面应该是 cv::waitKey 函数,它显示指定毫秒的图像。否则,它不会显示图像

只需添加waitKey()函数调用

capture.read(frame);
cv::imshow("TEST", frame);
cv::waitKey(25);

推荐阅读