OpenCV  3.2.0
Open Source Computer Vision
fitellipse.cpp

An example using the fitEllipse technique

/********************************************************************************
*
*
* 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.
*
*
*
********************************************************************************/
#include <iostream>
using namespace cv;
using namespace std;
static 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 ../data/stuff.jpg]\n" << endl;
}
int sliderPos = 70;
Mat image;
void processImage(int, void*);
int main( int argc, char** argv )
{
cv::CommandLineParser parser(argc, argv,
"{help h||}{@image|../data/stuff.jpg|}"
);
if (parser.has("help"))
{
help();
return 0;
}
string filename = parser.get<string>("@image");
image = imread(filename, 0);
if( image.empty() )
{
cout << "Couldn't open image " << filename << "\n";
return 0;
}
imshow("source", image);
namedWindow("result", 1);
// Create toolbars. HighGUI use.
createTrackbar( "threshold", "result", &sliderPos, 255, processImage );
processImage(0, 0);
// Wait for a key stroke; the same function arranges events processing
return 0;
}
// Define trackbar callback functon. This function find contours,
// draw it and approximate it by ellipses.
void processImage(int /*h*/, void*)
{
vector<vector<Point> > contours;
Mat bimage = image >= sliderPos;
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);
}
cv::Point_< float >
cv::Scalar_< double >::all
static Scalar_< double > all(double v0)
returns a scalar with all elements set to v0
imgproc.hpp
cv::RETR_LIST
@ RETR_LIST
Definition: imgproc.hpp:429
cv::Mat::zeros
static MatExpr zeros(int rows, int cols, int type)
Returns a zero array of the specified size and type.
cv::waitKey
int waitKey(int delay=0)
Waits for a pressed key.
highgui.hpp
cv::namedWindow
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
cv::Scalar_< double >
cv::Mat::convertTo
void convertTo(OutputArray m, int rtype, double alpha=1, double beta=0) const
Converts an array to another data type with optional scaling.
cv::line
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.
CV_32F
#define CV_32F
Definition: interface.h:72
cv::imread
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
cv::Size_::width
_Tp width
Definition: types.hpp:308
cv::RotatedRect::center
Point2f center
Definition: types.hpp:491
CV_8UC3
#define CV_8UC3
Definition: interface.h:84
cv::Mat::size
MatSize size
Definition: mat.hpp:1978
imgcodecs.hpp
MIN
#define MIN(a, b)
Definition: cvdef.h:410
cv::RotatedRect::size
Size2f size
Definition: types.hpp:492
cv::RotatedRect
The class represents rotated (i.e. not up-right) rectangles on a plane.
Definition: types.hpp:465
cv::LINE_AA
@ LINE_AA
antialiased line
Definition: core.hpp:218
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
cv::RotatedRect::points
void points(Point2f pts[]) const
cv::Scalar
Scalar_< double > Scalar
Definition: types.hpp:606
cv::drawContours
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.
cv::Mat
n-dimensional dense array class
Definition: mat.hpp:741
i
for i
Definition: modelConvert.m:63
cv::RotatedRect::angle
float angle
Definition: types.hpp:493
cv::fitEllipse
RotatedRect fitEllipse(InputArray points)
Fits an ellipse around a set of 2D points.
cv::CommandLineParser
Designed for command line parsing.
Definition: utility.hpp:735
cv::CHAIN_APPROX_NONE
@ CHAIN_APPROX_NONE
Definition: imgproc.hpp:445
cv::Size_::height
_Tp height
Definition: types.hpp:308
cv::createTrackbar
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
Creates a trackbar and attaches it to the specified window.
cv
Definition: affine.hpp:52
MAX
#define MAX(a, b)
Definition: cvdef.h:414
cv::findContours
void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())
Finds contours in a binary image.
cv::ellipse
void ellipse(InputOutputArray img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a simple or thick elliptic arc or fills an ellipse sector.