/santa/OpenCV/samples/c/edge.c

https://github.com/siegleal/iSanta · C · 68 lines · 42 code · 17 blank · 9 comment · 3 complexity · e3923700f2a173f8a2b524262e5d70b2 MD5 · raw file

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