This source file includes following definitions.
- main
- processImage
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int sliderPos = 70;
Mat image;
void processImage(int, void*);
int main( int argc, char** argv )
{
const char* filename = argc == 2 ? argv[1] : (char*)"../data/stuff.jpg";
image = imread(filename, 0);
if( image.empty() )
{
cout << "Couldn't open image " << filename << "\nUsage: fitellipse <image_name>\n";
return 0;
}
imshow("source", image);
namedWindow("result", 1);
createTrackbar( "threshold", "result", &sliderPos, 255, processImage );
processImage(0, 0);
waitKey();
return 0;
}
void processImage(int , void*)
{
vector<vector<Point> > contours;
Mat bimage = image >= sliderPos;
findContours(bimage, contours, RETR_LIST, CHAIN_APPROX_NONE);
Mat cimage = Mat::zeros(bimage.size(), CV_8UC3);
for(size_t i = 0; i < contours.size(); i++)
{
size_t count = contours[i].size();
if( count < 6 )
continue;
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);
ellipse(cimage, box, Scalar(0,0,255), 1, LINE_AA);
ellipse(cimage, box.center, box.size*0.5f, box.angle, 0, 360, Scalar(0,255,255), 1, LINE_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, LINE_AA);
}
imshow("result", cimage);
}