3

我正在尝试创建一个 1 和 0 的 3d 矩阵。我想通过在它们之间形成一条 1 的线来将 2 个点连接在一起(最短距离)。

它看起来像这样,但在 3d 中

path_pixels = [0,0,1,0,0,0,0,0,0,0,0,0,0,0,0;

               0,0,0,1,0,0,0,0,0,0,0,0,0,0,0;

               0,0,0,0,1,0,0,0,0,0,0,0,0,0,0];

我可以使用此代码在 2d 中做到这一点

clc;
clear;
%matrix size

A = zeros(70);
A(20,20) = 1;   %arbitrary point

B = zeros(70);
B(40,40) = 1; %arbitrary point

D1 = bwdist(A);
D2 = bwdist(B);

D_sum = D1 + D2 ;

path_pixels = imregionalmin(D_sum);

spy(path_pixels)

如何将此方法扩展到 3d?

4

1 回答 1

1

这完全取决于您所说的“连接”是什么意思。但是,如果目标是起点和终点之间的一个单元格宽的区域,那么它看起来很像“一个像素宽”的线,可以定义如下。

% start and end point of line
a = [1 10 2];
b = [4 1 9];

% get diffs
ab = b - a;

% find number of steps required to be "one pixel wide" in the shorter
% two dimensions
n = max(abs(ab)) + 1;

% compute line
s = repmat(linspace(0, 1, n)', 1, 3);
for d = 1:3
    s(:, d) = s(:, d) * ab(d) + a(d);
end

% round to nearest pixel
s = round(s);

% if desired, apply to a matrix
N = 10;
X = zeros(N, N, N);
X(sub2ind(size(X), s(:, 1), s(:, 2), s(:, 3))) = 1;

% or, plot
clf
plot3(s(:, 1), s(:, 2), s(:, 3), 'r.-')
axis(N * [0 1 0 1 0 1])
grid on

请原谅丑陋的代码,我这样做很匆忙;)。

于 2015-05-11T18:22:54.890 回答