Reputation: 75
I have the following binary image:
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.
Upvotes: 2
Views: 3061
Reputation: 221514
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
Upvotes: 2