Results 1 to 2 of 2

Thread: IplImage to QImage memory issue

  1. #1
    Join Date
    May 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default IplImage to QImage memory issue

    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.
    Qt Code:
    1. QImage MainWindow::IplImage2QImage(IplImage *iplImg)
    2. {
    3. this->h = iplImg->height;
    4. this->w = iplImg->width;
    5. this->channels = iplImg->nChannels;
    6. this->qimg = new QImage(w, h, QImage::Format_ARGB32);
    7. this->data = iplImg->imageData;
    8.  
    9. for (int y = 0; y < h; y++, data += iplImg->widthStep)
    10. {
    11. for (int x = 0; x < w; x++)
    12. {
    13. char r, g, b, a = 0;
    14. if (channels == 1)
    15. {
    16. r = data[x * channels];
    17. g = data[x * channels];
    18. b = data[x * channels];
    19. }
    20. else if (channels == 3 || channels == 4)
    21. {
    22. r = data[x * channels + 2];
    23. g = data[x * channels + 1];
    24. b = data[x * channels];
    25. }
    26.  
    27. if (channels == 4)
    28. {
    29. a = data[x * channels + 3];
    30. qimg->setPixel(x, y, qRgba(r, g, b, a));
    31. }
    32. else
    33. {
    34. qimg->setPixel(x, y, qRgb(r, g, b));
    35. }
    36. }
    37. }
    38. return *qimg;
    39. }
    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
    Qt Code:
    1. while(i<numFrames){
    2. if(i<(SelectedFrame+numofFrames)){
    3. cvGrabFrame(capture);
    4. img2=cvRetrieveFrame(capture);
    5. qimage=this->IplImage2QImage(img2);
    6. QPainter p(&qimage);
    7. p.drawImage(xcoordinate, ycoordinate, input , 0,0,-1,-1,Qt::AutoColor);
    8. img3=this->qtToCv(&(qimage));
    9. cvWriteFrame(writer,img3);
    10. i++;
    11. img2=0;
    12. img3=0;
    13. p.end();
    14. qimage=QImage();
    15. }
    16. else {
    17. cvGrabFrame(capture);
    18. img2=cvRetrieveFrame(capture);
    19. cvWriteFrame(writer,img2);
    20. i++;
    21. img2=0;
    22. }
    23. ui->progressBar_2->setValue(i);
    24. }
    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

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: IplImage to QImage memory issue

    The reason for the memory consumption is that you are only allocating memory, and never free it.
    There is no need to allocate a new QImage for each iteration.
    Allocate the QImage once, and reuse it.
    Qt Code:
    1. QImage MainWindow::IplImage2QImage(IplImage *iplImg)
    2. {
    3. this->h = iplImg->height;
    4. this->w = iplImg->width;
    5. this->channels = iplImg->nChannels;
    6. if(this->qimg == NULL); //don't forget to set qimg = NULL in the constructor, and delete it in the destructor (or give it a parent)
    7. this->qimg = new QImage(w, h, QImage::Format_ARGB32);
    8. this->data = iplImg->imageData;
    9. .....
    10. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. QPainter/QImage/memory management error
    By p3l-outrepid in forum Qt Programming
    Replies: 0
    Last Post: 10th May 2010, 12:53
  2. qimage to iplimage
    By jack1989 in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2010, 15:32
  3. QImage destruction is not releasing memory.
    By node_ex in forum Qt Programming
    Replies: 1
    Last Post: 15th November 2008, 08:52
  4. reading writing qimage / qbitmap to and from memory
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2008, 11:28
  5. QImage Issue
    By vishal.chauhan in forum Qt Programming
    Replies: 5
    Last Post: 5th February 2007, 04:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.