Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 二值图像中物体长度的自动求取(MATLAB)_Image_Matlab_Image Processing_Binary - Fatal编程技术网

Image 二值图像中物体长度的自动求取(MATLAB)

Image 二值图像中物体长度的自动求取(MATLAB),image,matlab,image-processing,binary,Image,Matlab,Image Processing,Binary,我有以下二进制图像: 我使用imtool中的标尺手动选择起点和终点以获得长度。是否有一种方法可以自动获取长度,即从第一个白色像素到最后一个白色像素(最长长度),而无需手动执行。代码 %%// 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(

我有以下二进制图像:

我使用imtool中的标尺手动选择起点和终点以获得长度。是否有一种方法可以自动获取长度,即从第一个白色像素到最后一个白色像素(最长长度),而无需手动执行。

代码

%%// 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

我得到的长度是307199,实际上大约是200个像素,确保里面只有一个斑点。用于查找最大Blob函数pdist2不在我的目录中!无论如何,我会查一查的,非常感谢@user3318709检查一些编辑以使用自定义函数,以防您没有
pdist2