在Matlab中计算每个1像素到任何零像素的最短距离

时间:2017-02-12 00:22:41

标签: matlab matrix transform distance pixels

我正在计算从每个前景像素到背景像素的最短距离。我尝试了一些选项,但没有按照我的意图工作(有一个内置的Matlab函数'bwdist'给出了该像素与最近的非零像素之间的距离。但我正在创建自己的一个以给出1之间的距离像素和最接近的零像素。)这是我的版本之一。

说'我'是10x10像素的原始矩阵(它是随机创建的。实际矩阵比这大得多。)

Im =
 0     0     0     0     0     0     0     0     0
 0     0     0     1     1     1     1     0     0
 0     0     0     1     1     1     1     1     0
 0     0     1     0     1     1     1     1     1
 0     0     1     1     1     1     1     1     1
 0     0     0     1     1     1     1     1     0
 0     0     0     0     1     1     1     1     0
 0     0     0     0     1     1     1     1     0
 0     0     0     0     0     1     1     0     0
 0     0     0     0     0     0     0     0     0

DT = Im;%create a copy matrix of Im
for i = 1: size(DT,1)
    for j = 1: size(DT,2)
        %I want to select all pixels with a distance of 1 to current pixel 
        %(i,j), e.g. (i-1,j), (i,j-1), (i+1,j),(i,j+1) would be the case for Euclidean
        %distance. The large size of matrix (say 512x512) also makes it very inefficient
        %use four for-loops to find these pixels with distance of 1 to current pixel. So
        %I use (i-1,j) etc instead of using 
        %sqrt(sum(bsxfun(@minus,[u v],[i j]).^2,2))
        %to find out all (u,v)s with distance of 1 to current pixel (i,j).
        %But I do believe there are thousands smart ways to make this work efficiently. 

        if (Im(i-1,j) == 0 || Im(i,j-1) == 0 || Im(i,j+1) == 0 || Im(i+1,j) == 0)%I want to mark all pixels with 0 to remain as 0
            DT(i-1,j) = Im(i-1,j);
            DT(i,j-1) = Im(i-1,j);
            DT(i,j+1) = Im(i,j+1);
            DT(i+1,j) = Im(i+1,j);

        else
        %I want to update the visited pixels with the minimum value 
        %of calculated distances. Apparently, here is my problem. The code is not correct.

            DT(i-1,j) = min(DT(i-1,j),Im(i-1,j) + DT(i,j));
            DT(i,j-1) = min(DT(i,j-1),Im(i,j-1) + DT(i,j));
            DT(i,j+1) = Im(i,j+1) + DT(i,j));
            DT(i+1,j) = Im(i+1,j) + DT(i,j);
        end      
    end
end

非常感谢您提前获得任何帮助!

2 个答案:

答案 0 :(得分:0)

  

有一个内置的Matlab函数bwdist可以给出距离   在该像素和最近的非零像素之间。但我正在创造我的   拥有一个给出1像素和最近零之间的距离   像素。

我认为你不需要创建自己的功能,因为你仍然可以使用bwdist来做你想做的事情。您可以使用logical NOT ~反转图片,然后使用bwdist,如以下示例所示:

% Binary image.
Im = [
    0 0 0
    0 1 0
    0 0 0];

% Distance transform of binary image.
D1 = bwdist(Im)

% Distance transform of inverted binary image.
D2 = bwdist(~Im)

输出:

D1 =
    1.4142    1.0000    1.4142
    1.0000         0    1.0000
    1.4142    1.0000    1.4142
D2 =
     0     0     0
     0     1     0
     0     0     0

答案 1 :(得分:0)

我自己想通了。这是我的代码:

DT = Im;%create a copy matrix of Im
for i = 1: size(DT,1)
    for j = 1: size(DT,2)
            %I want to select all pixels with a distance of 1 to current pixel 
        %(i,j), e.g. (i-1,j), (i,j-1), (i+1,j),(i,j+1) would be the case for Euclidean
        %distance. The large size of matrix (say 512x512) also makes it very inefficient
        %use four for-loops to find these pixels with distance of 1 to current pixel. So
        %I use (i-1,j) etc instead of using 
        %sqrt(sum(bsxfun(@minus,[u v],[i j]).^2,2))
        %to find out all (u,v)s with distance of 1 to current pixel (i,j).
        %But I do believe there are thousands smart ways to make this work efficiently. 

        if (Im(i-1,j) == 0 || Im(i,j-1) == 0 || Im(i,j+1) == 0 || Im(i+1,j) == 0)%I want to mark all pixels with 0 to remain as 0
            DT(i-1,j) = Im(i-1,j);
            DT(i,j-1) = Im(i-1,j);
            DT(i,j+1) = Im(i,j+1);
            DT(i+1,j) = Im(i+1,j);

        else
        %I want to update the visited pixels with the minimum value 
        %of calculated distances. Apparently, here is my problem. The code is not correct.

            DT(i-1,j) = min(DT(i-1,j),Im(i-1,j) + DT(i,j));
            DT(i,j-1) = min(DT(i,j-1),Im(i,j-1) + DT(i,j));
           DT(i,j+1) = min(DT(i,j+1),Im(i,j+1) + DT(i,j));
            DT(i+1,j) = Im(i+1,j) + DT(i,j);
        end      
    end
end

此外,如果存在,循环必须从下向上扫描以用最小值替换DT。