2
* @function findContours_Demo.cpp
3
* @brief Demo code to find contours in an image
7
#include "opencv2/imgcodecs.hpp"
8
#include "opencv2/highgui/highgui.hpp"
9
#include "opencv2/imgproc/imgproc.hpp"
17
Mat src; Mat src_gray;
23
void thresh_callback(int, void* );
28
int main( int, char** argv )
31
src = imread(argv[1]);
34
cerr << "No image supplied ..." << endl;
38
/// Convert image to gray and blur it
39
cvtColor( src, src_gray, COLOR_BGR2GRAY );
40
blur( src_gray, src_gray, Size(3,3) );
43
const char* source_window = "Source";
44
namedWindow( source_window, WINDOW_AUTOSIZE );
45
imshow( source_window, src );
47
createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );
48
thresh_callback( 0, 0 );
55
* @function thresh_callback
57
void thresh_callback(int, void* )
60
vector<vector<Point> > contours;
61
vector<Vec4i> hierarchy;
63
/// Detect edges using canny
64
Canny( src_gray, canny_output, thresh, thresh*2, 3 );
66
findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
69
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
70
for( size_t i = 0; i< contours.size(); i++ )
72
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
73
drawContours( drawing, contours, (int)i, color, 2, 8, hierarchy, 0, Point() );
77
namedWindow( "Contours", WINDOW_AUTOSIZE );
78
imshow( "Contours", drawing );