简体   繁体   中英

Why VideoCapture doesn't work in member functions?

I spent some hours trying to read cv::VideoCapture frames from member Thread with a member function of the same class. All usual code of creating, read and imshow() was in this member function.

I thought the problem was in Thread but I make some test code and find out it in a member function.

That test code:

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

Compiles OK, but didn't work properly. It shows empty "TEST" window.

Why doesn't work cv::VideoCapture::read(cv::Mat) in bember functions?

PS: opencv v3.4.2

According to reference about imshow

This function should be followed by cv::waitKey function which displays the image for specified milliseconds. Otherwise, it won't display the image .

just add the call of waitKey() function

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM