Dear forum,
I am using this method to change from IplImage captured by OpenCV library to a QImage to use QPainter class on and paint on it another QImage, now the problem is that I am using the method in a while loop which eats up my memory (over 1GB in mere seconds) and I can't find a way around it.
QImage MainWindow
::IplImage2QImage(IplImage
*iplImg
) {
this->h = iplImg->height;
this->w = iplImg->width;
this->channels = iplImg->nChannels;
this->data = iplImg->imageData;
for (int y = 0; y < h; y++, data += iplImg->widthStep)
{
for (int x = 0; x < w; x++)
{
char r, g, b, a = 0;
if (channels == 1)
{
r = data[x * channels];
g = data[x * channels];
b = data[x * channels];
}
else if (channels == 3 || channels == 4)
{
r = data[x * channels + 2];
g = data[x * channels + 1];
b = data[x * channels];
}
if (channels == 4)
{
a = data[x * channels + 3];
qimg->setPixel(x, y, qRgba(r, g, b, a));
}
else
{
qimg->setPixel(x, y, qRgb(r, g, b));
}
}
}
return *qimg;
}
QImage MainWindow::IplImage2QImage(IplImage *iplImg)
{
this->h = iplImg->height;
this->w = iplImg->width;
this->channels = iplImg->nChannels;
this->qimg = new QImage(w, h, QImage::Format_ARGB32);
this->data = iplImg->imageData;
for (int y = 0; y < h; y++, data += iplImg->widthStep)
{
for (int x = 0; x < w; x++)
{
char r, g, b, a = 0;
if (channels == 1)
{
r = data[x * channels];
g = data[x * channels];
b = data[x * channels];
}
else if (channels == 3 || channels == 4)
{
r = data[x * channels + 2];
g = data[x * channels + 1];
b = data[x * channels];
}
if (channels == 4)
{
a = data[x * channels + 3];
qimg->setPixel(x, y, qRgba(r, g, b, a));
}
else
{
qimg->setPixel(x, y, qRgb(r, g, b));
}
}
}
return *qimg;
}
To copy to clipboard, switch view to plain text mode
now that's the method itself and the while loop for looping on frames is the following
while(i<numFrames){
if(i<(SelectedFrame+numofFrames)){
cvGrabFrame(capture);
img2=cvRetrieveFrame(capture);
qimage=this->IplImage2QImage(img2);
p.drawImage(xcoordinate, ycoordinate, input , 0,0,-1,-1,Qt::AutoColor);
img3=this->qtToCv(&(qimage));
cvWriteFrame(writer,img3);
i++;
img2=0;
img3=0;
p.end();
}
else {
cvGrabFrame(capture);
img2=cvRetrieveFrame(capture);
cvWriteFrame(writer,img2);
i++;
img2=0;
}
ui->progressBar_2->setValue(i);
}
while(i<numFrames){
if(i<(SelectedFrame+numofFrames)){
cvGrabFrame(capture);
img2=cvRetrieveFrame(capture);
qimage=this->IplImage2QImage(img2);
QPainter p(&qimage);
p.drawImage(xcoordinate, ycoordinate, input , 0,0,-1,-1,Qt::AutoColor);
img3=this->qtToCv(&(qimage));
cvWriteFrame(writer,img3);
i++;
img2=0;
img3=0;
p.end();
qimage=QImage();
}
else {
cvGrabFrame(capture);
img2=cvRetrieveFrame(capture);
cvWriteFrame(writer,img2);
i++;
img2=0;
}
ui->progressBar_2->setValue(i);
}
To copy to clipboard, switch view to plain text mode
now I know that the problem is with the method because I commented everything else and only the calling of the method caused the memory overload. All I need is for another method (or a change in this method) to ensure that not too much memory is used while applying it.
Thanks a lot and best regards,
p3l
Bookmarks