• Open Source Computer Vision Library

Canny边缘检测2

Wikipedia,自由的百科全书

运行结果:

Image:Example-edge.png

代码:

  1. #ifdef _CH_
  2. #pragma package <opencv>
  3. #endif
  4.  
  5. #ifndef _EiC
  6. #include "cv.h"
  7. #include "highgui.h"
  8. #endif
  9.  
  10. char wndname[] = "Edge";
  11. char tbarname[] = "Threshold";
  12.  
  13.  
  14. IplImage *image = 0, *cedge = 0, *gray = 0, *edge = 0;
  15.  
  16. // define a trackbar callback
  17. void on_trackbar(int h)
  18. {
  19. cvSmooth( gray, edge, CV_BLUR, 3, 3, 0, 0 );
  20. cvNot( gray, edge );
  21.  
  22. // Run the edge detector on grayscale
  23. cvCanny(gray, edge, (float)h, (float)h*3, 3);
  24.  
  25. cvZero( cedge );
  26. // copy edge points
  27. cvCopy( image, cedge, edge );
  28.  
  29. cvShowImage(wndname, cedge);
  30. }
  31.  
  32. int main( int argc, char** argv )
  33. {
  34. char* filename = argc == 2 ? argv[1] : (char*)"fruits.jpg";
  35. int edge_thresh = 1;
  36. if( (image = cvLoadImage( filename, 1)) == 0 )
  37. return -1;
  38.  
  39. // Create the output image
  40. cedge = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 3);
  41.  
  42. // Convert to grayscale
  43. gray = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 1);
  44. edge = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 1);
  45. cvCvtColor(image, gray, CV_BGR2GRAY);
  46.  
  47. // Create a window
  48. cvNamedWindow(wndname, 1);
  49.  
  50. // create a toolbar
  51. cvCreateTrackbar(tbarname, wndname, &edge_thresh, 100, on_trackbar);
  52.  
  53. // Show the image
  54. on_trackbar(0);
  55.  
  56. // Wait for a key stroke; the same function arranges events processing
  57. cvWaitKey(0);
  58. cvReleaseImage(&image);
  59. cvReleaseImage(&gray);
  60. cvReleaseImage(&edge);
  61. cvDestroyWindow(wndname);
  62.  
  63. return 0;
  64. }
  65.  
  66. #ifdef _EiC
  67. main(1,"edge.c");
  68. #endif
  69.  

Python版本

  1. #! /usr/bin/env python
  2.  
  3. print "OpenCV Python version of edge"
  4.  
  5. import sys
  6.  
  7. # import the necessary things for OpenCV
  8. from opencv import cv
  9. from opencv import highgui
  10.  
  11. # some definitions
  12. win_name = "Edge"
  13. trackbar_name = "Threshold"
  14.  
  15. # the callback on the trackbar
  16. def on_trackbar (position):
  17.  
  18. cv.cvSmooth (gray, edge, cv.CV_BLUR, 3, 3, 0)
  19. cv.cvNot (gray, edge)
  20.  
  21. # run the edge dector on gray scale
  22. cv.cvCanny (gray, edge, position, position * 3, 3)
  23.  
  24. # reset
  25. cv.cvSetZero (col_edge)
  26.  
  27. # copy edge points
  28. cv.cvCopy (image, col_edge, edge)
  29.  
  30. # show the image
  31. highgui.cvShowImage (win_name, col_edge)
  32.  
  33. if __name__ == '__main__':
  34. filename = "../c/fruits.jpg"
  35.  
  36. if len(sys.argv)>1:
  37. filename = sys.argv[1]
  38.  
  39. # load the image gived on the command line
  40. image = highgui.cvLoadImage (filename)
  41.  
  42. if not image:
  43. print "Error loading image '%s'" % filename
  44. sys.exit(-1)
  45.  
  46. # create the output image
  47. col_edge = cv.cvCreateImage (cv.cvSize (image.width, image.height), 8, 3)
  48.  
  49. # convert to grayscale
  50. gray = cv.cvCreateImage (cv.cvSize (image.width, image.height), 8, 1)
  51. edge = cv.cvCreateImage (cv.cvSize (image.width, image.height), 8, 1)
  52. cv.cvCvtColor (image, gray, cv.CV_BGR2GRAY)
  53.  
  54. # create the window
  55. highgui.cvNamedWindow (win_name, highgui.CV_WINDOW_AUTOSIZE)
  56.  
  57. # create the trackbar
  58. highgui.cvCreateTrackbar (trackbar_name, win_name, 1, 100, on_trackbar)
  59.  
  60. # show the image
  61. on_trackbar (0)
  62.  
  63. # wait a key pressed to end
  64. highgui.cvWaitKey (0)
  65.  
Views
Personal tools