 |
OpenCV
3.2.0
Open Source Computer Vision
|
An example using the fitEllipse technique
#include <iostream>
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;
void processImage(int, void*);
int main( int argc, char** argv )
{
"{help h||}{@image|../data/stuff.jpg|}"
);
if (parser.has("help"))
{
help();
return 0;
}
string filename = parser.get<string>("@image");
if( image.empty() )
{
cout << "Couldn't open image " << filename << "\n";
return 0;
}
createTrackbar(
"threshold",
"result", &sliderPos, 255, processImage );
processImage(0, 0);
return 0;
}
void processImage(int , void*)
{
vector<vector<Point> > contours;
Mat bimage = image >= sliderPos;
for(
size_t i = 0;
i < contours.size();
i++)
{
size_t count = contours[
i].size();
if( count < 6 )
continue;
continue;
ellipse(cimage, box.
center, box.
size*0.5f, box.
angle, 0, 360,
Scalar(0,255,255), 1,
LINE_AA);
for( int j = 0; j < 4; j++ )
}
}
static Scalar_< double > all(double v0)
returns a scalar with all elements set to v0
@ RETR_LIST
Definition: imgproc.hpp:429
static MatExpr zeros(int rows, int cols, int type)
Returns a zero array of the specified size and type.
int waitKey(int delay=0)
Waits for a pressed key.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
void convertTo(OutputArray m, int rtype, double alpha=1, double beta=0) const
Converts an array to another data type with optional scaling.
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.
#define CV_32F
Definition: interface.h:72
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
_Tp width
Definition: types.hpp:308
Point2f center
Definition: types.hpp:491
#define CV_8UC3
Definition: interface.h:84
MatSize size
Definition: mat.hpp:1978
#define MIN(a, b)
Definition: cvdef.h:410
Size2f size
Definition: types.hpp:492
The class represents rotated (i.e. not up-right) rectangles on a plane.
Definition: types.hpp:465
@ LINE_AA
antialiased line
Definition: core.hpp:218
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
void points(Point2f pts[]) const
Scalar_< double > Scalar
Definition: types.hpp:606
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.
n-dimensional dense array class
Definition: mat.hpp:741
for i
Definition: modelConvert.m:63
float angle
Definition: types.hpp:493
RotatedRect fitEllipse(InputArray points)
Fits an ellipse around a set of 2D points.
Designed for command line parsing.
Definition: utility.hpp:735
@ CHAIN_APPROX_NONE
Definition: imgproc.hpp:445
_Tp height
Definition: types.hpp:308
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.
Definition: affine.hpp:52
#define MAX(a, b)
Definition: cvdef.h:414
void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())
Finds contours in a binary image.
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.