How to fit an ellipse to a blob in binary image?
6 次查看(过去 30 天)
显示 更早的评论
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!![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/574197/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/574197/image.png)
3 个评论
Adam Danz
2021-4-6
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.
采纳的回答
DGM
2021-4-6
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/574837/image.png)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/574842/image.png)
4 个评论
DGM
2021-4-7
If it resolves your question, please accept the answer so the question gets moved into the right list.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!