我有以下二进制图片:
http://www.4shared.com/download/BozvHQcHba/untitled2.jpg?lgfp=3000
我使用imtool中的标尺手动选择起点和终点来获取长度。有没有办法自动获取长度,即第一个白色像素为最后一个白色像素(最长长度),而无需手动操作。
答案 0 :(得分:1)
<强>代码强>
%%// 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(:))
相关功能
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