MATLAB Answers

Obtain Number of Pixels in Area Around Binary Image Object

19 ビュー (過去 30 日間)
Andrew Poissant
Andrew Poissant 2018 年 7 月 5 日
編集済み: Anton Semechko 2018 年 7 月 6 日
I have a binary image and use matlab's 'regionprops' function to fit ellipses to the image (see attached photo). I want to obtain the total number of pixels of the black area within each ellipse. I use the 'Area' property of regionprops to get the total number of pixels of the white area within the fitted ellipses, but I want the total number of pixels of the black area within each fitted ellipse. How would I get this?

  0 件のコメント

Sign in to comment.

採用された回答

Anton Semechko
Anton Semechko 2018 年 7 月 5 日
To distinguish between points/pixels inside the ellipse vs. those outside, you need to know parameters of the ellipse. Here is an example:
% Ellipse parameters
ab=sort(3*rand(1,2)+1,'descend'); % lengths of the principal semi-axes
r=pi*rand(1); % orientation of the ellipse; relative to the x-axis
R=[cos(r) -sin(r);sin(r) cos(r)]; % directions of the principal axes (along columns)
C=10*randn(2,1); % center of the ellipse
% Visualize ellipse
t=linspace(0,2*pi,1E3);
X=[cos(t);sin(t)];
Y=bsxfun(@plus,R*diag(ab)*X,C);
figure('color','w')
plot(Y(1,:),Y(2,:),'-b','LineWidth',2)
axis equal
hold on
% Generate a random set of points around the ellipse
N=1E3;
P=2*rand(2,N)-1;
P=bsxfun(@plus,2*R*diag(ab)*P,C); % random point cloud
% Classify points depending on whether they are inside (and on the boundary)
% or outside the ellipse; to do this we need to know parameters of the ellipse
dP=bsxfun(@minus,P,C); % center the points
dP=R'*dP; % change basis
dP=diag(1./ab)*dP; % normalize length of principal axes to 1
id_in=sum(dP.^2,1)<=1; % point P(:,i) is inside the ellipse if norm(dP(:,i))<1 and on its boundary if norm(dP(:,i))=1
% Visualize points
P_in=P(:,id_in);
P_out=P(:,~id_in);
plot(P_in(1,:),P_in(2,:),'.g','MarkerSize',10,'LineWidth',2)
plot(P_out(1,:),P_out(2,:),'xr','MarkerSize',5,'LineWidth',2)
xlabel('x','FontSize',25)
ylabel('y','FontSize',25)

  4 件のコメント

表示 1 件の古いコメント
Anton Semechko
Anton Semechko 2018 年 7 月 6 日
Binary image is composed on pixels --> pixels have (x,y) coordinates
Let's say bw is your binary image. To get (x,y) coordinates of all black pixels, use command:
[y,x]=find(~bw);
Andrew Poissant
Andrew Poissant 2018 年 7 月 6 日
Ah yes, great point. Thanks for the help! Very easy to understand.
Anton Semechko
Anton Semechko 2018 年 7 月 6 日
You are welcome, Andrew. Let me know if run into any problems when classifying pixels as inside or outside ellipse boundary.

Sign in to comment.

その他の回答 (0 件)


Translated by