• Open Source Computer Vision Library

轮廓(contour)检测2

Wikipedia,自由的百科全书

运行结果

Image:Example-contours-image.pngImage:Example-contours-contours.png

代码

  1. #ifdef _CH_
  2. #pragma package <opencv>
  3. #endif
  4.  
  5. #ifndef _EiC
  6. #include "cv.h"
  7. #include "highgui.h"
  8. #include <math.h>
  9. #endif
  10.  
  11. #define w 500
  12. int levels = 3;
  13. CvSeq* contours = 0;
  14.  
  15. void on_trackbar(int pos)
  16. {
  17. IplImage* cnt_img = cvCreateImage( cvSize(w,w), 8, 3 );
  18. CvSeq* _contours = contours;
  19. int _levels = levels - 3;
  20. if( _levels <= 0 ) // get to the nearest face to make it look more funny
  21. _contours = _contours->h_next->h_next->h_next;
  22. cvZero( cnt_img );
  23. cvDrawContours( cnt_img, _contours, CV_RGB(255,0,0), CV_RGB(0,255,0), _levels, 3, CV_AA, cvPoint(0,0) );
  24. cvShowImage( "contours", cnt_img );
  25. cvReleaseImage( &cnt_img );
  26. }
  27.  
  28. int main( int argc, char** argv )
  29. {
  30. int i, j;
  31. CvMemStorage* storage = cvCreateMemStorage(0);
  32. IplImage* img = cvCreateImage( cvSize(w,w), 8, 1 );
  33.  
  34. cvZero( img );
  35.  
  36. for( i=0; i < 6; i++ )
  37. {
  38. int dx = (i%2)*250 - 30;
  39. int dy = (i/2)*150;
  40. CvScalar white = cvRealScalar(255);
  41. CvScalar black = cvRealScalar(0);
  42.  
  43. if( i == 0 )
  44. {
  45. for( j = 0; j <= 10; j++ )
  46. {
  47. double angle = (j+5)*CV_PI/21;
  48. cvLine(img, cvPoint(cvRound(dx+100+j*10-80*cos(angle)),
  49. cvRound(dy+100-90*sin(angle))),
  50. cvPoint(cvRound(dx+100+j*10-30*cos(angle)),
  51. cvRound(dy+100-30*sin(angle))), white, 1, 8, 0);
  52. }
  53. }
  54.  
  55. cvEllipse( img, cvPoint(dx+150, dy+100), cvSize(100,70), 0, 0, 360, white, -1, 8, 0 );
  56. cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(30,20), 0, 0, 360, black, -1, 8, 0 );
  57. cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(30,20), 0, 0, 360, black, -1, 8, 0 );
  58. cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(15,15), 0, 0, 360, white, -1, 8, 0 );
  59. cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(15,15), 0, 0, 360, white, -1, 8, 0 );
  60. cvEllipse( img, cvPoint(dx+115, dy+70), cvSize(5,5), 0, 0, 360, black, -1, 8, 0 );
  61. cvEllipse( img, cvPoint(dx+185, dy+70), cvSize(5,5), 0, 0, 360, black, -1, 8, 0 );
  62. cvEllipse( img, cvPoint(dx+150, dy+100), cvSize(10,5), 0, 0, 360, black, -1, 8, 0 );
  63. cvEllipse( img, cvPoint(dx+150, dy+150), cvSize(40,10), 0, 0, 360, black, -1, 8, 0 );
  64. cvEllipse( img, cvPoint(dx+27, dy+100), cvSize(20,35), 0, 0, 360, white, -1, 8, 0 );
  65. cvEllipse( img, cvPoint(dx+273, dy+100), cvSize(20,35), 0, 0, 360, white, -1, 8, 0 );
  66. }
  67.  
  68. cvNamedWindow( "image", 1 );
  69. cvShowImage( "image", img );
  70.  
  71. cvFindContours( img, storage, &contours, sizeof(CvContour),
  72. CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
  73.  
  74. // comment this out if you do not want approximation
  75. contours = cvApproxPoly( contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, 3, 1 );
  76.  
  77. cvNamedWindow( "contours", 1 );
  78. cvCreateTrackbar( "levels+3", "contours", &levels, 7, on_trackbar );
  79.  
  80. on_trackbar(0);
  81. cvWaitKey(0);
  82. cvReleaseMemStorage( &storage );
  83. cvReleaseImage( &img );
  84.  
  85. return 0;
  86. }
  87.  
  88. #ifdef _EiC
  89. main(1,"");
  90. #endif
  91.  

Python版本

  1.  
  2. #! /usr/bin/env python
  3.  
  4. print "OpenCV Python version of contours"
  5.  
  6. # import the necessary things for OpenCV
  7. from opencv import cv
  8. from opencv import highgui
  9.  
  10. # some default constants
  11. _SIZE = 500
  12. _DEFAULT_LEVEL = 3
  13.  
  14. # definition of some colors
  15. _red = cv.cvScalar (0, 0, 255, 0);
  16. _green = cv.cvScalar (0, 255, 0, 0);
  17. _white = cv.cvRealScalar (255)
  18. _black = cv.cvRealScalar (0)
  19.  
  20. # the callback on the trackbar, to set the level of contours we want
  21. # to display
  22. def on_trackbar (position):
  23.  
  24. # create the image for putting in it the founded contours
  25. contours_image = cv.cvCreateImage (cv.cvSize (_SIZE, _SIZE), 8, 3)
  26.  
  27. # compute the real level of display, given the current position
  28. levels = position - 3
  29.  
  30. # initialisation
  31. _contours = contours
  32.  
  33. if levels <= 0:
  34. # zero or negative value
  35. # => get to the nearest face to make it look more funny
  36. _contours = contours.h_next.h_next.h_next
  37.  
  38. # first, clear the image where we will draw contours
  39. cv.cvSetZero (contours_image)
  40.  
  41. # draw contours in red and green
  42. cv.cvDrawContours (contours_image, _contours,
  43. _red, _green,
  44. levels, 3, cv.CV_AA,
  45. cv.cvPoint (0, 0))
  46.  
  47. # finally, show the image
  48. highgui.cvShowImage ("contours", contours_image)
  49.  
  50. if __name__ == '__main__':
  51.  
  52. # create the image where we want to display results
  53. image = cv.cvCreateImage (cv.cvSize (_SIZE, _SIZE), 8, 1)
  54.  
  55. # start with an empty image
  56. cv.cvSetZero (image)
  57.  
  58. # draw the original picture
  59. for i in range (6):
  60. dx = (i % 2) * 250 - 30
  61. dy = (i / 2) * 150
  62.  
  63. cv.cvEllipse (image,
  64. cv.cvPoint (dx + 150, dy + 100),
  65. cv.cvSize (100, 70),
  66. 0, 0, 360, _white, -1, 8, 0)
  67. cv.cvEllipse (image,
  68. cv.cvPoint (dx + 115, dy + 70),
  69. cv.cvSize (30, 20),
  70. 0, 0, 360, _black, -1, 8, 0)
  71. cv.cvEllipse (image,
  72. cv.cvPoint (dx + 185, dy + 70),
  73. cv.cvSize (30, 20),
  74. 0, 0, 360, _black, -1, 8, 0)
  75. cv.cvEllipse (image,
  76. cv.cvPoint (dx + 115, dy + 70),
  77. cv.cvSize (15, 15),
  78. 0, 0, 360, _white, -1, 8, 0)
  79. cv.cvEllipse (image,
  80. cv.cvPoint (dx + 185, dy + 70),
  81. cv.cvSize (15, 15),
  82. 0, 0, 360, _white, -1, 8, 0)
  83. cv.cvEllipse (image,
  84. cv.cvPoint (dx + 115, dy + 70),
  85. cv.cvSize (5, 5),
  86. 0, 0, 360, _black, -1, 8, 0)
  87. cv.cvEllipse (image,
  88. cv.cvPoint (dx + 185, dy + 70),
  89. cv.cvSize (5, 5),
  90. 0, 0, 360, _black, -1, 8, 0)
  91. cv.cvEllipse (image,
  92. cv.cvPoint (dx + 150, dy + 100),
  93. cv.cvSize (10, 5),
  94. 0, 0, 360, _black, -1, 8, 0)
  95. cv.cvEllipse (image,
  96. cv.cvPoint (dx + 150, dy + 150),
  97. cv.cvSize (40, 10),
  98. 0, 0, 360, _black, -1, 8, 0)
  99. cv.cvEllipse (image,
  100. cv.cvPoint (dx + 27, dy + 100),
  101. cv.cvSize (20, 35),
  102. 0, 0, 360, _white, -1, 8, 0)
  103. cv.cvEllipse (image,
  104. cv.cvPoint (dx + 273, dy + 100),
  105. cv.cvSize (20, 35),
  106. 0, 0, 360, _white, -1, 8, 0)
  107.  
  108. # create window and display the original picture in it
  109. highgui.cvNamedWindow ("image", 1)
  110. highgui.cvShowImage ("image", image)
  111.  
  112. # create the storage area
  113. storage = cv.cvCreateMemStorage (0)
  114.  
  115. # find the contours
  116. nb_contours, contours = cv.cvFindContours (image,
  117. storage,
  118. cv.sizeof_CvContour,
  119. cv.CV_RETR_TREE,
  120. cv.CV_CHAIN_APPROX_SIMPLE,
  121. cv.cvPoint (0,0))
  122.  
  123. # comment this out if you do not want approximation
  124. contours = cv.cvApproxPoly (contours, cv.sizeof_CvContour,
  125. storage,
  126. cv.CV_POLY_APPROX_DP, 3, 1)
  127.  
  128. # create the window for the contours
  129. highgui.cvNamedWindow ("contours", 1)
  130.  
  131. # create the trackbar, to enable the change of the displayed level
  132. highgui.cvCreateTrackbar ("levels+3", "contours", 3, 7, on_trackbar)
  133.  
  134. # call one time the callback, so we will have the 1st display done
  135. on_trackbar (_DEFAULT_LEVEL)
  136.  
  137. # wait a key pressed to end
  138. highgui.cvWaitKey (0)
  139.  
Views
Personal tools