How do I write a program using MATLAB to find the circumference of a area (white) using the unit as a pixcel (i ever Used to command g = regionprops (label, 'Area');)

2 次查看(过去 30 天)
1234.jpg

采纳的回答

KSSV
KSSV 2018-11-17
编辑:KSSV 2018-11-17
I = imread(image) ;
I = rgb2gray(I) ; % If gray alredy not needed
[y,x] = find(I) ;
idx = boundary(x,y) ;
imshow(I)
hold on
plot(x(idx),y(idx))
dx = diff(idx) ; dy = diff(y(idx)) ;
d = sqrt(dx.^2+dy.^2) ;
sum(d)
  8 个评论
Bruno Luong
Bruno Luong 2018-11-17
编辑:Bruno Luong 2018-11-17
Sorry but this method proposed by KSSV won't give the correct result for non-convex shape, since boundary will not follow closely the concave part (unpredictable sideeffect of alphashape).
As showed in the test script where the shape is 2 circles that are touching.
true circ = 2092.3
boundary method = 1790.75
Code:
x = linspace(-3,3,1000);
y = x(:);
Img = ((x-1).^2 + y.^2 < 1) | ((x+1).^2 + y.^2 < 1);
close all
imagesc(Img)
colormap gray
axis equal
truec = (length(x)-1)*(2/6*2*pi);
[y,x] = find(Img) ;
idx = boundary(x,y) ;
dx = diff(x(idx)) ; dy = diff(y(idx)) ;
d = sqrt(dx.^2+dy.^2) ;
c2 = sum(d);
hold on
plot(x(idx),y(idx),'r','linewidth',3)
fprintf('true circ = %g\n', truec)
fprintf('boundary method = %g\n', c2)

请先登录,再进行评论。

更多回答(1 个)

Bruno Luong
Bruno Luong 2018-11-17
编辑:Bruno Luong 2018-11-17
Assuming your binary is Img with one connexed component
c = contourc(double(Img),[1 1]);
c = c(:,1+(1:c(2,1)));
d = diff(c(:,[1:end 1]),1,2);
crcumference = sum(sqrt(sum(d.^2,1)))
You must be aware the boundary might have a fractal property, therefore the circumference is something difficult to defined and computed accurately.
circle.png

Community Treasure Hunt

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

Start Hunting!

Translated by