How to fit an ellipse to an image in matlab.

조회 수: 102 (최근 30일)
Hardit Singh
Hardit Singh 2019년 12월 9일
댓글: Connor 2023년 11월 11일
diskimage1.jpgI have a image like this and I want to fit an ellipse around this disk image and get the info about the ellipse like its axis, center point, etc. I am unsure how to use the image proccessing toolbox to draw an ellipse to it and get info about the ellipse.

채택된 답변

Akira Agata
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
  댓글 수: 11
Image Analyst
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);
Connor
Connor 2023년 11월 11일
Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2019년 12월 11일

Matt J
Matt J 2023년 11월 10일
편집: Matt J 2023년 11월 10일
You can use ellipticalFit from this FEX download
processImage('Connors_image.png')
ans =
ellipticalFit with properties: center: [196.4747 218.0069] a: 130.0274 b: 119.1008 angle: 3.0398
processImage('Hardits_image.png')
ans =
ellipticalFit with properties: center: [171.3735 173.6428] a: 112.7502 b: 104.1636 angle: 47.0771
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 CenterFile Exchange에서 Biomedical Imaging에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by