Machine Vision

Showing results for 
Search instead for 
Did you mean: 

Using IMAQ Detect Shapes to detect an ellipse

Go to solution

I am working with, what looks to me to be, elliptical images of laser spots and I want to use the IMAQ Detect Shapes (Ellipses) function to identify the perimeter of those ellipses, yet I cannot seem to get it to find the ellipse.  Here below is an image of the laser spot both before and after it has been thresholded. Yet my LabVIEW code cannot seem to recognize either as an ellipse.  I know the code works because I can pass it an image of a ideal (drawn) ellipse and it finds it easily.





I'm hoping that someone can tell me what's wrong and how to fix it.

I have attached an example project folder/code/images  (LabVIEW v14). Included in with it is a screenshot of the code being able to find the perfect (artificial) rotated ellipse.  A second screen shot shows an attempt with the foregoing actual laser spot image where the code cannot find the ellipse.  Note that after not having any luck finding a good combination of settings with the parameter clusters native to these IMAQ functions used as front-panel controls, for faster exploration of the parameter space, I revamped the code to use sliders for the many numeric values that control the image processing. I still did not find a magic combination that worked. Also, as part of the “faster exploration”, the code will make two different sounds depending on whether or not an error has occurred (bad combination of parameters, usually) or if the number of ellipses found has changed.

0 Kudos
Message 1 of 9

Have you looked into blob tools?


Once you have a binary image, blob tools are very powerful.  They tend to be a little slow.


Thoughts welcome.









0 Kudos
Message 2 of 9

I have a code that detects circles / ellipse perimeter. 

But I don't understand what are exactly you are trying to do.

1. Detect the perimeter pixels by image processing?

2. Detect the major / minor axis?

Seems to me that your issue first is to create robust threshold. 

I am using threshold by feature method. I know the expected spot size. So I am looking for particular spot size.

Is your threshold works OK?

Amit Shachaf
0 Kudos
Message 3 of 9
Accepted by topic author WNM

I was trying my code that is not really design for this kind of image.

My images had lower resolution and was on circle. 

But you can see the idea from the images. 

1. You need good auto threshold. If you know the expected size of the laser spot that can help. I can expand on this subject if you need.

2. After the threshold you need to clean the holes from the ellipse. I recommend morphological operation. Try IMAQ fill holes or IMAQ Morphology (closing)

3. Then you can use find the perimeter by subtracting Image after erosion (IMAQ Morphology) from image after dilation (IMAQ Morphology).  Perimeter = image dilation - Image erosion.

4. If you want to find the main axis the you can go from step #2 to blob analysis or alternatively use find ellipse. 

Amit Shachaf
Message 4 of 9

Here is example how to find perimeter.

Amit Shachaf
0 Kudos
Message 5 of 9

Thank you for your suggestions.  The end goal is to find the geometric center of an ellipse that best fits the perimeter of the image.   I have tried using the IMAQ GrayMorphology VI to both dilate and erode (with various numbers of iterations specified) the thresholded image followed by subtraction of the eroded image from the dilated image to get a perimeter (image below) and even with this to work with, the IMAQ Detect Shapes VI fails to find any sort of ellipse that will fit it.





0 Kudos
Message 6 of 9

Your other option is to use blob analysis to get the major axis and their crossing.

Blob analysis is using different math then detect shape (I am not familiar with the exact math of detect shape). It should always find the major and minor axis of the blob equivalent ellipse. 

In that case you need to use the whole blob and not just the perimeter. You should also close all the holes by morphology before the blob analysis.

Blob analysis will use all the pixels inside the blob. 


Amit Shachaf
0 Kudos
Message 7 of 9
Accepted by topic author WNM

I had NI support look at this problem and they too could not find an acceptable set of parameters to use with the IMAQ Detect Shapes/Ellipses to get it to work with my laser spot. I guess that VI is just more picky than it has any right to be given all the parameters that it takes to define a "goodness of fit". On to plan B.


Following AmitShachaf’s suggestion (Thanks!) I did the following and got an alternative working:

  • Threshold the image so the laser spot is a bright ellipsoid with relatively clean edges (fill value =1)
  • Fill holes in the thresholded image (IMAQ FillHole, fill value =1)
  • Filter the image with a low-pass erosion filter to clean fuzz off its edges (IMAQ RemoveParticle)
  • Using 2 copies of the thresholded/filled image, dilate the perimeter of one and erode the perimeter of the other (IMAQ Morphology – Dilate & Erode)
  • Subtract the eroded image from the dilated image to leave a perimeter line or string of points (value=1) on a black (value=0) background
  • Convert the perimeter image to an array of 1s/0s and extract from it the X/Y indices of all non-zero pixels.
  • Create an evenly sampled (around the perimeter) subset of X/Y coordinate pairs
    1. Convert the X/Y “raster scan” cartesian coordinates to polar coordinates with an origin that is more or less centered in field of points making up the perimeter
    2. Pick out coordinate pairs based on more or less even angle increments from 0-to-360°
    3. Convert the selected coordinate pairs back to cartesian coordinates with the origin back at image 0/0.
  • Pass the selected perimeter X/Y coordinate pairs to the “IMAQ Fit Ellipse 2” VI.   It should find an ellipse for a reasonably broad set of parameters.

The resulting demo (folder of VIs) is attached. (LabVIEW version 14, uses NI Vision Development and OpenG array libraries)

0 Kudos
Message 8 of 9

After further testing with more images from a variety of sources, I have revised the manner in which I'm finding the perimeter of the ellipse.   The prior routine seemed to do OK when it was able to create a filled thresholded ellipse so it could look from the center/inside out to find edge values. However, in the case of one image, I could not get the center of the ellipse to reliably fill in properly so that working from the center out was likely to find bright-to-dark transitions that were not on the actual edge of the ellipse but instead somewhere in its interior.  I managed to implement a different method where I take the thresholded image (an array of 0/1 values) and go through it row by row (Y) examining the column values (X) from the outside in (meaning that for each row I work in from the two ends of a row toward the middle) looking for where I find 3 or more non-zero values, indicating that I’ve found an outside left, right or shared edge.  This new method doesn’t allow a user to specify the number of points to be used in the curve fit.  It instead just returns all (a few thousand for the ellipses in my 5k test images) of the non-zero edge coordinate pairs that it finds.  So far this does not seem to be too many to overwhelm the fit-ellipse function (an entire image’s worth of points is however).  My new test routine is attached. It too is LabVIEW 2014 and the only external library it requires is the IMAQ Vision one.

0 Kudos
Message 9 of 9