watershed.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <opencv2/core/utility.hpp>
  2. #include "opencv2/imgproc.hpp"
  3. #include "opencv2/imgcodecs.hpp"
  4. #include "opencv2/highgui.hpp"
  5. #include <cstdio>
  6. #include <iostream>
  7. using namespace cv;
  8. using namespace std;
  9. static void help(char** argv)
  10. {
  11. cout << "\nThis program demonstrates the famous watershed segmentation algorithm in OpenCV: watershed()\n"
  12. "Usage:\n" << argv[0] <<" [image_name -- default is fruits.jpg]\n" << endl;
  13. cout << "Hot keys: \n"
  14. "\tESC - quit the program\n"
  15. "\tr - restore the original image\n"
  16. "\tw or SPACE - run watershed segmentation algorithm\n"
  17. "\t\t(before running it, *roughly* mark the areas to segment on the image)\n"
  18. "\t (before that, roughly outline several markers on the image)\n";
  19. }
  20. Mat markerMask, img;
  21. Point prevPt(-1, -1);
  22. static void onMouse( int event, int x, int y, int flags, void* )
  23. {
  24. if( x < 0 || x >= img.cols || y < 0 || y >= img.rows )
  25. return;
  26. if( event == EVENT_LBUTTONUP || !(flags & EVENT_FLAG_LBUTTON) )
  27. prevPt = Point(-1,-1);
  28. else if( event == EVENT_LBUTTONDOWN )
  29. prevPt = Point(x,y);
  30. else if( event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON) )
  31. {
  32. Point pt(x, y);
  33. if( prevPt.x < 0 )
  34. prevPt = pt;
  35. line( markerMask, prevPt, pt, Scalar::all(255), 5, 8, 0 );
  36. line( img, prevPt, pt, Scalar::all(255), 5, 8, 0 );
  37. prevPt = pt;
  38. imshow("image", img);
  39. }
  40. }
  41. int main( int argc, char** argv )
  42. {
  43. cv::CommandLineParser parser(argc, argv, "{help h | | }{ @input | fruits.jpg | }");
  44. if (parser.has("help"))
  45. {
  46. help(argv);
  47. return 0;
  48. }
  49. string filename = samples::findFile(parser.get<string>("@input"));
  50. Mat img0 = imread(filename, 1), imgGray;
  51. if( img0.empty() )
  52. {
  53. cout << "Couldn't open image ";
  54. help(argv);
  55. return 0;
  56. }
  57. help(argv);
  58. namedWindow( "image", 1 );
  59. img0.copyTo(img);
  60. cvtColor(img, markerMask, COLOR_BGR2GRAY);
  61. cvtColor(markerMask, imgGray, COLOR_GRAY2BGR);
  62. markerMask = Scalar::all(0);
  63. imshow( "image", img );
  64. setMouseCallback( "image", onMouse, 0 );
  65. for(;;)
  66. {
  67. char c = (char)waitKey(0);
  68. if( c == 27 )
  69. break;
  70. if( c == 'r' )
  71. {
  72. markerMask = Scalar::all(0);
  73. img0.copyTo(img);
  74. imshow( "image", img );
  75. }
  76. if( c == 'w' || c == ' ' )
  77. {
  78. int i, j, compCount = 0;
  79. vector<vector<Point> > contours;
  80. vector<Vec4i> hierarchy;
  81. findContours(markerMask, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);
  82. if( contours.empty() )
  83. continue;
  84. Mat markers(markerMask.size(), CV_32S);
  85. markers = Scalar::all(0);
  86. int idx = 0;
  87. for( ; idx >= 0; idx = hierarchy[idx][0], compCount++ )
  88. drawContours(markers, contours, idx, Scalar::all(compCount+1), -1, 8, hierarchy, INT_MAX);
  89. if( compCount == 0 )
  90. continue;
  91. vector<Vec3b> colorTab;
  92. for( i = 0; i < compCount; i++ )
  93. {
  94. int b = theRNG().uniform(0, 255);
  95. int g = theRNG().uniform(0, 255);
  96. int r = theRNG().uniform(0, 255);
  97. colorTab.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r));
  98. }
  99. double t = (double)getTickCount();
  100. watershed( img0, markers );
  101. t = (double)getTickCount() - t;
  102. printf( "execution time = %gms\n", t*1000./getTickFrequency() );
  103. Mat wshed(markers.size(), CV_8UC3);
  104. // paint the watershed image
  105. for( i = 0; i < markers.rows; i++ )
  106. for( j = 0; j < markers.cols; j++ )
  107. {
  108. int index = markers.at<int>(i,j);
  109. if( index == -1 )
  110. wshed.at<Vec3b>(i,j) = Vec3b(255,255,255);
  111. else if( index <= 0 || index > compCount )
  112. wshed.at<Vec3b>(i,j) = Vec3b(0,0,0);
  113. else
  114. wshed.at<Vec3b>(i,j) = colorTab[index - 1];
  115. }
  116. wshed = wshed*0.5 + imgGray*0.5;
  117. imshow( "watershed transform", wshed );
  118. }
  119. }
  120. return 0;
  121. }