我有一个问题,我试图在单独的线程上显示两个图像,其中两个图像都来自同一帧,但其中一个是原始帧的副本,另一个是原始的。程序将能够完美地显示原始帧,但是当具有克隆/复制图像的线程到达imshow()时,它崩溃并显示错误:"断言失败(大小。宽度> 0,,
有人能帮忙吗?问候。
这是接收相机输入的类:
CameraInput::CameraInput()
{
// Initialize capturing live feed from the camera
capture.open(0);
capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
// Couldn't get a device? Throw an error and quit
if(!capture.isOpened())
{
qDebug() << "Capture could not be opened successfully.";
}
qDebug() << "Capure =" << QThread::currentThreadId();
}
void CameraInput::captureImage()
{
capture >> frame;
if(!frame.empty())
{
emit capturedImage(&frame);
}
//qDebug() << "capture" << QThread::currentThreadId();
}
这是当到达"emit capturedImage(&Frame);"时运行的函数。
void Controll::inputImage(cv::Mat* imgIn)
{
cv::imshow("video", *imgIn);
if (processReady)
{
processReady = false;
emit image(imgIn);
}
emit requestImage();
// see how much time has elapsed
time(&end);
// calculate current FPS
++counterOrg;
sec = difftime (end, start);
fpsOrg = counterOrg / sec;
// will print out Inf until sec is greater than 0
printf("FPS Org stream = %.2fn", fpsOrg);
}
这是当到达"emit image(imgIn);"时调用的函数。
void Process::processImage(cv::Mat* img)
{
cv::Mat imgHSV = img->clone();
emit (processedImage(&imgHSV));
emit (readyForWork());
}
在下面的代码中,我们遇到了一个错误,调试行打印出来,但是在imshow()上我们得到了提到的错误。我们还尝试使用复制构造函数和copyTo(),它们都产生相同的结果。
void Controll::processedImage(cv::Mat* imgIn)
{
qDebug() << "About to show thresh image...";
cv::imshow("thresh", *imgIn);
// see how much time has elapsed
time(&end);
// calculate current FPS
++counterProcessed;
sec = difftime (end, start);
fpsProcessed = counterProcessed / sec;
// will print out Inf until sec is greater than 0
printf("FPS processed stream = %.2fn", fpsProcessed);
}
int main()
{
int c;
// Objects
CameraInput *camera = new CameraInput();
Controll *troller = new Controll();
Process *processer = new Process();
Tracking *tracker = new Tracking();
Serial_Communication *serial = new Serial_Communication("/dev/ttyUSB0");
// Threads
QThread *t1 = new QThread;
QThread *t2 = new QThread;
QThread *t3 = new QThread;
camera->moveToThread(t1);
processer->moveToThread(t2);
tracker->moveToThread(t3);
serial->moveToThread(t3);
// Connections
QObject::connect(t1, SIGNAL(started()), camera, SLOT(captureImage()));
QObject::connect(camera, SIGNAL(capturedImage(cv::Mat*)), troller, SLOT(inputImage(cv::Mat*)));
QObject::connect(t2, SIGNAL(started()), troller, SLOT(processerReady()));
QObject::connect(troller, SIGNAL(image(cv::Mat*)), processer, SLOT(processImage(cv::Mat*)));
QObject::connect(troller, SIGNAL(requestImage()), camera, SLOT(captureImage()));
QObject::connect(processer, SIGNAL(posXposY(int,int)), tracker, SLOT(position(int,int)));
QObject::connect(tracker, SIGNAL(directionAndSpeed(int,int)), serial, SLOT(sendData(int,int)));
QObject::connect(processer, SIGNAL(readyForWork()), troller, SLOT(processerReady()));
QObject::connect(processer, SIGNAL(processedImage(cv::Mat*)), troller, SLOT(processedImage(cv::Mat*)));
// Need to add finish/clean up stuff for terminating threads.
// Starting Threads
t1->start();
t2->start();
t3->start();
// wait for key to exit
while (true) {
c = cvWaitKey(1);
if(c!=-1)
{
// Add thread termination before breaking the loop.
// If pressed, break out of the loop
break;
}
}
return 0;
}
通过将指针和引用一起移开来解决这个问题。我们尝试按照建议使用引用,但后来我们最终得到了错误:"QObject::connect:不能队列类型的参数'cv::Mat&'(确保'cv::Mat&'是使用qRegisterMetaType()注册的。")。我们无法注册cv::Mat&,但cv::Mat工作正常