/********************************************************************************
*
*
* This program is demonstration for ellipse fitting. Program finds
* contours and approximate it by ellipses using three methods.
* 1: OpenCV's original method fitEllipse which implements Fitzgibbon 1995 method.
* 2: The Approximate Mean Square (AMS) method fitEllipseAMS proposed by Taubin 1991
* 3: The Direct least square (Direct) method fitEllipseDirect proposed by Fitzgibbon1999.
*
* Trackbar specify threshold parameter.
*
* White lines is contours/input points and the true ellipse used to generate the data.
* 1: Blue lines is fitting ellipses using openCV's original method.
* 2: Green lines is fitting ellipses using the AMS method.
* 3: Red lines is fitting ellipses using the Direct method.
*
*
* Original Author: Denis Burenkov
* AMS and Direct Methods Author: Jasper Shemilt
*
*
********************************************************************************/
#include <iostream>
class canvas{
public:
bool setupQ;
int minDims,maxDims;
double scale;
int rows, cols;
void init(int minD, int maxD){
// Initialise the canvas with minimum and maximum rows and column sizes.
minDims = minD; maxDims = maxD;
scale = 1.0;
rows = 0;
cols = 0;
setupQ = false;
}
// Stretch the canvas to include the points min and max.
if(setupQ){
if(corner.
x < max.
x){corner.
x = (int)(max.
x + 1.0);};
if(corner.
y < max.
y){corner.
y = (int)(max.
y + 1.0);};
if(origin.
x > min.
x){origin.
x = (int) min.
x;};
if(origin.
y > min.
y){origin.
y = (int) min.
y;};
} else {
corner =
cv::Point((
int)(max.
x + 1.0), (
int)(max.
y + 1.0));
}
int c = (int)(scale*((corner.
x + 1.0) - origin.
x));
if(c<minDims){
scale = scale * (double)minDims/(double)c;
} else {
if(c>maxDims){
scale = scale * (double)maxDims/(double)c;
}
}
int r = (int)(scale*((corner.
y + 1.0) - origin.
y));
if(r<minDims){
scale = scale * (double)minDims/(double)r;
} else {
if(r>maxDims){
scale = scale * (double)maxDims/(double)r;
}
}
cols = (int)(scale*((corner.
x + 1.0) - origin.
x));
rows = (int)(scale*((corner.
y + 1.0) - origin.
y));
setupQ = true;
}
void stretch(vector<Point2f> pts)
{ // Stretch the canvas so all the points pts are on the canvas.
for(size_t i=1; i < pts.size(); i++){
if(max.
x < pnt.
x){max.
x = pnt.
x;};
if(max.
y < pnt.
y){max.
y = pnt.
y;};
if(min.
x > pnt.
x){min.
x = pnt.
x;};
if(min.
y > pnt.
y){min.
y = pnt.
y;};
};
stretch(min, max);
}
{ // Stretch the canvas so that the rectangle box is on the canvas.
for( int i = 0; i < 4; i++ ){
if(max.
x < pnt.
x){max.
x = pnt.
x;};
if(max.
y < pnt.
y){max.
y = pnt.
y;};
if(min.
x > pnt.
x){min.
x = pnt.
x;};
if(min.
y > pnt.
y){min.
y = pnt.
y;};
}
stretch(min, max);
}
{
stretch(box);
}
for( int j = 0; j < 4; j++ ){
line(img, vtx[j], vtx[(j+1)%4], color, lineThickness,
LINE_AA);
}
}
void drawPoints(vector<Point2f> pts,
cv::Scalar color)
{
stretch(pts);
}
for(size_t i=0; i < pts.size(); i++){
};
}
void drawLabels( std::vector<std::string> text, std::vector<cv::Scalar> colors)
{
}
int vPos = 0;
for (size_t i=0; i < text.size(); i++) {
std::string txt = text[i];
vPos += (int)(1.3 * textsize.
height);
}
}
};
static void help()
{
cout <<
"\nThis program is demonstration for ellipse fitting. The program finds\n"
"contours and approximate it by ellipses. Three methods are used to find the \n"
"elliptical fits: fitEllipse, fitEllipseAMS and fitEllipseDirect.\n"
"Call:\n"
"./fitellipse [image_name -- Default ellipses.jpg]\n" << endl;
}
int sliderPos = 70;
bool fitEllipseQ, fitEllipseAMSQ, fitEllipseDirectQ;
void processImage(int, void*);
int main( int argc, char** argv )
{
fitEllipseQ = true;
fitEllipseAMSQ = true;
fitEllipseDirectQ = true;
{
help();
return 0;
}
string filename = parser.
get<
string>(
"@image");
if( image.empty() )
{
cout << "Couldn't open image " << filename << "\n";
return 0;
}
// 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 function. This function finds contours,
// draws them, and approximates by ellipses.
void processImage(int /*h*/, void*)
{
vector<vector<Point> > contours;
Mat bimage = image >= sliderPos;
canvas paper;
std::vector<std::string> text;
std::vector<cv::Scalar> color;
if (fitEllipseQ) {
text.push_back("OpenCV");
color.push_back(fitEllipseColor);
}
if (fitEllipseAMSQ) {
text.push_back("AMS");
color.push_back(fitEllipseAMSColor);
}
if (fitEllipseDirectQ) {
text.push_back("Direct");
color.push_back(fitEllipseDirectColor);
}
paper.drawLabels(text, color);
int margin = 2;
vector< vector<Point2f> > points;
for(size_t i = 0; i < contours.size(); i++)
{
size_t count = contours[i].size();
if( count < 6 )
continue;
vector<Point2f>pts;
for (
int j = 0; j < pointsf.
rows; j++) {
if ((pnt.
x > margin && pnt.
y > margin && pnt.
x < bimage.
cols-margin && pnt.
y < bimage.
rows-margin)) {
if(j%20==0){
pts.push_back(pnt);
}
}
}
points.push_back(pts);
}
for(size_t i = 0; i < points.size(); i++)
{
vector<Point2f> pts = points[i];
if (pts.size()<=5) {
continue;
}
if (fitEllipseQ) {
}
if (fitEllipseAMSQ) {
}
if (fitEllipseDirectQ) {
}
if (fitEllipseQ) {
paper.drawEllipseWithBox(box, fitEllipseColor, 3);
}
if (fitEllipseAMSQ) {
paper.drawEllipseWithBox(boxAMS, fitEllipseAMSColor, 2);
}
if (fitEllipseDirectQ) {
paper.drawEllipseWithBox(boxDirect, fitEllipseDirectColor, 1);
}
}
}