opencv/samples/cpp/snippets/watershed.cpp

145 lines
4.3 KiB
C++
Raw Normal View History

#include <opencv2/core/utility.hpp>
#include "opencv2/imgproc.hpp"
2014-07-04 22:48:15 +08:00
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
2010-11-28 07:15:16 +08:00
2010-11-29 17:31:47 +08:00
#include <cstdio>
2010-11-28 07:15:16 +08:00
#include <iostream>
using namespace cv;
using namespace std;
2019-07-28 17:09:17 +08:00
static void help(char** argv)
2010-12-04 16:31:05 +08:00
{
2012-06-08 01:21:29 +08:00
cout << "\nThis program demonstrates the famous watershed segmentation algorithm in OpenCV: watershed()\n"
2019-07-28 17:09:17 +08:00
"Usage:\n" << argv[0] <<" [image_name -- default is fruits.jpg]\n" << endl;
2010-12-04 16:31:05 +08:00
2012-06-08 01:21:29 +08:00
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";
2010-12-04 16:31:05 +08:00
}
2010-11-28 07:15:16 +08:00
Mat markerMask, img;
Point prevPt(-1, -1);
2012-06-08 01:21:29 +08:00
static void onMouse( int event, int x, int y, int flags, void* )
2010-11-28 07:15:16 +08:00
{
if( x < 0 || x >= img.cols || y < 0 || y >= img.rows )
return;
if( event == EVENT_LBUTTONUP || !(flags & EVENT_FLAG_LBUTTON) )
2010-11-28 07:15:16 +08:00
prevPt = Point(-1,-1);
else if( event == EVENT_LBUTTONDOWN )
2010-11-28 07:15:16 +08:00
prevPt = Point(x,y);
else if( event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON) )
2010-11-28 07:15:16 +08:00
{
Point pt(x, y);
if( prevPt.x < 0 )
prevPt = pt;
line( markerMask, prevPt, pt, Scalar::all(255), 5, 8, 0 );
line( img, prevPt, pt, Scalar::all(255), 5, 8, 0 );
prevPt = pt;
imshow("image", img);
}
}
int main( int argc, char** argv )
{
2018-10-31 20:48:56 +08:00
cv::CommandLineParser parser(argc, argv, "{help h | | }{ @input | fruits.jpg | }");
2015-08-01 23:24:23 +08:00
if (parser.has("help"))
{
2019-07-28 17:09:17 +08:00
help(argv);
2015-08-01 23:24:23 +08:00
return 0;
}
2018-10-31 20:48:56 +08:00
string filename = samples::findFile(parser.get<string>("@input"));
Mat img0 = imread(filename, IMREAD_COLOR), imgGray;
2012-06-08 01:21:29 +08:00
2010-11-28 07:15:16 +08:00
if( img0.empty() )
{
2019-07-28 17:09:17 +08:00
cout << "Couldn't open image ";
help(argv);
2010-11-28 07:15:16 +08:00
return 0;
}
2019-07-28 17:09:17 +08:00
help(argv);
2010-11-28 07:15:16 +08:00
namedWindow( "image", 1 );
img0.copyTo(img);
cvtColor(img, markerMask, COLOR_BGR2GRAY);
cvtColor(markerMask, imgGray, COLOR_GRAY2BGR);
2010-11-28 07:15:16 +08:00
markerMask = Scalar::all(0);
imshow( "image", img );
setMouseCallback( "image", onMouse, 0 );
for(;;)
{
char c = (char)waitKey(0);
2010-11-28 07:15:16 +08:00
if( c == 27 )
2010-11-28 07:15:16 +08:00
break;
if( c == 'r' )
2010-11-28 07:15:16 +08:00
{
markerMask = Scalar::all(0);
img0.copyTo(img);
imshow( "image", img );
}
if( c == 'w' || c == ' ' )
2010-11-28 07:15:16 +08:00
{
int i, j, compCount = 0;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
2012-06-08 01:21:29 +08:00
findContours(markerMask, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);
2012-06-08 01:21:29 +08:00
2010-12-04 23:35:34 +08:00
if( contours.empty() )
continue;
2010-11-28 07:15:16 +08:00
Mat markers(markerMask.size(), CV_32S);
markers = Scalar::all(0);
int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0], compCount++ )
drawContours(markers, contours, idx, Scalar::all(compCount+1), -1, 8, hierarchy, INT_MAX);
if( compCount == 0 )
continue;
2012-06-08 01:21:29 +08:00
2010-11-28 07:15:16 +08:00
vector<Vec3b> colorTab;
for( i = 0; i < compCount; i++ )
{
int b = theRNG().uniform(0, 255);
int g = theRNG().uniform(0, 255);
int r = theRNG().uniform(0, 255);
2012-06-08 01:21:29 +08:00
2010-11-28 07:15:16 +08:00
colorTab.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r));
}
double t = (double)getTickCount();
watershed( img0, markers );
t = (double)getTickCount() - t;
printf( "execution time = %gms\n", t*1000./getTickFrequency() );
Mat wshed(markers.size(), CV_8UC3);
2012-06-08 01:21:29 +08:00
2010-11-28 07:15:16 +08:00
// paint the watershed image
for( i = 0; i < markers.rows; i++ )
for( j = 0; j < markers.cols; j++ )
{
int index = markers.at<int>(i,j);
if( index == -1 )
2010-11-28 07:15:16 +08:00
wshed.at<Vec3b>(i,j) = Vec3b(255,255,255);
else if( index <= 0 || index > compCount )
2010-11-28 07:15:16 +08:00
wshed.at<Vec3b>(i,j) = Vec3b(0,0,0);
else
wshed.at<Vec3b>(i,j) = colorTab[index - 1];
2010-11-28 07:15:16 +08:00
}
wshed = wshed*0.5 + imgGray*0.5;
imshow( "watershed transform", wshed );
}
}
return 0;
}