Results 1 to 4 of 4

Thread: How to find a color contours after convert img to black and white??

  1. #1
    Join Date
    Jun 2014
    Posts
    33
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to find a color contours after convert img to black and white??

    Hi all,

    I have already converted the img to black and white,

    i would like to find the contours and use a rectangle frame to show frame the wanted part.
    I am not sure how to achieve in Qt.

    Please advise!
    Thanks a lot


    Added after 1 30 minutes:


    I have tried openCV, but fail, code as following...

    Qt Code:
    1. void Dialog::prcFrame()
    2. {
    3. imgHSV= cvCloneImage(frame);
    4. cvCvtColor(frame,imgHSV,CV_BGR2HSV);
    5. imgFilter= cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,1);
    6. cvInRangeS(imgHSV,cvScalar(ui->hueSlide1->value(),ui->satSlide1->value(),ui->lumSlide1->value(),0),cvScalar(ui->hueSlide2->value(),ui->satSlide2->value(),ui->lumSlide2->value(),0),imgFilter);
    7.  
    8. QImage imgThresdhed = QImage ((const uchar*)imgFilter->imageData,imgFilter->width,imgFilter->height,QImage::Format_Indexed8).rgbSwapped();
    9. ui->filter->setPixmap(QPixmap::fromImage(imgThresdhed));
    10.  
    11. unsigned char *data_hsv= (unsigned char*)imgHSV->imageData;
    12. int step_hsv = imgHSV->widthStep/sizeof(unsigned char), chanels_hsv=imgHSV->nChannels;
    13. cvFindContours(imgFilter, storage, &contour, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE,cvPoint(0,0));
    14.  
    15. for(;contour;contour = contour->h_next)
    16. {
    17. area1=fabs(cvContourArea(contour,CV_WHOLE_SEQ,1 ));
    18.  
    19. if(area1<100 || area1>50000 )
    20. {
    21. cvSeqRemove(contour,0);
    22. continue;
    23. }
    24.  
    25. total =0;
    26. total_S=0;
    27. total_V=0;
    28.  
    29. for(l = 0;l<contour->total;++l)
    30. {
    31. pt = (CvPoint *)cvGetSeqElem(contour,l);
    32. H = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+0];
    33. S = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+1];
    34. V = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+2];
    35. total = H + total;
    36. total_S= S + total_S;
    37. total_V= V + total_V;
    38.  
    39. }
    40.  
    41. avg = total / (contour->total);
    42. avg_S=total_S / (contour->total);
    43. avg_V=total_V / (contour->total);
    44.  
    45.  
    46. if((avg>=ui->hueSlide1->value())&&(avg<=ui->hueSlide2->value())&&(avg_S>=ui->satSlide1->value())&&(avg_S<=ui->satSlide2->value())&&(avg_V>=ui->lumSlide1->value())&&(avg_V<=ui->lumSlide2->value()))
    47. {
    48. for(i = N-1; i >= 0; --i)
    49. {
    50. if(area1 > maxArea1[i])
    51. {
    52. maxArea1[i] = area1;
    53. contours1[i] = contour;
    54. for(m = (i-1); m >= 0; --m)
    55. {
    56. if(maxArea1[m] < maxArea1[m+1])
    57. {
    58. tmp_area1 = maxArea1[m+1];
    59. tmp_cont = contours1[m+1];
    60. maxArea1[m+1] = maxArea1[m];
    61. contours1[m+1] = contours1[m];
    62. maxArea1[m] = tmp_area1;
    63. contours1[m] = tmp_cont;
    64. }
    65. }
    66. break;
    67. }
    68. }
    69. }
    70. }
    71.  
    72. cvReleaseImage(&imgFilter);
    73. cvReleaseImage(&imgHSV);
    74. }
    To copy to clipboard, switch view to plain text mode 

    ERROR:
    Qt Code:
    1. OpenCV Error: Null pointer () in cvStartFindContours, file /home/pi/OpenCV-2.4.2/modules/imgproc/src/contours.cpp, line 187
    2.  
    3.  
    4.  
    5. terminate called after throwing an instance of 'cv::Exception'
    6.  
    7.  
    8.  
    9. what(): /home/pi/OpenCV-2.4.2/modules/imgproc/src/contours.cpp:187: error: (-27) in function cvStartFindContours
    10.  
    11.  
    12.  
    13.  
    14.  
    15.  
    16. The program has unexpectedly finished.
    17.  
    18.  
    19. /home/pi/qt/getCam/getCam exited with code 0
    To copy to clipboard, switch view to plain text mode 
    Last edited by YDYD; 11th July 2014 at 05:11.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,590
    Thanks
    13
    Thanked 1,586 Times in 1,515 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to find a color contours after convert img to black and white??

    Here is a wild guess: storage (the second argument to cvFindContours()) is null.

  3. The following user says thank you to ChrisW67 for this useful post:

    YDYD (11th July 2014)

  4. #3
    Join Date
    Jun 2014
    Posts
    33
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to find a color contours after convert img to black and white??

    yes, solved. Thanks.

    If i want draw rectangle, cvRectangle can't use.
    because the image is a QImage,
    any qt command can draw rectangle?

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to find a color contours after convert img to black and white??


Similar Threads

  1. Replies: 4
    Last Post: 5th August 2011, 21:04
  2. QSS, trying to make the text on the combo box WHITE not black.
    By technoViking in forum Qt Programming
    Replies: 3
    Last Post: 22nd June 2010, 00:57
  3. Image conversion black and white
    By offline in forum Qt Programming
    Replies: 1
    Last Post: 25th March 2010, 02:21
  4. İmage Convert Black and White
    By offline in forum Qt Programming
    Replies: 1
    Last Post: 8th March 2010, 06:08
  5. Printing plot black & white
    By maybe78 in forum Qwt
    Replies: 4
    Last Post: 9th October 2009, 08:39

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.