# How to use Cvb.Foundation.CreateCircle2DByRegression

Hello,

I am struggling to work out how to use Cvb.Foundation.CreateCircle2DByRegression but it always returns 0 and I am not sure what I am missing:

``````       Cvb.Image.PIXELLIST pIXELLIST= Cvb.Image.CreatePixelList(2);
//double[] values = new double[pixels.Count * 2];
//for (int i = 0; i < pixels.Count; ++i)
//{
//  values[i * 2] = pixels[i].X;
//  values[i * 2 + 1] = pixels[i].Y;
//}

double[] values = { 0, 1, -1, 0, 1, 0 };

var cicrleRes = Cvb.Foundation.CreateCircle2DByRegression(pIXELLIST);
``````

Any ideas would be appreciated.

I have an example, I just looked it up… it is VB6 but let’s look at the logic

1. creating a pixellist, in my case I’ve incorporated it into a threshold operation from a scrollbar. It looks like you are ok there.
``````pixelList = CreatePixelList(2)
For x = 1 To xdim - 1
For y = 1 To ydim - 1
gl = PixVal(imghandle, 0, x, y)
If gl < HScrThresh.Value Then
point(Index) = x
point(Index + 1) = y

Index = Index + 2

End If
Next
Next
``````
1. after that it is easy… try these functions:

circleOutput = CreateCircle2DByRegression(pixelList)

result = GetCircle2DCenter(circleOutput, ctrX, ctrY)

1. and a display?

circPos(0).x = ctrX
circPos(0).y = ctrY
circPos(1).x = point(21)
circPos(1).y = point(22)

CVdisplay1.AddOverlayObject “Circle”, “Circle”, False, False, vbRed, vbGreen, False, 1, circPos(0).x, ObjectData

Hello Amicelli,

each pixel you add with AddPixel represents one coordinate ( x and y) on your circle. So you need to add every pixel value individually .

For example:

double[] value1 = { 2, 1 };
double[] value2 = { 3, 2 };
double[] value3 = { 2, 3 };
double[] value4 = { 1, 2 };

Hello @JonV,

this worked fine when I used a given set of values:

``````      Cvb.Image.PIXELLIST pIXELLIST = Cvb.Image.CreatePixelList(2);

bool success = Cvb.Image.AddPixel(pIXELLIST, new double[] { -1, 0 });
success = Cvb.Image.AddPixel(pIXELLIST, new double[] { 0, 1 });
success = Cvb.Image.AddPixel(pIXELLIST, new double[] { 1, 0 });

var cicrleRes = Cvb.Foundation.CreateCircle2DByRegression(pIXELLIST);
``````

But as soon as I put all the values from this image in there it was throwing an exception.

Any ideas why?

Thanks.

Hi @Amicelli,

Two questions:

1. How many points did you add? You said “all”, but I find it hard to believe that you’d try to fit a circle through all the white dots in the image.
2. Would it be possible to hand in a crash dump to support@stemmer-imaging.de?

Generally: The edge image you show does not really contain a circle, the structure to me looks elliptic (as if looking at a circle from an angle), so even if the regression does yield a result, it will probably be a fairly poor match to a circle.

Hello @illusive.

thanks for coming back to me and sorry for the late reply as I started to do something else and disregarded this approach.

I did try to replicate it again, but it does not seem to crash (if I use the supplied image). New application. Maybe something was corrupt while I was handling it in my main application… who knows…

My hope was that it would give me the best circle regardless how much “crap” I put in.

However, you are right, the form I want to look at is elliptical. Unfortunately, this adds much more to the complexity. I did try hough circles (which went all over the place) and I am currently looking at smallest enclosing circles (I was thinking of using smallest enclosing ellipsoids but it adds too many unknowns and for the first version it might be enough).