【问题标题】:automatically finding length of object in binary image (MATLAB)自动查找二进制图像中对象的长度(MATLAB)
【发布时间】:2014-06-12 20:16:36
【问题描述】:

我有以下二进制图像:

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

我使用 imtool 中的标尺手动选择起点和终点以获取长度。有没有办法自动获取长度,即第一个白色像素到最后一个白色像素(最长长度),而无需手动进行。

【问题讨论】:

    标签: image matlab image-processing binary


    【解决方案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
    

    【讨论】:

    • 我得到的长度是 307199,而实际上它大约是 200 像素
    • 确保其中只有一个 blob。使用this 查找最大的blob
    • 我的目录中没有函数pdist2!
    • 无论如何我会检查一下,非常感谢!
    • @user3318709 检查一些编辑以使用自定义函数,以防您没有pdist2
    猜你喜欢
    • 2016-07-14
    • 2014-05-02
    • 2014-08-07
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多