how can I find the furthest pair of points in a 3D binary matrix?
3 views (last 30 days)
Show older comments
the 3D matrix only contains one object. I want to know which of the two '1' values are the furthest away from each other. I read through all the functions and there doesn't seem to be a way. Is there one I don't know about?
0 Comments
Accepted Answer
Image Analyst
on 14 Aug 2017
Just do a brute force triple for loop. I attach a way in 2-D. A vectorized way would be to use find() and pdist2(), if you have the Statistics and Machine Learning Toolbox. Attach your 3-D binary image in a .mat file if you need more help. If you do that, let me know if you have the stats toolbox.
3 Comments
Image Analyst
on 15 Aug 2017
You forgot to say if you tried it yourself. I expect that you did, so you can compare your code to mine and see if you get the same values.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 20;
filename = fullfile(pwd, 'armsegmentation.mat');
s = load(filename)
arm = s.Arm;
maxValue = max(arm(:))
linearIndexes = find(arm);
for k = 1 : length(linearIndexes)
[yRow(k), xCol(k), zSlice(k)] = ind2sub(size(arm), linearIndexes(k));
end
xyz = [xCol', yRow', zSlice'];
distances = pdist2(xyz, xyz);
imshow(distances, []);
title('Distances between points', 'FontSize', fontSize);
xlabel('Point 1', 'FontSize', fontSize);
ylabel('Point 2', 'FontSize', fontSize);
axis on;
% Find the max distance
maxDistance = max(distances(:))
% Find the indexes where it occurs
[index1, index2] = find(distances == maxDistance)
fprintf('Farthest apart are linear indexes %d and %d.\n',...
index1(1), index2(1));
% Extract the x,y,z of the indexes.
x1 = xyz(index1(1), 1);
y1 = xyz(index1(1), 2);
z1 = xyz(index1(1), 3);
x2 = xyz(index2(1), 1);
y2 = xyz(index2(1), 2);
z2 = xyz(index2(1), 3);
fprintf('Linear index %d is at row = %d, column = %d, and slice %d.\n',...
index1(1), y1, x1, z1);
fprintf('Linear index %d is at row = %d, column = %d, and slice %d.\n',...
index2(1), y2, x2, z2);
fprintf('They are %f voxels apart.\n', maxDistance)
Results:
Farthest apart are linear indexes 2382 and 157.
Linear index 2382 is at row = 107, column = 181, and slice 35.
Linear index 157 is at row = 130, column = 108, and slice 28.
They are 76.857010 voxels apart.
More Answers (0)
See Also
Categories
Find more on Image Processing and Computer Vision in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!