Steve on Image Processing and MATLAB

Concepts, algorithms & MATLAB

Visualizing regionprops ellipse measurements 19

Posted by Steve Eddins,

The Image Processing Toolbox function regionprops gives you measurements of shape-based measurements of image regions. It's pretty useful, and I have shown many examples of it on this blog. Today I want to show you how to visualize the ellipse-based measurements produced by regionprops.

There are several supported measurements that are based on approximating regions by ellipses:

  • Orientation
  • MajorAxisLength
  • MinorAxisLength
  • Eccentricity

Here's an example.

url = '';
bw = imread(url);

Call regionprops with the list of measurements you desire:

s = regionprops(bw, 'Orientation', 'MajorAxisLength', ...
    'MinorAxisLength', 'Eccentricity')
s = 

99x1 struct array with fields:

From the output you can infer that regionprops found 99 objects in the binary image. It returned the desired measurements as a 99-element struct array. Here are the measurements for the 10th object:

ans = 

    MajorAxisLength: 28.7693
    MinorAxisLength: 9.4320
       Eccentricity: 0.9447
        Orientation: -27.0162

How can you do a visual sanity check of the output? We should probably include some regionprops visualization utilities in the toolbox, but in the meantime I'll show you some code you can use to superimpose the fitted ellipses over the original image.

First, we'll one additional measurement: the centroid.

s = regionprops(bw, 'Orientation', 'MajorAxisLength', ...
    'MinorAxisLength', 'Eccentricity', 'Centroid');

For each ellipse, we'll use a parametric form of the ellipse equation to plot the outline of the ellipse over the image.

hold on

phi = linspace(0,2*pi,50);
cosphi = cos(phi);
sinphi = sin(phi);

for k = 1:length(s)
    xbar = s(k).Centroid(1);
    ybar = s(k).Centroid(2);

    a = s(k).MajorAxisLength/2;
    b = s(k).MinorAxisLength/2;

    theta = pi*s(k).Orientation/180;
    R = [ cos(theta)   sin(theta)
         -sin(theta)   cos(theta)];

    xy = [a*cosphi; b*sinphi];
    xy = R*xy;

    x = xy(1,:) + xbar;
    y = xy(2,:) + ybar;

hold off

You can zoom in to see exactly why some of the ellipses don't seem to match objects in the image very well. Normally you'd zoom interactively using the zoom controls in the figure toolbar, but of course I can't do that in this blog post, so I'll use the axis function.

axis([140 225 85 170])

Now you can see that the two objects in the center of the display are actually touching at one point. That caused regionprops to consider them to be a single object and to fit an ellipse to both of them together.

Get the MATLAB code

Published with MATLAB® 7.10


Comments are closed.

19 CommentsOldest to Newest

Nevine replied on : 1 of 19
In the images I have worked with, I have been able to eliminate such false positives by using the 'Solidity' property of regionprops. In your example, eliminating objects that have Solidity less than 0.8 should work.
Matthew replied on : 2 of 19
The types of errors shown at the end of the post can be minimized by switching from 8-connectivity to 4-connectivity. I think that showing the utility of that switch would be a helpful addition to this post.
Barry Stevensson replied on : 4 of 19
Hi steve, I am currently working on image processing of neutron diffraction patterns (bmp files). The files contain hexagonally-arranged six spots with intensity is highest in the center of each spot. I would like to do elliptical fit for each of these spots and then find its center of mass. Then, i want to do also elliptical fit connecting all center of mass' of these six spots. Intensity of each spots is also of interest. Can you give me a hint how to do this? Thank you very much, Barry
Andy Kraushaar replied on : 5 of 19
Nice Example. Do you know a method how to find out if a given point x,y is within the area of an ellipses or outside?
Steve replied on : 7 of 19
Barry—The regionprops function can optionally take the original grayscale image and then compute grayscale-based measurements, including center of mass. However, the ellipse fit measurements in regionprops are for shapes only, not grayscale-weighted shapes. I'm not sure how to do that.
Hazem replied on : 9 of 19
Hi Steve, I am working on project for gait recognition, where i model the segmented human body binary image, into 6 segments to extract joint angles, i am using ellipse fitting to extract the orientation of each segments, the problem i have is that the fitted ellipse are not tight to the segments and include alot of non body area arround the segments (semi circular ellipses), how can i make the ellipses to fit inside the body segment.
Steve replied on : 10 of 19
Hazem—Ellipse-fitting in the toolbox is based on matching 2nd-order moments. If you fit an ellipse to a shape that is not an ellipse, then you're going to have object pixels that aren't in the fitted ellipse and fitted ellipse pixels that aren't in the object. If you goal is to extract orientation, why is this a problem? If the objects aren't very ellipse-like, then is orientation very meaningful?
Sri devi replied on : 11 of 19
Hello Steve, I'm new to MATLAB and I'm doing my project for detecting the plaque in carotid artery.Here we have to find the radius of the carotid wall.I found on the net that if we find the centroid we can calculate the radius from it.Can you help me how to find the centroid and radius of the carotid wall using regionprops......
Joe replied on : 12 of 19
Hi Steve, first of all I have to say I love your explanations and guides. They are very usefull, this post too! I've tried this and I am able to visualize the ellipsoids. But when I was testing it on many examples, after time I asked my self on which algorithm the ellipsoids are generated. I found out, that it's based on the central 2nd moment and I was continuing searching in the Internet how this algorithm works. I found an interesting explanation, how the result should have to look at the end (after using the algorithm). The Major Axis: Being on the Centroid of the Object you look for the farest distance to the border of object. This is the first Point of the MajorAxis and the Reflection (viewing from Centroid) is the 2nd Point. The minor axis also have to go through the centroid, but have to cut the MajorAxis with 90 degrees. You also have to look for the farest distance to the border and then reflect this point. I tested it on my example and found out, that the orientation of the ellipsoid is quite right, but the longueurs of the moments don't agree with my expectation. Can you explain me, how the exactly algorithm to generate the ellipsods work. Or can you tell me, why the ellisoids doesn't look like expected? Many thanks!
Steve replied on : 13 of 19
Joe—Sounds like you found a description for some other method, not a description for regionprops. regionprops calculates the 2nd-order moments of the object in question and then returns measurements of the ellipse with the same 2nd-order moments.
Joe replied on : 14 of 19
Hey Steve, I Searched in the internet, how this moment gets calculated and found out, that you have to calculate the covariance of the object. I tried this out with the following code: props=regionprops(imread('bitmap.bmp')<100,'all'); [V D]=eig(cov(props.PixelList)); In D should stand the MajorAxisLength and MinorAxisLength (the diagonale) and in V the orientation (you have to calculate the tangens of V). But the result doesn't agree with the parameters in regionprops :/ I don't get it.
Joe replied on : 15 of 19
Here is the link, from where I got the mathematics. For images I used the 2nd order central moment. µ00 - µ22
Steve replied on : 16 of 19
Joe—See the subfunction ComputeEllipseParams inside regionprops.m to find out exactly what regionprops is doing. The equations used are from Haralick and Shapiro, Computer and Robot Vision vol. 1, Appendix A, Addison-Wesley 1992. I did a sanity check by constructing an image containing an ellipse with major axis length = 100 and minor axis length = 50, and regionprops returned the correct measurements.
Tanya replied on : 17 of 19
Steve, Thanks for the code that was helpful. Is there any chance you have the code to plot the major and minor axis of the ellipse? I've tried to extract it from the code, but I've not gotten it to work. Tanya
Steve replied on : 18 of 19
Tanya—Given the centroid, the major and minor axis lengths, and the orientation, you can use trig functions cos and sin to calculate the end-points of the axes in order to plot them. I'll do another blog post soon to illustrate.
AD replied on : 19 of 19
Hello Steve, I love your code. It was really useful for my work. I need a further help. How is it possible to remove the plotting part and to actually save each ellipse in the bw image. I am running a long code and I do not want to plot x,y during the running, moreover I need something that looks like your image with the fitting red ellipses to go ahead with my code and to other stuff. Actually x and y are axes coordinate. What I would need is to convert them in a way that I can add the red pixel value into my bw image that is actually a 1024x1392 matrix. I hope it is clear. Could you help me?