PDA

View Full Version : Qt with openCV, cvRealese ERROR



YDYD
9th July 2014, 09:16
Hi all,

I am getting some problem with my code,

I can compile and RUN, but it is very laggy.

My RAM seems not enough for my programme
If i leave the programme running for a few minutes, it will force close itself without any error msg.

and, if I quit the programme manually, there is a error messange.

Below if a part of my code and error msg.
I think the part have problem is the cvRelease part, and IplImage to QImage part.


void Dialog::createCam()
{
timer = new QTimer(this);

cam = cvCaptureFromCAM(-1);

if(cam==NULL)
qDebug()<<"error";

timer->start(0);
connect(timer,SIGNAL(timeout()),this,SLOT(getFrame ()));
connect(timer,SIGNAL(timeout()),this,SLOT(prcFrame ()));
}

void Dialog::getFrame()
{
frame = cvQueryFrame(cam);
QImage image = QImage ((const uchar*)frame->imageData,frame->width,frame->height,QImage::Format_RGB888).rgbSwapped();//rgbSwapped() make color better
ui->original->setPixmap(QPixmap::fromImage(image));
}

void Dialog::prcFrame() //threshold,not used yet
{
/*IplImage *imgHSV= cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,3);
cvCvtColor(frame,imgHSV,CV_BGR2HSV);
IplImage *imgFilter= cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,1);
//threshed , get value from slider value*/
cvCloneImage(frame);
cvCvtColor(frame,frame,CV_BGR2HSV);
IplImage *imgFilter= cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,1);
cvInRangeS(frame,cvScalar(ui->hueSlide1->value(),ui->satSlide1->value(),ui->lumSlide1->value(),0),cvScalar(ui->hueSlide2->value(),ui->satSlide2->value(),ui->lumSlide2->value(),0),imgFilter);

QImage imgThresdhed = QImage ((const uchar*)imgFilter->imageData,imgFilter->width,imgFilter->height,QImage::Format_Indexed8).rgbSwapped();
ui->filter->setPixmap(QPixmap::fromImage(imgThresdhed));
}


Dialog::~Dialog()


{

timer->stop();


cvReleaseCapture(&cam);


cvReleaseImage(&imgFilter);


delete ui;


}

ERROR Msg:

OpenCV Error: Bad argument (unrecognized or unsupported array type) in cvReleaseData, file /home/pi/OpenCV-2.4.2/modules/core/src/array.cpp, line 996



terminate called after throwing an instance of 'cv::Exception'



what(): /home/pi/OpenCV-2.4.2/modules/core/src/array.cpp:996: error: (-5) unrecognized or unsupported array type in function cvReleaseData






The program has unexpectedly finished.


/home/pi/qt/getCam/getCam exited with code 0

PLEASE ADVISE, thank you

stampede
9th July 2014, 09:33
1. You are leaking memory by calling cvCloneImage without taking care of releasing the result:


cvCloneImage(frame); // bad, leaks memory

// should be
IplImage * result = cvCloneFrame(frame);
...
cvReleaseImage(&result);


2. You are not allowed to modify the frame returned by cvQueryFrame:

cvCvtColor(frame,frame,CV_BGR2HSV); // wrong, attempt to modify frame owned by underlying capture mechanism
should be


IplImage * result = cvCloneFrame(frame);
cvCvtColor(frame,result,CV_BGR2HSV);
// work on the result from now on
cvReleaseImage(&result);

3. memory leak related to "imgFilter" image not released:

IplImage *imgFilter= cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,1);
// no cvReleaseImage(&imgFilter); after processing this frame
4. attempt to release uninitialized image in destructor:


Dialog::~Dialog()


{

timer->stop();


cvReleaseCapture(&cam);


cvReleaseImage(&imgFilter); // this is NOT the image from prcFrame method !


delete ui;


}

I assume you don't initialize it in constructor.

YDYD
9th July 2014, 09:47
Hi,
Thanks for the reply..

So for *imgFilter and *result, where should i release them?

Before, i use to do what you tell me,

i did like this:

IplImage *imgHSV= cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,3);
cvCvtColor(frame,imgHSV,CV_BGR2HSV);
IplImage *imgFilter= cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,1);

but, this show me QImage: no enough memory, return NULL image.

stampede
9th July 2014, 10:03
So for *imgFilter and *result, where should i release them?
When you don't need them anymore, probably at the end of prcFrame method.

but, this show me QImage: no enough memory, return NULL image.
That was before or after you've added the image release code ?

YDYD
9th July 2014, 10:07
Hi,

i added both release at the end and inside of prcFrame method,

A lot better, but it is still laggy, delay for about 1 sec to 2 sec ,

anyway, Thanks alot!!

Have a nice day =)

^NyAw^
9th July 2014, 10:28
Hi,

You are using a 0 time timer and so you don't let too much time to the main thread to process the events.
You have to calculate the timer resolution using the camera fps. Asign 33ms to the timer to get 30fps and let the proper time to the main thread to update the GUI,...