Bounding smallest tilted rectangle

Hi everybody,

I want to measure semi-axes of an inclined object by fitting the smallest bounding rectangle.
The bounding rectangle tool of ImageJ fits a horizontal (or vertical) rectangle which is not useful in this case.
I need to do this measurement for hundreds of images and I would really appreciate it if anybody let me know how I can do that.


Here is a macro that selects your structure using a best-fit ellipse:

id = getImageID();

run("Duplicate...", "tmp");

// crop out the scale bar
makeRectangle(0, 0, 455, 372);

// threshold and mask
setOption("BlackBackground", true);
run("Convert to Mask");

// preprocess: fill in the main structure
run("Fill Holes");

// filter out the tiny spots
run("Analyze Particles...", "size=100-Infinity add");

// ditch the temporary image

// select the structure in the original image
roiManager("Select", 0);

// fit the structure to an ellipse
run("Fit Ellipse");

I developed it using the Macro Recorder feature of ImageJ.

Note that the “Fit Ellipse” command requires Fiji.

Here is the result:

Once you have an ellipse ROI, you can extract the major and minor axis information… except that I don’t know how to do it from the ImageJ macro language. But it must be possible somehow! :smile:

1 Like

If you go to Analyze > Set Measurements, and check the box for “Fit Ellipse”:

run("Set Measurements...", "fit display redirect=None decimal=3");

Then you can get the major and minor axis at the “Analyze Particles” step:

run("Analyze Particles...", "size=100-Infinity display add");

1 Like

I tried your approach for some images and it works well when the object is close to ellipse.
I attached the result I obtained for a semi-rectangular object.

Anyway thank you very much ctrueden.

The point is that your approach does not give the real width and length of the object as it fits the closest ellipse to the object. you see it from the results I obtained by applying your approach in the case of a semi-rectangular object.

Moreover, I have dumbbell shape objects in some of my images.
I attached one in the below and I should measure the axes defined as red arrows on the image.

Anyway, thank you very much for your hint tswayne.

Another half-baked idea:

  • Start with the same process I did before

    • Threshold
    • Create a mask
    • Optionally skeletonize
    • Fill holes
  • Now you have a big solid shape corresponding to your object.

  • Run the Process :arrow_forward: Binary :arrow_forward: Distance Map command to generate an image where the values represent the closest distance to a black pixel from a white one. This produces e.g.:

    The value of the brightest line down the center of your structure represents the radius (half the distance) of the minor axis length there.

  • As for the major axis, you could use Process :arrow_forward: Binary :arrow_forward: Ultimate Points to produce e.g.:

    Then measure the distance between those two white dots, plus the Distance Map value of each dot’s coordinate from the other image. This gives you the major axis length.

There is probably a slicker way. But I think this scheme would work.

EDIT: A slicker way for the major axis is to use the Feret’s Diameter statistic. As @tswayne says, run Set Measurements… to enable it, then use Analyze Particles to measure it for your big shape. In my quick test it corresponded very closely to the major axis length.


Thanks Curtis very much.
I will try that.

The latest ImageJ daily build (1.51j10) adds a tilted rectangle selection tool, activated by right clicking on the rectangle tool icon. Use the Measure command after creating a tilted rectangle and its length and width are listed in the Results window under “TRLength” and “TRWidth”.


hello! were you ever able to resolve this problem? i have to do a similar analysis and am struggling to find the solution. thanks.

This problem is resolved with the “rotating calipers” algorithm.
I think Michael Doube (@mdoube) had written a plugin to that effect.


Feret Min and Feret Max from the rotating calipers algorithm are built-in to ImageJ’s ‘Measure’ command, which @Wayne and I worked on several years ago. Select Analyze > Set Measurements > Feret’s diameter to get Feret and MinFeret values in the Results table. The Feret’s diameters are not guaranteed to be orthogonal, as far as I can tell.

It looks possible to support a minimal bounding rectangle that forces Feret max and min to be orthogonal, if it can be extracted from / implemented in the code.

1 Like