Fit ellipse to objects in image

19 ビュー (過去 30 日間)
Sean Dobson
Sean Dobson 2022 年 1 月 7 日
コメント済み: Sean Dobson 2022 年 1 月 10 日
Hello, I am trying to fit ellipses to many different objects within a binary images. The ultimate goal is calculate the average eccentricity of the objects, but I can currently only plot a boundary around the objects. Any suggestions would be much appreciated. Image below (metallic sample with visible porosity).
clear
clc
close all
[f,p]=uigetfile('*.tif','select image file');
file_name=strcat(p,f)
BW = imbinarize(rgb2gray(imread(file_name)),0.68);
[B,L,N,A] = bwboundaries(BW);
figure; imshow(BW); hold on;
% Loop through object boundaries
for k = 1:N
% Boundary k is the parent of a hole if the k-th column
% of the adjacency matrix A contains a non-zero element
if (nnz(A(:,k)) > 0)
boundary = B{k};
% Loop through the children of boundary k
area1=polyarea(boundary(:,2), boundary(:,1));
for l = find(A(:,k))'
boundary = B{l};
plot(boundary(:,2),...
boundary(:,1),'g','LineWidth',2);
areaholes(l)=polyarea(boundary(:,2), boundary(:,1));
end
end
end

採用された回答

Kevin Holly
Kevin Holly 2022 年 1 月 7 日
I would suggest using regionprops. Steve Eddins walks through the process on his blog.
  2 件のコメント
Kevin Holly
Kevin Holly 2022 年 1 月 7 日
regionprops also can provide the eccentricity value.
Sean Dobson
Sean Dobson 2022 年 1 月 10 日
Thank you for your help!

サインインしてコメントする。

その他の回答 (2 件)

Matt J
Matt J 2022 年 1 月 7 日
編集済み: Matt J 2022 年 1 月 7 日
T=regionprops('table',~BW,'MajorAxisLength','MinorAxisLength','Eccentricity');
meanEccentricity=mean(T.Eccentricity);
Areas=pi*T.MajorAxis.Length*T.MinorAxisLength/4;
  1 件のコメント
Sean Dobson
Sean Dobson 2022 年 1 月 10 日
Thank you for your help!

サインインしてコメントする。


Matt J
Matt J 2022 年 1 月 7 日
編集済み: Matt J 2022 年 1 月 7 日
You cna use ellipticalFit(), distributed here:
BW0 = imbinarize(rgb2gray(imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/856545/image.jpeg')),0.68);
BW=bwareaopen(~BW0,15);
B=bwboundaries(BW,'noholes');
figure; imshow(BW0); hold on;
% Loop through object boundaries
N=numel(B);
for k = 1:N
efit=ellipticalFit(flipud(B{k}.'));
hold on
fimplicit(efit,'Color','r','LineWidth',3);
hold off
areaholes(k)=pi*efit.a*efit.b/4;
end
axis([0.0070 0.7170 0.7402 1.2728]*1000)

カテゴリ

Help Center および File ExchangeFeature Detection and Extraction についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by