Clear Filters
Clear Filters

How to fit an ellipse to a blob in binary image?

12 views (last 30 days)
Hi! I am trying to fit an ellipse to images, in which my object is partially masked. For example. a picture below, is a binarized image, you can see that the objects in front are partially masking the ellipse in the center. How can I fit an ellipse to such a partially masked blob images?
Thank you!
  3 Comments
NeuronDB
NeuronDB on 6 Apr 2021
Thank youf or the link! Unfortunately, it doesnt work :( the results look like this, where the elipse is fit to all the blobs.
Here is what I would like to happen instead, fitting an ellipse to the blob in the middle (indicated with green) below -
Best wishes!
Adam Danz
Adam Danz on 6 Apr 2021
It's not that the solution doesn't work. My guess is that it's not being implementing correctly.
The solution uses regionprops to get the major and minor axis lengths and the orientation. That's what I would use, too and it's all you need to define the elipses. But first you need to isolate the blob because the noise in the upper right may also be considered in the major/minor axis estimates.

Sign in to comment.

Accepted Answer

DGM
DGM on 6 Apr 2021
Keep in mind that most tools for morphological operations assume that the foreground (i.e. the "objects" ) are white. In order to work on the blob, you'll need to pick a relevant threshold and invert the image. You could've used imbinarize(), but I don't have it in R2015b. This works too.
% Read image
I = imread('blobs.png');
Igray = rgb2gray(I);
% i kept the green content. adjust threshold otherwise
% foreground is typically considered to be the white areas
% so this also inverts the image
BW = Igray<0.9*255;
% Extract the maximum area
BW = imclearborder(BW); % get rid of objects connected to the image boundary
BW = bwareafilt(BW,1); % pick the single largest object
This gives the best fit ellipse:
Which looks about right. Bear in mind, it's a best fit to an irregular region. The holes in the object mean it's not going to follow the original contour, if that's what you'd hoped. If so, you could loosen it up with this once the object is isolated:
BW = bwconvhull(BW); % after using bwareafilt
  4 Comments
DGM
DGM on 7 Apr 2021
If it resolves your question, please accept the answer so the question gets moved into the right list.

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!