【问题标题】:Calculate the shortest distance of each 1-pixel to any zero-pixel in Matlab在Matlab中计算每个1像素到任何零像素的最短距离
【发布时间】:2017-02-12 00:22:41
【问题描述】:

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

说'Im'是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

非常感谢您提前提供的任何帮助!

【问题讨论】:

  • bwdist(1-Im) 怎么样?
  • 它使用 bwdist(~Im) 工作。但我正在玩,看看上面的代码是否可以以同样的方式工作。

标签: matlab matrix transform distance pixels


【解决方案1】:

有一个内置的 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

【讨论】:

  • 不错的发现!但是我仍然想知道我是否按照我在问题中给出的基本想法来做到这一点,如何在没有 bwdist 的情况下使其工作?欣赏。
【解决方案2】:

我自己想出来的。这是我的代码:

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 替换为最小值(如果有)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 2018-07-04
    • 1970-01-01
    • 2014-11-16
    • 2020-07-29
    • 2011-07-23
    • 1970-01-01
    相关资源
    最近更新 更多