Asked by Xen
on 11 Oct 2015

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.

Answer by David Young
on 12 Oct 2015

Edited by David Young
on 12 Oct 2015

Accepted Answer

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.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Image Analyst (view profile)

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/248035-how-to-find-the-true-perimeter-of-objects-in-a-binary-image#comment_315658

## Xen (view profile)

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/248035-how-to-find-the-true-perimeter-of-objects-in-a-binary-image#comment_315730

## Image Analyst (view profile)

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/248035-how-to-find-the-true-perimeter-of-objects-in-a-binary-image#comment_315736

## Xen (view profile)

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/248035-how-to-find-the-true-perimeter-of-objects-in-a-binary-image#comment_315922

Sign in to comment.