#include <cstdio>
#include <iostream>
using namespace cv;
using namespace std;
static void help()
{
cout << "\nThis program demonstrates the famous watershed segmentation algorithm in OpenCV: watershed()\n"
"Usage:\n"
"./watershed [image_name -- default is ../data/fruits.jpg]\n" << endl;
cout << "Hot keys: \n"
"\tESC - quit the program\n"
"\tr - restore the original image\n"
"\tw or SPACE - run watershed segmentation algorithm\n"
"\t\t(before running it, *roughly* mark the areas to segment on the image)\n"
"\t (before that, roughly outline several markers on the image)\n";
}
static void onMouse( int event, int x, int y, int flags, void* )
{
if( x < 0 || x >= img.cols || y < 0 || y >= img.rows )
return;
{
if( prevPt.x < 0 )
prevPt = pt;
prevPt = pt;
}
}
int main(
int argc,
char** argv )
{
char* filename = argc >= 2 ? argv[1] : (char*)"../data/fruits.jpg";
{
cout << "Couldn'g open image " << filename << ". Usage: watershed <image_name>\n";
return 0;
}
help();
for(;;)
{
if( (char)c == 27 )
break;
if( (char)c == 'r' )
{
}
if( (char)c == 'w' || (char)c == ' ' )
{
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
if( contours.empty() )
continue;
int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0], compCount++ )
if( compCount == 0 )
continue;
vector<Vec3b> colorTab;
for( i = 0; i < compCount; i++ )
{
}
for( i = 0; i < markers.rows; i++ )
for( j = 0; j < markers.cols; j++ )
{
int index = markers.at<
int>(
i,j);
if( index == -1 )
else if( index <= 0 || index > compCount )
else
wshed.at<
Vec3b>(
i,j) = colorTab[index - 1];
}
wshed = wshed*0.5 + imgGray*0.5;
imshow(
"watershed transform", wshed );
}
}
return 0;
}