I'm developing an application which requires inspection of certain industrial process using a live video feed from a single camera. I want to know if it's possible to create multiple windows
using only one camera. Also, each window
will have a different Region Of Interest
. I'm running this code in QThread
Here is the code I've been trying but it crashes as soon as I run it.
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.
}
}
}
Can anyone tell me where am I going wrong?
You can absolutely do that. Simply create multiple view and link the output of the camera (via some update-signal) to a display functionality in the window you want it to be (via some update-and-render-slot). So, conceptually:
Each view runs its own event loop then, so that should be fine. A general note: AFAIK it is considered bad practice to subclass QThread
or do similar things. I would prefer the worker approach here.
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.