2010-11-26 18:59:40 +01:00
|
|
|
#include <opencv2/imgproc/imgproc.hpp>
|
|
|
|
#include <opencv2/highgui/highgui.hpp>
|
2010-05-11 19:44:00 +02:00
|
|
|
#include <math.h>
|
2010-11-24 07:03:21 +01:00
|
|
|
#include <iostream>
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
using namespace cv;
|
2010-11-26 18:59:40 +01:00
|
|
|
using namespace std;
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2010-11-24 07:03:21 +01:00
|
|
|
void help()
|
|
|
|
{
|
|
|
|
cout
|
2010-12-04 09:21:11 +01:00
|
|
|
<< "\nThis program illustrates the use of findContours and drawContours\n"
|
2010-11-24 07:03:21 +01:00
|
|
|
<< "The original image is put up along with the image of drawn contours\n"
|
|
|
|
<< "Usage:\n"
|
|
|
|
<< "./contours2\n"
|
|
|
|
<< "\nA trackbar is put up which controls the contour level from -3 to 3\n"
|
|
|
|
<< endl;
|
|
|
|
}
|
|
|
|
|
2010-05-11 19:44:00 +02:00
|
|
|
const int w = 500;
|
|
|
|
int levels = 3;
|
|
|
|
|
|
|
|
vector<vector<Point> > contours;
|
|
|
|
vector<Vec4i> hierarchy;
|
|
|
|
|
2010-07-17 12:35:17 +02:00
|
|
|
void on_trackbar(int, void*)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
|
|
|
Mat cnt_img = Mat::zeros(w, w, CV_8UC3);
|
|
|
|
int _levels = levels - 3;
|
|
|
|
drawContours( cnt_img, contours, _levels <= 0 ? 3 : -1, Scalar(128,255,255),
|
|
|
|
3, CV_AA, hierarchy, std::abs(_levels) );
|
|
|
|
|
|
|
|
imshow("contours", cnt_img);
|
|
|
|
}
|
|
|
|
|
2010-11-26 19:25:30 +01:00
|
|
|
int main( int argc, char**)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
|
|
|
Mat img = Mat::zeros(w, w, CV_8UC1);
|
2010-11-24 07:03:21 +01:00
|
|
|
if(argc > 1)
|
|
|
|
{
|
|
|
|
help();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
//Draw 6 faces
|
2010-05-11 19:44:00 +02:00
|
|
|
for( int i = 0; i < 6; i++ )
|
|
|
|
{
|
|
|
|
int dx = (i%2)*250 - 30;
|
|
|
|
int dy = (i/2)*150;
|
|
|
|
const Scalar white = Scalar(255);
|
|
|
|
const Scalar black = Scalar(0);
|
|
|
|
|
|
|
|
if( i == 0 )
|
|
|
|
{
|
|
|
|
for( int j = 0; j <= 10; j++ )
|
|
|
|
{
|
|
|
|
double angle = (j+5)*CV_PI/21;
|
|
|
|
line(img, Point(cvRound(dx+100+j*10-80*cos(angle)),
|
|
|
|
cvRound(dy+100-90*sin(angle))),
|
|
|
|
Point(cvRound(dx+100+j*10-30*cos(angle)),
|
|
|
|
cvRound(dy+100-30*sin(angle))), white, 1, 8, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ellipse( img, Point(dx+150, dy+100), Size(100,70), 0, 0, 360, white, -1, 8, 0 );
|
|
|
|
ellipse( img, Point(dx+115, dy+70), Size(30,20), 0, 0, 360, black, -1, 8, 0 );
|
|
|
|
ellipse( img, Point(dx+185, dy+70), Size(30,20), 0, 0, 360, black, -1, 8, 0 );
|
|
|
|
ellipse( img, Point(dx+115, dy+70), Size(15,15), 0, 0, 360, white, -1, 8, 0 );
|
|
|
|
ellipse( img, Point(dx+185, dy+70), Size(15,15), 0, 0, 360, white, -1, 8, 0 );
|
|
|
|
ellipse( img, Point(dx+115, dy+70), Size(5,5), 0, 0, 360, black, -1, 8, 0 );
|
|
|
|
ellipse( img, Point(dx+185, dy+70), Size(5,5), 0, 0, 360, black, -1, 8, 0 );
|
|
|
|
ellipse( img, Point(dx+150, dy+100), Size(10,5), 0, 0, 360, black, -1, 8, 0 );
|
|
|
|
ellipse( img, Point(dx+150, dy+150), Size(40,10), 0, 0, 360, black, -1, 8, 0 );
|
|
|
|
ellipse( img, Point(dx+27, dy+100), Size(20,35), 0, 0, 360, white, -1, 8, 0 );
|
|
|
|
ellipse( img, Point(dx+273, dy+100), Size(20,35), 0, 0, 360, white, -1, 8, 0 );
|
|
|
|
}
|
2010-11-24 07:03:21 +01:00
|
|
|
//show the faces
|
2010-05-11 19:44:00 +02:00
|
|
|
namedWindow( "image", 1 );
|
|
|
|
imshow( "image", img );
|
2010-11-24 07:03:21 +01:00
|
|
|
//Extract the contours so that
|
2010-05-11 19:44:00 +02:00
|
|
|
vector<vector<Point> > contours0;
|
|
|
|
findContours( img, contours0, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
|
|
|
|
|
|
|
|
contours.resize(contours0.size());
|
|
|
|
for( size_t k = 0; k < contours0.size(); k++ )
|
|
|
|
approxPolyDP(Mat(contours0[k]), contours[k], 3, true);
|
|
|
|
|
|
|
|
namedWindow( "contours", 1 );
|
|
|
|
createTrackbar( "levels+3", "contours", &levels, 7, on_trackbar );
|
|
|
|
|
|
|
|
on_trackbar(0,0);
|
|
|
|
waitKey();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|