matlab - Connecting two points in multidimensional matrix -
i'm trying create 3d matrix of 1's , 0's. want connect 2 points (shortest distance) forming line of 1's in between them.
it this, in 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]; i'm able in 2d using code
clc; clear; %matrix size = 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) how can expand method 3d?
it depends mean "connect", exactly. but, if goal one-cell wide region between starting , end points, looks lot "one-pixel wide" line, defined follows.
% start , end point of line = [1 10 2]; b = [4 1 9]; % diffs ab = b - a; % find number of steps required "one pixel wide" in shorter % 2 dimensions n = max(abs(ab)) + 1; % compute line s = repmat(linspace(0, 1, n)', 1, 3); d = 1:3 s(:, d) = s(:, d) * ab(d) + a(d); end % round nearest pixel s = round(s); % if desired, apply 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 please forgive ugly code, did in rush ;).
Comments
Post a Comment