复制图像后,imshow中的断言失败(size.width> 0& size.height> 0)

时间:2014-05-07 09:49:41

标签: c++ qt opencv

我有一个问题,我试图在不同的线程上显示两个图像,其中两个图像都来自同一帧,但其中一个是原始帧的副本,另一个是原始帧。该程序将能够完美地显示原始帧,但是当具有克隆/复制图像的线程达到imshow()时,它会崩溃显示错误:“断言失败(size.width> 0&& size.height> 0 )在imshow“。

有没有人可以提供帮助? 问候。

这是摄像机输入的课程:

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(&amp; 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 = %.2f\n", 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 = %.2f\n", fpsProcessed);
}

这是main.cpp:

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;
}

1 个答案:

答案 0 :(得分:1)

通过完全脱离指针和引用解决了这个问题。我们尝试使用建议的引用,但后来我们得到了错误:&#34; QObject :: connect:无法对类型&#39; cv :: Mat&amp;&#39;的参数进行排队。 (确保使用qRegisterMetaType()注册&#39; cv :: Mat&amp;&#39;)&#34;。我们无法注册cv :: Mat&amp;,但cv :: Mat工作正常。