views:

1044

answers:

3

Hello everyone!

I have a problem with Qt.

Here is a part of code that troubles me:

    void FullScreenImage::QImageIplImageCvt(IplImage *input)
{
    help=cvCreateImage(cvGetSize(input), input->depth, input->nChannels);
    cvCvtColor(input, help, CV_BGR2RGB);
    QImage tmp((uchar *)help->imageData, help->width, help->height, help->widthStep, QImage::Format_RGB888);
    this->setPixmap(QPixmap::fromImage(tmp).scaled(this->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
    cvReleaseImage(&help);
}


void FullScreenImage::hideOnScreen() {
    this->hide();
    this->clear();
}

void FullScreenImage::showOnScreen(IplImage *slika, int delay) {
    QImageIplImageCvt(slika);
    this->showFullScreen();
    if(delay>0)
 QTimer::singleShot(delay*1000, this, SLOT(hideOnScreen()));
}

So, the method showOnScreen uses private method QImageIplImageCvt to create QImage from IplImage (which is used by the openCV), which is then used to create QPixmap in order to show the image in full screen. FullScreenImage class inherits QLabel.

After some delay, the fullscreen picture should be hidden, so I use QTimer to trigger an event after some delay. The event handler is the hideOnScreen method which hides the label and should clear the memory.

The problem is the following:

Whenever I call QPixmap::fromImage, it allocates the memory for the pixmap data and copies the data from QImage memory buffer to the QPixmap memory buffer. After the label is hidden, the QPixmap data still remains allocated, and even worse, after the new QPixmap::fromImage call the new chunk of memory is allocated for the new picture, and the old data is not freed from memory. This causes a memory leak (cca 10 MB per method call with my testing pictures). How can I solve that leak?

I've even tried to create a private QPixmap variable, store pixmap created by the QPixmap::fromImage to it, and then tried to call its destructor in hideOnScreen method, but it didn't help.

Is there a non-static way to create QPixmap from QImage? Or even better, is there a way to create QPixmap directly from IplImage* ?

Thank you in advance for your answers.

+1  A: 

You should be able to use QPixmap::loadFromData to create a QPixmap directly. So something like the following might work:

QPixmap p;
p.loadFromData((uchar *)help->imageData, help->width * help->height);

Without knowing anything about OpenCV or IplImage, I can't guarantee that the above snippet is correct, but hopefully it starts you down the right path.

Matt Rogers
Thank you. I'll try this approach.
If this worked for you, could you mark the answer as accepted? Thanks
Matt Rogers
Does not work, as loadFromData is not designed for raw image data.
ypnos
A: 

There is no way to create QPixmap directly from IplImage* (and also not from cv::Mat). However, your solution there is to refrain from writing everything into one line and work with a pointer that you free yourself.

e.g.:

if (this->mypixmap)
    delete this->mypixmap;
this->mypixmap = new QPixmap(QPixmap::fromImage(tmp).scaled(this->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
this->setPixmap(this->mypixmap);

Don't forget to set mypixmap(NULL) in the constructor.

ypnos
A: 

Hi all,

    I have problems similar to the above comments which I have codes in the following...


QImage image(filePath);
QPixmap pixmap = QPixmap::fromImage(image);
imageLabel->setPixmap(pixmap.scaled(pixmap.size(),Qt::KeepAspectRatio)); 


   The above codes work for small jpg files but not the ones bigger than 2mb or above, is there anyone know the problem of it?

Thanks

Kirk