![untitled.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/253935/untitled.png)
How to fit an ellipse to an image in matlab.
35 ビュー (過去 30 日間)
古いコメントを表示
![diskimage1.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/253610/diskimage1.jpeg)
0 件のコメント
採用された回答
Akira Agata
2019 年 12 月 11 日
How about the following?
% Read image
I = imread('diskimage1.jpeg');
% Binarize
Igray = rgb2gray(I);
BW = imbinarize(Igray);
% Extract the maximum area
BW = imclearborder(BW);
BW = bwareafilt(BW,1);
% Calculate centroid, orientation and major/minor axis length of the ellipse
s = regionprops(BW,{'Centroid','Orientation','MajorAxisLength','MinorAxisLength'});
% Calculate the ellipse line
theta = linspace(0,2*pi);
col = (s.MajorAxisLength/2)*cos(theta);
row = (s.MinorAxisLength/2)*sin(theta);
M = makehgtform('translate',[s.Centroid, 0],'zrotate',deg2rad(-1*s.Orientation));
D = M*[col;row;zeros(1,numel(row));ones(1,numel(row))];
% Visualize the result
figure
imshow(Igray)
hold on
plot(D(1,:),D(2,:),'r','LineWidth',2)
![untitled.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/253935/untitled.png)
11 件のコメント
Image Analyst
2023 年 11 月 10 日
@Connor that is not a blob. Well it is but it's a very snake-like, line-like blob. So the fit is accurate. But I suspect what you want is to fit the (x,y) coordinates of the blob to an ellipse. So you need to get x and y from the binary image like this:
[y, x] = find(binaryImage);
and then use the ellipse fit in the FAQ: https://matlab.fandom.com/wiki/FAQ#How_can_I_fit_an_ellipse_or_other_shape_to_a_set_of_XY_data?
その他の回答 (2 件)
Image Analyst
2019 年 12 月 11 日
See Steve's blog: Visualizing regionprops ellipse measurements
0 件のコメント
Matt J
2023 年 11 月 10 日
編集済み: Matt J
2023 年 11 月 10 日
You can use ellipticalFit from this FEX download
processImage('Connors_image.png')
processImage('Hardits_image.png')
function e=processImage(filename)
I = imread(filename);
% Binarize
I = bwareafilt(imclearborder(imbinarize(rgb2gray(I))),1);
b = bwboundaries(I);
e=ellipticalFit(flipud(b{1}'));
imshow(I,[]); hold on
showfit(e,'LineWidth',1,'Color','r','LineStyle','--'); hold off
end
参考
カテゴリ
Help Center および File Exchange で Signal Modeling についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!