2010-05-11 19:44:00 +02:00
|
|
|
/********************************************************************************
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is demonstration for ellipse fitting. Program finds
|
|
|
|
* contours and approximate it by ellipses.
|
|
|
|
*
|
|
|
|
* Trackbar specify threshold parametr.
|
|
|
|
*
|
|
|
|
* White lines is contours. Red lines is fitting ellipses.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Autor: Denis Burenkov.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
********************************************************************************/
|
2010-11-28 00:15:16 +01:00
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
2010-11-26 18:59:40 +01:00
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
2010-11-28 00:15:16 +01:00
|
|
|
#include <iostream>
|
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2010-12-04 09:30:19 +01:00
|
|
|
void help()
|
|
|
|
{
|
|
|
|
cout <<
|
|
|
|
"\nThis program is demonstration for ellipse fitting. The program finds\n"
|
|
|
|
"contours and approximate it by ellipses.\n"
|
|
|
|
"Call:\n"
|
|
|
|
"./fitellipse [image_name -- Default stuff.jpg]\n" << endl;
|
|
|
|
}
|
|
|
|
|
2010-11-28 00:15:16 +01:00
|
|
|
int sliderPos = 70;
|
|
|
|
|
|
|
|
Mat image;
|
|
|
|
|
|
|
|
void processImage(int, void*);
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
int main( int argc, char** argv )
|
|
|
|
{
|
|
|
|
const char* filename = argc == 2 ? argv[1] : (char*)"stuff.jpg";
|
2010-11-28 00:15:16 +01:00
|
|
|
image = imread(filename, 0);
|
|
|
|
if( image.empty() )
|
|
|
|
{
|
2010-12-04 09:30:19 +01:00
|
|
|
cout << "Couldn't open image " << filename << "\nUsage: fitellipse <image_name>\n";
|
2010-11-28 00:15:16 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2010-11-28 00:15:16 +01:00
|
|
|
imshow("source", image);
|
|
|
|
namedWindow("result", 1);
|
|
|
|
|
2010-05-11 19:44:00 +02:00
|
|
|
// Create toolbars. HighGUI use.
|
2010-11-28 00:15:16 +01:00
|
|
|
createTrackbar( "threshold", "result", &sliderPos, 255, processImage );
|
|
|
|
processImage(0, 0);
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
// Wait for a key stroke; the same function arranges events processing
|
2010-11-28 00:15:16 +01:00
|
|
|
waitKey();
|
2010-05-11 19:44:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define trackbar callback functon. This function find contours,
|
|
|
|
// draw it and approximate it by ellipses.
|
2010-12-21 12:37:08 +01:00
|
|
|
void processImage(int /*h*/, void*)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2010-11-28 00:15:16 +01:00
|
|
|
vector<vector<Point> > contours;
|
|
|
|
Mat bimage = image >= sliderPos;
|
|
|
|
|
|
|
|
findContours(bimage, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2010-11-28 00:15:16 +01:00
|
|
|
Mat cimage = Mat::zeros(bimage.size(), CV_8UC3);
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2010-11-28 00:15:16 +01:00
|
|
|
for(size_t i = 0; i < contours.size(); i++)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2010-11-28 00:15:16 +01:00
|
|
|
size_t count = contours[i].size();
|
2010-05-11 19:44:00 +02:00
|
|
|
if( count < 6 )
|
|
|
|
continue;
|
2010-11-28 00:15:16 +01:00
|
|
|
|
|
|
|
Mat pointsf;
|
|
|
|
Mat(contours[i]).convertTo(pointsf, CV_32F);
|
|
|
|
RotatedRect box = fitEllipse(pointsf);
|
|
|
|
|
|
|
|
if( MAX(box.size.width, box.size.height) > MIN(box.size.width, box.size.height)*30 )
|
|
|
|
continue;
|
|
|
|
drawContours(cimage, contours, (int)i, Scalar::all(255), 1, 8);
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2010-11-28 00:15:16 +01:00
|
|
|
ellipse(cimage, box, Scalar(0,0,255), 1, CV_AA);
|
2010-11-29 19:14:08 +01:00
|
|
|
ellipse(cimage, box.center, box.size*0.5f, box.angle, 0, 360, Scalar(0,255,255), 1, CV_AA);
|
|
|
|
Point2f vtx[4];
|
|
|
|
box.points(vtx);
|
|
|
|
for( int j = 0; j < 4; j++ )
|
|
|
|
line(cimage, vtx[j], vtx[(j+1)%4], Scalar(0,255,0), 1, CV_AA);
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
2010-11-28 00:15:16 +01:00
|
|
|
imshow("result", cimage);
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|