 |
OpenCV
3.2.0
Open Source Computer Vision
|
An example using the watershed algorithm
#include <cstdio>
#include <iostream>
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 )
{
if (parser.has("help"))
{
help();
return 0;
}
string filename = parser.get<string>("@input");
if( img0.empty() )
{
cout << "Couldn'g open image " << filename << ". Usage: watershed <image_name>\n";
return 0;
}
help();
img0.copyTo(img);
for(;;)
{
if( c == 27 )
break;
if( c == 'r' )
{
img0.copyTo(img);
}
if( c == 'w' || 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);
else if( index <= 0 || index > compCount )
else
}
wshed = wshed*0.5 + imgGray*0.5;
imshow(
"watershed transform", wshed );
}
}
return 0;
}
int uniform(int a, int b)
returns uniformly distributed integer random number from [a,b) range
Vec< uchar, 3 > Vec3b
Definition: matx.hpp:364
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
Definition: mat.hpp:1959
void watershed(InputArray image, InputOutputArray markers)
Performs a marker-based image segmentation using the watershed algorithm.
static Scalar_< double > all(double v0)
returns a scalar with all elements set to v0
@ RETR_CCOMP
Definition: imgproc.hpp:434
RNG & theRNG()
Returns the default random number generator.
@ EVENT_FLAG_LBUTTON
indicates that the left mouse button is down.
Definition: highgui.hpp:221
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
Converts an image from one color space to another.
int waitKey(int delay=0)
Waits for a pressed key.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
@ EVENT_LBUTTONDOWN
indicates that the left mouse button is pressed.
Definition: highgui.hpp:206
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a line segment connecting two points.
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
@ COLOR_BGR2GRAY
convert between RGB/BGR and grayscale, color conversions
Definition: imgproc.hpp:538
Template class for short numerical vectors, a partial case of Matx.
Definition: matx.hpp:306
int cols
Definition: mat.hpp:1959
#define CV_8UC3
Definition: interface.h:84
MatSize size
Definition: mat.hpp:1978
int64 getTickCount()
Returns the number of ticks.
unsigned char uchar
Definition: interface.h:47
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
double getTickFrequency()
Returns the number of ticks per second.
void drawContours(InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar &color, int thickness=1, int lineType=LINE_8, InputArray hierarchy=noArray(), int maxLevel=INT_MAX, Point offset=Point())
Draws contours outlines or filled contours.
void setMouseCallback(const String &winname, MouseCallback onMouse, void *userdata=0)
Sets mouse handler for the specified window.
Point2i Point
Definition: types.hpp:183
@ EVENT_MOUSEMOVE
indicates that the mouse pointer has moved over the window.
Definition: highgui.hpp:205
@ index
Definition: gr_skig.hpp:77
#define CV_32S
Definition: interface.h:71
n-dimensional dense array class
Definition: mat.hpp:741
for i
Definition: modelConvert.m:63
Designed for command line parsing.
Definition: utility.hpp:735
Definition: affine.hpp:52
@ EVENT_LBUTTONUP
indicates that left mouse button is released.
Definition: highgui.hpp:209
@ COLOR_GRAY2BGR
Definition: imgproc.hpp:540
@ CHAIN_APPROX_SIMPLE
Definition: imgproc.hpp:448
void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())
Finds contours in a binary image.