How to find the "true" perimeter of objects in a binary image?

조회 수: 5 (최근 30일)
Xen
Xen 2015년 10월 11일
댓글: Xen 2015년 10월 13일
Say, there is an object consisting of 3 pixels next to each other (■■■). If I calculate the perimeter using
sum(sum(bwperim(image)))
I get 3. But the actual perimeter (considering all sides of the object) is 8. How do I find this? Thanks.
  댓글 수: 4
Image Analyst
Image Analyst 2015년 10월 12일
That's too bad. Maybe some day you'll realize and understand the other ways of considering the pixels.
Optically speaking, the pixel center-to-pixel center definition is probably the most accurate, so an image of [0,1,1,1,0] would have a length of 2 rather than 3.
Xen
Xen 2015년 10월 13일
Thanks for the info (I guess?). Though I was expecting an explanation as to how the length of an object (which could have an abstract shape) relates to the perimeter of it, which is what I was asking in the first place, and maybe how that could give me the number 8 for that particular example.

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

채택된 답변

David Young
David Young 2015년 10월 12일
편집: David Young 2015년 10월 12일
One possible way is like this:
% Example binary image with 3 non-zero pixels
img = false(5); % Use zeros(5) if you prefer
img(3, 2:4) = true; % use 1 instead of true if you prefer
% Get complement of image, with a border round it in case the
% blob is at the boundary
notimg = true(size(img)+2);
notimg(2:end-1, 2:end-1) = ~img;
% Find locations where a non-zero pixel is adjacent to a zero pixel,
% for each cardinal direction in turn
topedges = img & notimg(1:end-2, 2:end-1);
leftedges = img & notimg(2:end-1, 1:end-2);
bottomedges = img & notimg(3:end, 2:end-1);
rightedges = img & notimg(2:end-1, 3:end);
% Sum each set of locations separately, then add to get total perimeter
perim = sum(topedges(:)) + sum(leftedges(:)) + ...
+ sum(bottomedges(:)) + sum(rightedges(:));
% print result
fprintf('Perimeter is %d\n', perim);
This is correct for your example. Before using it you should check it by testing on a wide range of examples. If you find a case for which it is incorrect, please describe it.
  댓글 수: 1
Xen
Xen 2015년 10월 12일
편집: Xen 2015년 10월 12일
Many thanks David. This works perfectly for a number of examples I've tried. Of course, if the object is unfilled (with a hole) it includes the "internal perimeter", but that's an easy fix. Thanks again.

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

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by