为什么视频捕获在成员函数中不起作用?

Why VideoCapture doesn't work in member functions?

本文关键字:函数 不起作用 成员 视频 为什么      更新时间:2023-10-16

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

我以为问题出在Thread中,但我做了一些测试代码,并在成员函数中找到了它。

测试代码:

main.cpp:

#include "myclass.hpp"
int main(int argc, char *argv[])
{
myclass m;
m.run();
return 0;
}

myclass.hpp

class myclass
{
public:
myclass();
virtual ~myclass();
void run();
};

myclass.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();
}

编译正常,但工作不正常。它显示空的"TEST"(测试(窗口。

为什么不能在bember函数中使用cv::VideoCapture::read(cv::Mat(?

PS:opencv v3.4.2

根据关于imshow 的参考

此函数后面应该跟cv::waitKey函数,该函数在指定的毫秒内显示图像否则,它将不会显示图像

只需添加对waitKey()函数的调用

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