If I understand correctly, you have two arrays of voxels in 3D that represent the perimeter - and you want the distance between voxels of one and the other. My approach would be something like this (assuming A
and B
are the initial volumes, and that they are the same size):
Aperi = bwperim(A,6);
Bperi = bwperim(B,6);
Now we need the coordinates of the non-zero voxels:
sz = size(A);
[xx yy zz] = meshgrid(1:sz(1), 1:sz(2), 1:sz(3));
indxA = find(Aperi==1);
posA = [xx(indxA(:)) yy(indxA(:)) zz(indxA(:))]; % a Nx3 matrix of x,y,z positions
indxB = find(Bperi==1);
posB = [xx(indxB(:)) yy(indxB(:)) zz(indxB(:))]; % a Mx3 matrix - note N ~= M
Now you have two 2D matrices and you can take the difference between points - but you don't know which pairs you want to compare so you need a bit more work:
delta = bsxfun(@minus, reshape(posA, 1, [], 3), reshape(posB, [], 1, 3) );
Now delta
has dimensions [M N 3], with differences between each pair of points in posA and posB. The distance is
distance = sqrt(sum(delta.*delta, 3));
And for each point in A, the distance to the closest point in B is
closest = min(distance);
which will be a [1 N]
row vector.
To put these values back into the original matrix, you need to go back to the indices you had in the beginning:
distMatrix = zeros(size(A));
distMatrix(indxA) = closest;
And I think you will now have the distance of the closest point on the perimeter of B to each point on the perimeter of A mapped to the surface of A.
Let me know if this gets you anywhere to the answer you were looking for...