Question

I have the following binary image: enter image description here

http://www.4shared.com/download/BozvHQcHba/untitled2.jpg?lgfp=3000

I manually select the start and end points using the ruler in imtool to get the length. Is there a way to automatically get the length i.e the first white pixel to last white pixel (longest length) without doing it manually.

Was it helpful?

Solution

Code

%%// Get the binary image
img = imread(filename1);
BW = im2bw(img);

%%// Find biggest blob
[L,num] = bwlabel( BW );
count_pixels_per_obj = sum(bsxfun(@eq,L(:),1:num));
[~,ind] = max(count_pixels_per_obj);
biggest_blob = (L==ind);

%%// Find row and column info for all edge pixels
BW1 = edge(biggest_blob,'canny');
[row1,col1] = find(BW1);

%%// Get the distance matrix and thus find the largest length separating
%%// them which is the length of the object/blob

%dist_mat = pdist2([row1 col1],[row1 col1]);
dist_mat = dist2s([row1 col1],[row1 col1]); %// If you do not have pdist2

length_blob = max(dist_mat(:))

Associated function

function out = dist2s(pt1,pt2)

out = NaN(size(pt1,1),size(pt2,1));
for m = 1:size(pt1,1)
    for n = 1:size(pt2,1)
        if(m~=n)
            out(m,n) = sqrt( (pt1(m,1)-pt2(n,1)).^2 + (pt1(m,2)-pt2(n,2)).^2 );
        end
    end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top