 # Is fitted ellipse in analyze->set measurements circumscribed or inscribed?

I need to know whether fitted ellipse in analyze->set measurements is circumscribed or inscribed.

1 Like

Neither, it finds minor/major axis directions and radii. Details here:

Consider some arbitrarily shaped closed profile, which we wish to
characterize in a quantitative manner by a series of terms, each
term providing a better approximation to the shape of the profile.
Assume also that we wish to include the orientation of the profile
(i.e. which way is up) in our characterization.
One approach is to view the profile as formed by a series harmonic
components, much in the same way that one can decompose a waveform
over a fixed interval into a series of Fourier harmonics over that
interval. From this perspective the first term is the mean radius,
or some related value (i.e. the area). The second term is the
magnitude and phase of the first harmonic, which is equivalent to the
best-fitting ellipse.
What constitutes the best-fitting ellipse? First, it should have the
same area. In statistics, the measure that attempts to characterize some
two-dimensional distribution of data points is the ‘ellipse of
concentration’ (see Cramer, Mathematical Methods of Statistics,
Princeton Univ. Press, 945, page 283). This measure equates the second
order central moments of the ellipse to those of the distribution,
and thereby effectively defines both the shape and size of the ellipse.
This technique can be applied to a profile by assuming that it constitutes
a uniform distribution of points bounded by the perimeter of the profile.
For most ‘blob-like’ shapes the area of the ellipse is close to that
of the profile, differing by no more than about 4%. We can then make
a small adjustment to the size of the ellipse, so as to give it the
same area as that of the profile. This is what is done here, and
therefore this is what we mean by ‘best-fitting’.
For a real pathologic case, consider a dumbell shape formed by two small
circles separated by a thin line. Changing the distance between the
circles alters the second order moments, and thus the size of the ellipse
of concentration, without altering the area of the profile.

John

1 Like

@bogovicj Does not make sense. You mentioned it finds the major/minor axis and radii of ellipse. so which ellipse? The ellipse should have relation with the object. It should either be circumscribed or inscribed.

There are other ways to choose an ellipse to fit a shape beside circumscribed and inscribed.
The method in Fiji tries to get the area of the shape correct. Circumscribed will over estimate area,
inscribed will underestimate area.

You can see that the result is neither circumscribed nor inscribed:

Does that image help?
John

2 Likes

@bogovicj Yes. It helped to understand what you mean.
I appreciate if you could give me your feedback regarding this question: How can I circumscribe an ellipse to an image by imagej macro?

Here is a macro that can be usefull for you

PS: I am not the author of this macro.
I can’t find the origin.
My apologies to the author.

``````  run("Blobs (25K)");
setAutoThreshold;
run("Set Measurements...", " area mean centroid fit ");
run("Analyze Particles...", "size=0 circularity=0 show=Ellipses display exclude clear ");
run("RGB Color");
for(i=0; i<nResults; i++) {
x=getResult('X',i);
y=getResult('Y',i);
d=getResult('Major',i);
a = getResult('Angle',i)*PI/180;
setColor("blue");
drawLine(x+(d/2)*cos(a),y-(d/2)*sin(a),x-(d/2)*cos(a),y+(d/2)*sin(a));
d=getResult('Minor',i);
a=a+PI/2;
setColor("red");
drawLine(x+(d/2)*cos(a),y-(d/2)*sin(a),x-(d/2)*cos(a),y+(d/2)*sin(a));
}
``````
1 Like

This is the best I could get. It’s a circle, but it includes all the target with a minimal surround.

//Circumscribed Ellipse
//ron_despain@hotmail.com
oit = getTitle();
run(“Set Measurements…”, “area mean standard modal min centroid center perimeter bounding fit shape feret’s integrated median skewness kurtosis area_fraction stack redirect=None decimal=3”);

//run("Rotate… ", “angle=164.44 grid=1 interpolation=Bicubic”);
setOption(“BlackBackground”, true);
setAutoThreshold(“Otsu”);
run(“Make Binary”);
run(“Invert”);
run(“Analyze Particles…”, “display exclude clear include”);
run(“Select None”);
fangle = getResult(“Angle”, 0);
run("Rotate… ", “angle=&fangle grid=1 interpolation=Bicubic”);

setOption(“BlackBackground”, true);
setAutoThreshold(“Otsu”);
run(“Make Binary”);
run(“Invert”);
run(“Analyze Particles…”, “display exclude clear include add”);
roiManager(“select”,0);
roiManager(“rename”,“Target i0”);
roiManager(“Show None”);
roiManager(“Rename”, “Fit”);
//run(“Select None”);

makeFeretsDiameter();

function makeFeretsDiameter()
{
requires(“1.29n”);
run(“Line Width…”, “line=1”);
diameter = 0.0;
getSelectionCoordinates(xCoordinates, yCoordinates);
n = xCoordinates.length;
dmax = 0;
for (i=0; i<n; i++) {
for (j=i; j<n; j++) {
dx = xCoordinates[i] - xCoordinates[j];
dy = yCoordinates[i] - yCoordinates[j];
d = sqrt(dxdx + dydy);
if (d>diameter) {
diameter = d;
if (d >= dmax) dmax = d;
i1 = i;
i2 = j;
}
}
}
makeLine(xCoordinates[i1], yCoordinates[i1],xCoordinates[i2],yCoordinates[i2],dmax);
roiManager(“select”,1);
roiManager(“rename”,“Feret Line i1”);
run(“Rotate…”, " angle=90");
roiManager(“Select”, 2);
roiManager(“Rename”, “Rotated i2”);
roiManager(“Select”, newArray(1,2));
roiManager(“Combine”);
roiManager(“Select”, 3);
roiManager(“Rename”, “Xboxes i3”);
run(“Enlarge…”, 1)
selectWindow(oit);
run(“Fit Circle”);
roiManager(“Select”, 4);
roiManager(“Rename”, “Circle i4”);
roiManager(“show none”);

`````` axminor = getResult("Height", 0);
axmajor = getResult("Width", 0);
ar = axminor/axmajor;

makeEllipse(xCoordinates[i1], yCoordinates[i1],xCoordinates[i2],yCoordinates[i2],1);
roiManager("Select", 5);
roiManager("Rename", "Ellipse i5");
run("Select None");

rfangle = -fangle;

run("Rotate... ", "angle=&rfangle grid=1 interpolation=Bicubic");
roiManager("Select", 5);
run("Rotate...", "rotate angle=&rfangle");
roiManager("Update");
roiManager("show none");
roiManager("Select", newArray(0,1,2,3,4));
roiManager("Delete");
roiManager("Select", 0);
roiManager("Rename", "Fit");
``````

}
close(“Results”);   Ron DeSpain

p.s. It looks like the plugin at this link provides inscribed circles.
https://imagejdocu.tudor.lu/doku.php?id=plugin:analysis:geodesic_distances:start

1 Like

@Mathew Thanks for sharing I tried that but did not work for my images.

@rondespain Thanks so much for your time. I need to be ellipse. In fact circle is a special case for ellipse but I need to fit ellipse and then find the ratio of larger diameter to smaller one.

**and scroll down to **

## 30.7 Set Measurements…

Fit ellipse Fits an ellipse to the selection. Uses the headings Major, Minor and Angle. Major and Minor are the primary and secondary axis of the best fitting ellipse. Angle is the angle between the primary axis and a line parallel to the X-axis of the image. The coordinates of the center of the ellipse are displayed as X and Y if Centroid is checked. Note that ImageJ cannot calculate the major and minor axis lengths if Pixel Aspect Ratio in the Analyze▷Set Scale…↓ dialog is not 1.0. There are several ways to view the fitted ellipse:

I checked to see if the eccentricity changes when an ellipse is scaled in ImageJ

The center ellipse below is the Selection>Fit Ellipse. The next one out is a scaled and translated version of that ellipse using Selection>Scale…and manual dragging. They both have the same ratio of major to minor axes’ (eccentricity) to the third decimal place. So if all you need is the eccentricity you can use the built in elliptical fit in Analyze Particles, since the enclosing ellipse has approximately the same eccentricity.  I have an idea for fitting an ellipse directly to an arbitrary shape. If I’m successful, I’ll share.

Ron DeSpain

1 Like

I think you will have to describe your data and your process of analysis as there an infinite number of ellipses that circumscribe the data.

e.g.

Can you describe how you would pick between the three solutions above? What about your data would allow that choice to be made in code?

Ron

3 Likes

If it were me, I’d aim for the smallest circumscribing ellipse - since that will have an area closest to the shape, but it’s not clear to me what the best approach would be to find it.

1 Like

bogovicj

I was asking Zeynab_Mousavi what her constraints would be for the ellipse in order to pick one of the many (possibly infinite) ellipses that can circumscribe any shape. Her data should have some features specific to it (concave, convex, tendrils, holes…etc.), and the solution sought (why is the ecentricity important?) that would guide an algorithm in picking which ellipse is best, or even if an ellipse is needed (maybe a convex hull, or bounding box…etc.). For instance…should the major axis lie along or parallel to the Feret line, or should it be a scaled and translated version of the built in Selection>Fit Ellipse maintaining the ecentricity, or as you advise the smallest in area of all the ellipses that can be circumscribed (I looked online and this option takes a lot of time e.g. https://www.researchgate.net/post/Can_anyone_suggest_a_fast_method_of_estimating_an_ellipse_with_constraints The Elliptical Hough transform is proposed here)? Her goal and the data should advise the constraints to be used.

It looks to me like the we need to define the Zeynab_Mousavi problem better, but it’s a great general problem for investigation.

I don’t see a plugin for the Elliptical Hough transform…do you know of one bogovicj? Please share if you do.

Ron

2 Likes

Hi Ron (@rondespain),

Good idea, thanks for pointing that out. The closest thing I’m aware of is this Hough circle transform implementation. So not quite what’s needed here.

Agreed!

John

1 Like

you want use the small ellipse’s centried, and scale the small ellipse as a same ration in X and Y to contain the mask fully, right? It is not a image question: It is a computation geometry question:

1. treat the contours as a polygon
2. find convex hull, and extract every convex point
3. count the ratio every convex point need to contain
4. scale using the max ratio
1 Like

function roiadd(name) //Helps organize the roi’s
{
roiindex = roiManager(“count”) -1;
roiManager(“select”, roiindex);
roiManager(“rename”, name+" - “+roiindex);
}
//setBatchMode(true);
//close(”*");
//open(“C:/Users/rdespain/Desktop/m51.tif”);
//Detect the target
setAutoThreshold(“IsoData dark”);
setOption(“BlackBackground”, true);
run(“Set Measurements…”, “area mean standard modal min centroid center perimeter bounding fit shape feret’s integrated median skewness kurtosis area_fraction stack redirect=None decimal=2”);
run(“Analyze Particles…”, " display exclude clear include add");
roiManager(“Select”, 0);
roiManager(“Rename”, “Orig. Target - 0”);

//Get Salient variables
fa = getResult(“FeretAngle”,0);
cx = getResult(“X”,0);
cy = getResult(“Y”,0);
ta = getResult(“Area”,0);
bx = getResult(“BX”,0);
by = getResult(“BY”,0);
bw = getResult(“Width”,0);
bh = getResult(“Height”,0);
bcx = bx + bw/2;
bcy = by + bh/2;
run(“Select All”);
getSelectionBounds(ix,iy,iw,ih);
icx = iw/2;
icy = ih/2;
dx = (icx - bcx);
dy = (icy - bcy);

//Translate and rotate the image to align x with the Feret max caliper line
run(“Translate…”, “x=&dx y=&dy interpolation=None”);
roiManager(“Show None”);
run(“Select None”);
run("Rotate… ", “angle=&fa grid=0 interpolation=Bicubic enlarge”);

//re-acquire the image and get it’s bounding box
doWand(icx, icy);
roiManager(“Select”, roiManager(“count”)-1);
getRawStatistics(targetarea);
Roi.getBounds(x, y, width, height);
makeRectangle(x, y, width, height);

//Fit the ellipse to the bounding box
roiManager(“Select”, 2);
run(“Fit Ellipse”);

//waitForUser(“Prior to expand”);

//Expand ellipse to contain the entire target
aa = 0;
while(targetarea > aa) {
roiManager(“Select”, newArray(1,3));
roiManager(“AND”);
run(“Clear Results”);
run(“Measure”);
aa = getResult(“Area”,0);
//print("Target Area = “+targetarea+” Anded Area = “+aa+” ta-aa = "+ targetarea-aa);
roiManager(“Select”, 3);
run(“Enlarge…”, “enlarge=1”);
roiManager(“Select”, roiManager(“count”)-2);
roiManager(“Delete”);
}
roiManager(“Select”, roiManager(“count”)-1);
roiManager(“Rename”,“Fit - 3”);
close(“ROI Manager”);
close(“Results”);
//End the program
//setBatchMode(false);
exit();

//Left for the student…the following should rotate and translage the image and ellipse back  1 Like

@rondespain I would say the ellipse that has least difference with the boundaries of the image. If xi and yi are the x and y of all the points of boundary, the best ellipse would be the one that has: min(sigmasqrt((xi-xellipse)^2+(yi-yellipse)^2))) sth like this formula.

@bogovicj
what is Elliptical Hough transform?
I read here : "image transform that allows for circular objects to be extracted from an image, even if the circle is incomplete. " is not clear. What is the main goal of hough transform?