Hello,

I'm working on a project and i've to find the center of circles.

I use OpenCv to find approximatively the center, then i use lsfitnonlineariteration function on the edges points (by canny), but the program crashes.

Thanks u.

Sources:

**Code:**

// the result of Hough algorithm on the picture is (161, 121) with a radius of 50 px.

int centre_x = 161, centre_y = 121, r = 50;

Mat image=imread("image.bmp", -1), edges;

Canny(image, edges, 50, 100);

vector<Point> pointsContour; // pointsContour contains the edges points (the superior part of the circle)

for(int i=0; i<centre_y; i++)

{

uchar *ptr = edges.ptr<uchar>(i);

for(int j=0; j<edges.cols; j++)

{

if(ptr[j])

{

pointsContour.push_back(Point(j, i));

}

}

}

int n = pointsContour.size(), m = 1, k = 2;

ap::real_1d_array y, c;

ap::real_2d_array x;

lsfitreport rep;

lsfitstate state;

x.setlength(n, m);

y.setlength(n);

c.setlength(k);

for(int i=0; i<n; i++)

{

x(i, 0) = pointsContour[i].x;

y(i) = pointsContour[i].y;

}

c(0) = centre_x;

c(1) = centre_y;

lsfitnonlinearfgh(x, y, c, n, m, k, state);

lsfitnonlinearsetcond(state, 0.1, 0.1, 0);

while(lsfitnonlineariteration(state))

{

/*

F = sqrt(r? - (x - xo)?) + yo

*/

state.f = sqrt(carre(r) - carre(state.x(0) - state.c(0))) + state.c(1);

if(state.needfg || state.needfgh)

{

/*

DF/Dxo = (-2*xo + x) / (2 * sqrt(r? - (x - xo)?))

DF/Dyo = 1;

*/

state.g(0) = (- 2.0 * state.c(0) + state.x(0)) /

(2.0 * sqrt(carre(r) - carre(state.x(0) - state.c(0))));

state.g(1) = 1.0;

}

if(state.needfgh)

{

/*

D?F/Dxo? = -(-2*xo + x) / (4 * (r? - (x - xo)?)^(3/2))

*/

state.h(0, 0) = - (-2.0 * state.c(0) + state.x(0)) /

(4.0 * (carre(r) - carre(state.x(0) - state.c(0))) * sqrt((carre(r) - carre(state.x(0) - state.c(0)))));

state.h(0, 1) = 0.0;

state.h(1, 0) = 0.0;

state.h(1, 1) = 0.0;

}

}

int info;

lsfitnonlinearresults(state, info, c, rep);

printf("%f\t%f\n", c(0), c(1));