Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么VideoCapture没有';你不能在成员函数中工作吗?_C++_Opencv_Pthreads - Fatal编程技术网

C++ 为什么VideoCapture没有';你不能在成员函数中工作吗?

C++ 为什么VideoCapture没有';你不能在成员函数中工作吗?,c++,opencv,pthreads,C++,Opencv,Pthreads,我花了几个小时试图从具有同一类的成员函数的成员线程中读取cv::VideoCapture帧。创建、读取和imshow()的所有常用代码都在这个成员函数中 我以为问题出在线程中,但我编写了一些测试代码,并在成员函数中找到了它 该测试代码: main.cpp: #include "myclass.hpp" int main(int argc, char *argv[]) { myclass m; m.run(); return 0; } myclass.hpp clas

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

我以为问题出在线程中,但我编写了一些测试代码,并在成员函数中找到了它

该测试代码:

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

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

    void run();
};
#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();
}
#包括
#包括
#包括“myclass.hpp”
myclass::myclass()
{
}
myclass::~myclass()
{
}
void myclass::run()
{
cv::视频捕获(0);
cv::垫架;
while(true)
{
捕获。读取(帧);
cv::imshow(“测试”,框架);
}
捕获。释放();
}
编译正常,但工作不正常。它显示空的“测试”窗口

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


PS:opencv v3.4.2

根据有关

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

只需添加调用
waitKey()
函数即可

capture.read(frame);
cv::imshow("TEST", frame);
cv::waitKey(25);
我认为waitKey()属于imshow(),可以将它放在一个类的线程函数中,也可以将VideoCapture::read(Mat)放在没有waitKey()的另一个类的成员函数中。我只是在读取(Mat)之后,将waitKey()从线程类函数移到读取器类函数。现在它可以工作了。