In a recently posted question @StackOverflow a user asked how to find the maximum Euclidean distance in a binary image produced by Canny edge detection using MATLAB. The original post is here. I report my response here just for archival purposes.

Provided that you have a binary edge image, you need to think of white pixels as points in a 2D space and look for the coordinates that correspond to the largest distance.
MATLAB can easily do this using the pdist2 function with some extra parameters.
Here is a piece of code to do the trick:

First you need to know where those white pixels are, so you initiate with a find function to get the coordinates. Then you use pdist2, which (in this case) returns the largest distances in a row matrix d and the corresponding indices in the row matrix i. Then you just need to check where is the maximum value in the largest distances to get the indices of the points with the maximum distance (these points are in a cell matrix of points p). This code captures all the possible maximum distance pixels, which could be more than a pair (ie. there could be pixel pairs with the same maximum distance). Of course, one may change the ‘euclidean’ parameters in the pdist2 function to get distances based on other metrics.

In order to keep only one copy of each line produced by the code the following code should be included:

pp=[];
for i=1:numel(p)
for j=i+1:numel(p)
if prod( double( [p{i}(3:4),p{i}(1:2)] == p{j}))
pp(end+1)=j;
end
end
end
j=1;
ppp={};
for i=1:numel(p)-numel(pp)
if j<=numel(pp) && i~=pp(j)
ppp{end+1}=p{i};
j=j+1;
end
end
p = ppp;

And if there is a need to display the result, another piece of code is needed as follows:

figure;
imshow( img);
hold on;
for i=1:numel(p)
line( [ p{i}(1), p{i}(3)], [p{i}(2), p{i}(4)]);
hdl = get(gca,'Children');
set( hdl(1),'LineWidth',2);
set( hdl(1),'color',[1 0 0]);
end
hold off;

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here:
Cookie Policy