简体   繁体   English

使用Qt显示来自OpenCV的网络摄像头流

[英]show webcam stream from OpenCV with Qt

so i can see my webcam stream with OpenCV with imshow with this simple code 所以我可以看到我的网络摄像头流与OpenCV与imshow与这个简单的代码

int main(int, char**)
{
    VideoCapture cap(0); 
    Mat edges;
    namedWindow("webcam", 1);
    while (true)
    {
        Mat frame;
        cap >> frame; 
        imshow("webcam", frame);
        if (waitKey(30) >= 0) break;
    }
    return 0;
}

now what i want to is to show the image from OpenCV in QImage in Widget on QT Here is a conversion from cv::Mat to QImage 现在我想要的是在QT中的Widget中显示来自OpenCV的图像这是从cv :: Mat到QImage的转换

QImage Mat2QImage(cv::Mat const& src)
{
    cv::Mat temp; 
    cvtColor(src, temp, CV_BGR2RGB); 
    QImage dest((const uchar *)temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
    dest.bits(); 
    // of QImage::QImage ( const uchar * data, int width, int height, Format format )
    return dest;
}

and the little code to show an image with QImage in QT 以及在QT中用QImage显示图像的小代码

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QImage myImage;
    myImage.load("a.png");
    QLabel myLabel;
    myLabel.setPixmap(QPixmap::fromImage(myImage));
    myLabel.show();
    return a.exec();
}

i tried to combine them in this way, but no luck 我试图以这种方式组合它们,但没有运气

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    VideoCapture cap(0);

    QImage myImage;
    QLabel myLabel;
    while (true)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera

        myImage = Mat2QImage(frame);
        myLabel.setPixmap(QPixmap::fromImage(myImage));
    }


    myLabel.show();

    return a.exec();

You have to create a Window that inherits from QMainWindow with a QTimer . 您必须使用QTimer创建一个继承自QMainWindowWindow In the constructor, connect the timer to a Window method. 在构造函数中,将计时器连接到Window方法。 You will put your openCV code into this timeout method, that will be called every X millisecond: 您将openCV代码放入此超时方法,该方法将每X毫秒调用一次:

class Window : public QMainWindow
{
    Q_OBJECT
    QTimer _timer;

    private slots:
    void on_timeout()
    {
        // put your opencv code in it
    }
    public:
    Window() :
        QMainWindow(), _timer(this)
    {
        connect(&_timer, SIGNAL(timeout()), this, SLOT(on_timeout()));
        // populate your window with images, labels, etc. here
        _timer.start(10 /*call the timer every 10 ms*/);
    }
};

Then show your Window in the main : 然后在主要显示你的窗口:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Window win;
    win.show();
    return a.exec();
}

If you use Qt creator, it is simpler to develop with Qt: think about it. 如果您使用Qt创建者,使用Qt开发更简单:考虑一下。

thank you @Boiethios for your response this is the final code i put it in mainwindow.cpp 谢谢@Boiethios的回复,这是我把它放在mainwindow.cpp中的最终代码

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}
class Window : public QMainWindow
{
    Q_OBJECT
        QTimer _timer;

    private slots:
    void on_timeout()
    {
        VideoCapture cap(0);

        Mat edges;
        namedWindow("edges", 1);
        while (true)
        {
            Mat frame;
            cap >> frame;
            myImage = Mat2QImage(frame);
            myLabel.setPixmap(QPixmap::fromImage(myImage));
            myLabel.show();
        }
    }
public:
    QImage myImage;
    QLabel myLabel;
    Window() :
        QMainWindow(), _timer(this)
    {
        connect(&_timer, SIGNAL(timeout()), this, SLOT(on_timeout()));
        // populate your window with images, labels, etc. here
        _timer.start(10 /*call the timer every 10 ms*/);
    }
};

it compile and execute fine but nothing happens just a blank window 它编译并执行正常,但没有任何事情只是一个空白的窗口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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