【问题标题】:Why VideoCapture doesn't work in member functions?为什么 VideoCapture 在成员函数中不起作用?
【发布时间】:2018-07-16 04:56:10
【问题描述】:

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

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

那个测试代码:

ma​​in.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();
}

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

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

PS:opencv v3.4.2

【问题讨论】:

    标签: c++ opencv pthreads


    【解决方案1】:

    根据参考关于imshow

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

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

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

    【讨论】:

    • 我认为waitKey()属于imshow(),可以放在一个类的Thread函数中,VideoCapture::read(Mat)放在另一个类的成员函数中,不用waitKey ()。我只是在 read(Mat) 之后将 waitKey() 从 Thread 类函数移动到 reader 类函数。现在可以了。
    猜你喜欢
    • 2015-09-04
    • 1970-01-01
    • 2018-06-16
    • 2011-11-20
    • 2016-09-07
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多