【问题标题】:How to enable Multiple Windows in Qt using OpenCV?如何使用 OpenCV 在 Qt 中启用多个窗口?
【发布时间】:2019-04-03 06:48:49
【问题描述】:

我正在开发一个应用程序,该应用程序需要使用来自单个摄像机的实时视频源来检查某些工业过程。我想知道是否可以仅使用一台相机创建multiple windows。此外,每个window 将有一个不同的Region Of Interest。 我在 QThread
中运行此代码 这是我一直在尝试的代码,但我一运行它就崩溃了。

mythread.cpp

#include "mythread.h"
#include "mainwindow.h"
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
using namespace std;

char key;

MyThread::MyThread(QObject *parent, bool b) : QThread(parent), Stop(b)
{
}

// run() will be called when a thread starts
void MyThread::run()
{
    cvNamedWindow("Camera_Output", 1);    //Create window
    cvNamedWindow("Camera_Output1", 1);

        CvCapture* capture = cvCaptureFromCAM(0);  //Capture using camera 0 connected to system
        cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, 640 );
        cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, 480 );

        CvCapture* capture1 = cvCaptureFromCAM(0);  //Capture using camera 0 connected to system
        cvSetCaptureProperty( capture1, CV_CAP_PROP_FRAME_WIDTH, 640 );
        cvSetCaptureProperty( capture1, CV_CAP_PROP_FRAME_HEIGHT, 480 );

     //Create loop for live streaming
     while(1){  

            IplImage* framein = cvQueryFrame(capture); //Create image frames from capture
            IplImage* framein1 = cvQueryFrame(capture1);

            /* sets the Region of Interest  - rectangle area has to be __INSIDE__ the image */
            cvSetImageROI(framein, cvRect(200, 200, 320, 240));
            cvSetImageROI(framein1, cvRect(500, 500, 320, 240));


            /* create destination image  - cvGetSize will return the width and the height of ROI */
            IplImage *frameout = cvCreateImage(cvGetSize(framein),  framein->depth, framein->nChannels);
            IplImage *frameout1 = cvCreateImage(cvGetSize(framein1),  framein1->depth, framein1->nChannels);

            /* copy subimage */
            cvCopy(framein, frameout, NULL);
            cvCopy(framein1, frameout1, NULL);

            /* always reset the Region of Interest */
            cvResetImageROI(framein);
            cvResetImageROI(framein1);

            cvShowImage("Camera_Output", frameout);   //Show image frames on created window
            cvShowImage("Camera_Output1", frameout1);

            key = cvWaitKey(10);     //Capture Keyboard stroke
            if (char(key) == 27){
                break;      //ESC key loop will break.
            }
        }
   }

谁能告诉我哪里出错了?

【问题讨论】:

    标签: c++ qt opencv ubuntu-16.04


    【解决方案1】:

    你绝对可以做到。 只需创建多个视图并将相机的输出(通过一些更新信号)链接到您希望它成为的窗口中的显示功能(通过一些更新和渲染槽)。 所以,从概念上讲:

    • 创建多个视图
    • 在每个视图中设置感兴趣区域
    • 将相机输出连接到每个视图
    • 如果有新图像到达,通知视图。每个视图都会提取感兴趣区域并进行渲染。

    然后每个视图都运行自己的事件循环,所以应该没问题。 一般说明:AFAIK 将QThread 子类化或做类似事情被认为是不好的做法。我更喜欢这里的worker approach

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多