Results 1 to 6 of 6

Thread: efficient way to display opencv image into Qt

  1. #1

    Default efficient way to display opencv image into Qt

    Hi all,

    I had develop an application to record and play rtp stream or local file using opencv.

    My opencv process run into seperate thread and I use opencv function to display the video :

    Qt Code:
    1. void opencv_thread::play_video(VideoCapture capture)
    2. {
    3. emit write_LOG(0,QTime::currentTime(),"playing " );
    4. stop_video_flag = false;
    5.  
    6. frame_index = 0;
    7. #ifdef DISPAY_by_OPENCV
    8. namedWindow("Player",CV_WINDOW_AUTOSIZE);
    9. #endif
    10.  
    11. /* Loop until frame ended or ESC is pressed */
    12. while(1)
    13. {
    14. /* grab frame image, and retrieve */
    15. Mat frame;
    16. capture >> frame; // get a new frame from camera
    17.  
    18. if (frame.empty())
    19. break;
    20.  
    21. Mat frame_flipped;
    22.  
    23. if (image_flip_is_Activate == true)
    24. {
    25. cv::flip(frame,frame_flipped,1);
    26. frame= frame_flipped;
    27. }
    28. #ifdef DISPAY_by_OPENCV
    29. imshow("Player", frame);
    30. #endif
    31. emit Qimage_available(MatToQImage(frame));
    32. #endif
    33.  
    34. Mat src_gray;
    35. cvtColor( frame, src_gray, CV_BGR2GRAY );
    36.  
    37. if (flag_gray == true)
    38. {
    39. display_gray(src_gray);
    40. }
    41. if (flag_histogram == true)
    42. {
    43. histogram(frame);
    44. }
    45. if (flag_edge == true)
    46. {
    47. edge(frame,src_gray);
    48. }
    49. if (flag_face_detect == true)
    50. {
    51.  
    52. face_detect(frame,src_gray);
    53. }
    54.  
    55. /* if ESC is pressed then exit loop */
    56. char c = cvWaitKey(33);
    57. if(c==27)
    58. break;
    59. if ((stop_video_flag == true) || (flag_pause == true))
    60. break;
    61.  
    62. capture_current_frame = capture.get(CV_CAP_PROP_POS_FRAMES);
    63.  
    64. int capture_number_of_frame = capture.get(CV_CAP_PROP_FRAME_COUNT);
    65. emit update_current_frame(capture_current_frame,capture_number_of_frame);
    66.  
    67. frame_index ++ ;
    68. }
    69.  
    70. /* destroy pointer to video */
    71. capture.release();
    72.  
    73. }
    To copy to clipboard, switch view to plain text mode 

    and everything work fine (not to much CPU load)............................................. except that I display video into a opencv and I would like to display it into Qt widget.

    So I try to convert Mat image from opencv to Qimage and emit a signal every time an image is available:

    Qt Code:
    1. QImage opencv_thread::MatToQImage(const Mat& mat)
    2. {
    3. // 8-bits unsigned, NO. OF CHANNELS=1
    4. if(mat.type()==CV_8UC1)
    5. {
    6. // Set the color table (used to translate colour indexes to qRgb values)
    7. QVector<QRgb> colorTable;
    8. for (int i=0; i<256; i++)
    9. colorTable.push_back(qRgb(i,i,i));
    10. // Copy input Mat
    11. const uchar *qImageBuffer = (const uchar*)mat.data;
    12. // Create QImage with same dimensions as input Mat
    13. QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
    14. img.setColorTable(colorTable);
    15. return img;
    16. }
    17. // 8-bits unsigned, NO. OF CHANNELS=3
    18. else if(mat.type()==CV_8UC3)
    19. {
    20. // Copy input Mat
    21. const uchar *qImageBuffer = (const uchar*)mat.data;
    22. // Create QImage with same dimensions as input Mat
    23. QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
    24. return img.rgbSwapped();
    25. }
    26. else
    27. {
    28. // qDebug() << "ERROR: Mat could not be converted to QImage.";
    29. return QImage();
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 


    This work pretty well for low resolution and frame rate. But when resolution/frame rate increase , CPU load increase too. ( For example with the video example form windows 7 Wildlife.wmv (1280x720@30fps) : With open_cv display CPU load = 10% ,With Qt display CPU load = 48% )

    Is there a efficiently way to display an opencv image into QT?

    Thanks

  2. #2

    Default Re: efficient way to display opencv image into Qt

    No one?

    I am very surprise that no one has done this kind of experience!

    No idea?

    thanks

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: efficient way to display opencv image into Qt

    Use OpenGl display window. Then you can load your image with glTexImage2D(...) without any conversion to QImage.
    Google for some code examples.

  4. #4
    Join Date
    Jul 2015
    Location
    Ahmedabad, India
    Posts
    12
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: efficient way to display opencv image into Qt

    Hello Guys,

    I have a Qt application in which I have used opengl to display opencv video on qt central widget in windows machine but now I have ported this application into raspberry pi and while compiling this app I am getting error like "glRaster2i, glOrtho, glDrawpixels not declared in this scope" if anybody have any idea regarding this error then please let me know. I will appreciate your help.

    Thanks

    Tushar Kachhadiya

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: efficient way to display opencv image into Qt

    "glRaster2i, glOrtho, glDrawpixels not declared in this scope"
    Usually that means you haven't included one or more header files. Figure out the correct header file where these symbols are defined and #include it. Then you can post your next question, which will be how to solve the link error about these unresolved symbols.

  6. #6
    Join Date
    Jul 2015
    Location
    Ahmedabad, India
    Posts
    12
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: efficient way to display opencv image into Qt

    Hi,

    I have included all required header files. If I compiled my application without these function it was compiling successful and runs in raspberry pi but when I opened camera on Qt QLabel on click of push button the Mainwindow was hanged. Then I must need to restart os to bring back to home screen.

    Thanks,

    Tushar Kachhadiya

Similar Threads

  1. OpenCV Image to QImage
    By kaszewczyk in forum Newbie
    Replies: 3
    Last Post: 11th December 2012, 21:19
  2. Replies: 0
    Last Post: 5th October 2012, 11:17
  3. Replies: 7
    Last Post: 31st January 2012, 15:19
  4. Replies: 8
    Last Post: 18th March 2011, 11:27
  5. Replies: 1
    Last Post: 23rd September 2010, 20:16

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.