/code/FullApp/OpenCV/lib/iPhoneSimulator/share/opencv/samples/c/watershed.cpp

https://github.com/siegleal/iSanta · C++ · 147 lines · 123 code · 21 blank · 3 comment · 33 complexity · b5d39b1263418087b7266d8f2e9ecec2 MD5 · raw file

  1. #ifdef _CH_
  2. #pragma package <opencv>
  3. #endif
  4. #ifndef _EiC
  5. #include "cv.h"
  6. #include "highgui.h"
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #endif
  10. IplImage* marker_mask = 0;
  11. IplImage* markers = 0;
  12. IplImage* img0 = 0, *img = 0, *img_gray = 0, *wshed = 0;
  13. CvPoint prev_pt = {-1,-1};
  14. void on_mouse( int event, int x, int y, int flags, void* param )
  15. {
  16. if( !img )
  17. return;
  18. if( event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON) )
  19. prev_pt = cvPoint(-1,-1);
  20. else if( event == CV_EVENT_LBUTTONDOWN )
  21. prev_pt = cvPoint(x,y);
  22. else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON) )
  23. {
  24. CvPoint pt = cvPoint(x,y);
  25. if( prev_pt.x < 0 )
  26. prev_pt = pt;
  27. cvLine( marker_mask, prev_pt, pt, cvScalarAll(255), 5, 8, 0 );
  28. cvLine( img, prev_pt, pt, cvScalarAll(255), 5, 8, 0 );
  29. prev_pt = pt;
  30. cvShowImage( "image", img );
  31. }
  32. }
  33. int main( int argc, char** argv )
  34. {
  35. char* filename = argc >= 2 ? argv[1] : (char*)"fruits.jpg";
  36. CvRNG rng = cvRNG(-1);
  37. if( (img0 = cvLoadImage(filename,1)) == 0 )
  38. return 0;
  39. printf( "Hot keys: \n"
  40. "\tESC - quit the program\n"
  41. "\tr - restore the original image\n"
  42. "\tw or SPACE - run watershed algorithm\n"
  43. "\t\t(before running it, roughly mark the areas on the image)\n"
  44. "\t (before that, roughly outline several markers on the image)\n" );
  45. cvNamedWindow( "image", 1 );
  46. cvNamedWindow( "watershed transform", 1 );
  47. img = cvCloneImage( img0 );
  48. img_gray = cvCloneImage( img0 );
  49. wshed = cvCloneImage( img0 );
  50. marker_mask = cvCreateImage( cvGetSize(img), 8, 1 );
  51. markers = cvCreateImage( cvGetSize(img), IPL_DEPTH_32S, 1 );
  52. cvCvtColor( img, marker_mask, CV_BGR2GRAY );
  53. cvCvtColor( marker_mask, img_gray, CV_GRAY2BGR );
  54. cvZero( marker_mask );
  55. cvZero( wshed );
  56. cvShowImage( "image", img );
  57. cvShowImage( "watershed transform", wshed );
  58. cvSetMouseCallback( "image", on_mouse, 0 );
  59. for(;;)
  60. {
  61. int c = cvWaitKey(0);
  62. if( (char)c == 27 )
  63. break;
  64. if( (char)c == 'r' )
  65. {
  66. cvZero( marker_mask );
  67. cvCopy( img0, img );
  68. cvShowImage( "image", img );
  69. }
  70. if( (char)c == 'w' || (char)c == ' ' )
  71. {
  72. CvMemStorage* storage = cvCreateMemStorage(0);
  73. CvSeq* contours = 0;
  74. CvMat* color_tab;
  75. int i, j, comp_count = 0;
  76. //cvSaveImage( "wshed_mask.png", marker_mask );
  77. //marker_mask = cvLoadImage( "wshed_mask.png", 0 );
  78. cvFindContours( marker_mask, storage, &contours, sizeof(CvContour),
  79. CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
  80. cvZero( markers );
  81. for( ; contours != 0; contours = contours->h_next, comp_count++ )
  82. {
  83. cvDrawContours( markers, contours, cvScalarAll(comp_count+1),
  84. cvScalarAll(comp_count+1), -1, -1, 8, cvPoint(0,0) );
  85. }
  86. color_tab = cvCreateMat( 1, comp_count, CV_8UC3 );
  87. for( i = 0; i < comp_count; i++ )
  88. {
  89. uchar* ptr = color_tab->data.ptr + i*3;
  90. ptr[0] = (uchar)(cvRandInt(&rng)%180 + 50);
  91. ptr[1] = (uchar)(cvRandInt(&rng)%180 + 50);
  92. ptr[2] = (uchar)(cvRandInt(&rng)%180 + 50);
  93. }
  94. {
  95. double t = (double)cvGetTickCount();
  96. cvWatershed( img0, markers );
  97. t = (double)cvGetTickCount() - t;
  98. printf( "exec time = %gms\n", t/(cvGetTickFrequency()*1000.) );
  99. }
  100. // paint the watershed image
  101. for( i = 0; i < markers->height; i++ )
  102. for( j = 0; j < markers->width; j++ )
  103. {
  104. int idx = CV_IMAGE_ELEM( markers, int, i, j );
  105. uchar* dst = &CV_IMAGE_ELEM( wshed, uchar, i, j*3 );
  106. if( idx == -1 )
  107. dst[0] = dst[1] = dst[2] = (uchar)255;
  108. else if( idx <= 0 || idx > comp_count )
  109. dst[0] = dst[1] = dst[2] = (uchar)0; // should not get here
  110. else
  111. {
  112. uchar* ptr = color_tab->data.ptr + (idx-1)*3;
  113. dst[0] = ptr[0]; dst[1] = ptr[1]; dst[2] = ptr[2];
  114. }
  115. }
  116. cvAddWeighted( wshed, 0.5, img_gray, 0.5, 0, wshed );
  117. cvShowImage( "watershed transform", wshed );
  118. cvReleaseMemStorage( &storage );
  119. cvReleaseMat( &color_tab );
  120. }
  121. }
  122. return 1;
  123. }
  124. #ifdef _EiC
  125. main(1,"watershed.cpp");
  126. #endif