1 #include "opencv2/highgui/highgui.hpp"
2 #include "opencv2/core/core.hpp"
3 #include "opencv2/imgproc/imgproc.hpp"
10 static void help(char** argv)
12 cout << "\nDemonstrate mean-shift based color segmentation in spatial pyramid.\n"
13 << "Call:\n " << argv[0] << " image\n"
14 << "This program allows you to set the spatial and color radius\n"
15 << "of the mean shift window as well as the number of pyramid reduction levels explored\n"
19 //This colors the segmentations
20 static void floodFillPostprocess( Mat& img, const Scalar& colorDiff=Scalar::all(1) )
22 CV_Assert( !img.empty() );
24 Mat mask( img.rows+2, img.cols+2, CV_8UC1, Scalar::all(0) );
25 for( int y = 0; y < img.rows; y++ )
27 for( int x = 0; x < img.cols; x++ )
29 if( mask.at<uchar>(y+1, x+1) == 0 )
31 Scalar newVal( rng(256), rng(256), rng(256) );
32 floodFill( img, mask, Point(x,y), newVal, 0, colorDiff, colorDiff );
38 string winName = "meanshift";
39 int spatialRad, colorRad, maxPyrLevel;
42 static void meanShiftSegmentation( int, void* )
44 cout << "spatialRad=" << spatialRad << "; "
45 << "colorRad=" << colorRad << "; "
46 << "maxPyrLevel=" << maxPyrLevel << endl;
47 pyrMeanShiftFiltering( img, res, spatialRad, colorRad, maxPyrLevel );
48 floodFillPostprocess( res, Scalar::all(2) );
49 imshow( winName, res );
52 int main(int argc, char** argv)
60 img = imread( argv[1] );
68 namedWindow( winName, WINDOW_AUTOSIZE );
70 createTrackbar( "spatialRad", winName, &spatialRad, 80, meanShiftSegmentation );
71 createTrackbar( "colorRad", winName, &colorRad, 60, meanShiftSegmentation );
72 createTrackbar( "maxPyrLevel", winName, &maxPyrLevel, 5, meanShiftSegmentation );
74 meanShiftSegmentation(0, 0);