Finding Max Distance between coordinates

77 vues (au cours des 30 derniers jours)
Prabs
Prabs le 20 Oct 2013
Réponse apportée : Alaa le 4 Mar 2015
Hi guys, Urgently need some help on this. I have made a code that analyses any given image and outputs the coordinates of the boundary points into 2 separate arrays, x and y. I want to find the distance between all the coordinates with each other. And then identify the coordinates that give the longest distance.
Thanks
  2 commentaires
sixwwwwww
sixwwwwww le 20 Oct 2013
Can you please share your code and image to sort out the solution?
Prabs
Prabs le 20 Oct 2013
if true
% s=imread('twolines.tif'); %calls the image into matlab
numwhite2 = regionprops(s,'BoundingBox', 'Extrema'); %finds the number of white pixels in the image, although it doesn't show when the image is called.
figure;imshow(s,[]); %graphically shows the image
% -- plot bounding box
figure;
imshow(s,[]);
hold;
h1=rectangle('position', numwhite2.BoundingBox);
set(h1, 'linewidth', 2, 'edgecolor', 'r');
% -- Draw bound on the image
[ilabel, num]=bwlabel(s); [B, L]=bwboundaries(ilabel, 'noholes');
coord=cell2mat(B); x=coord(:,1); y=coord(:,2); plot(y,x,'.g'); coorddata = [x y]; end

Connectez-vous pour commenter.

Réponses (3)

sixwwwwww
sixwwwwww le 20 Oct 2013
Dear Prabs, here is an example code which you can use for this purpose:
x = 1:10; % x component of coordinate
y = 2:2:20; % y component of coordinate
count = 1;
for i = 1:length(x) - 1
for j = i + 1:length(x)
distance(count) = sqrt((x(i) - x(j))^2 + (y(i) - y(j))^2);
Matrix(count, :) = [x(i) y(i) x(j) y(j) distance(count)];
count = count + 1;
end
end
SortedMatrix = sortrows(Matrix, 5);
MaxDis = SortedMatrix(size(Matrix, 1), :);
fprintf('Coordinates of maximum distnace are (x1, y1) = (%d, %d) and (x2, y2) = (%d, %d)\n', MaxDis(1), MaxDis(2), MaxDis(3), MaxDis(4))
fprintf('Maximum distance = %d\n', MaxDis(5))
I hope it helps.
  1 commentaire
Image Analyst
Image Analyst le 20 Oct 2013
You can use a vectorized approach, like I did in the engine at the middle of my code below to have a more "MATLAB-ish" faster, more efficient approach.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 20 Oct 2013
Modifié(e) : Image Analyst le 20 Oct 2013
You can find the distances from a point to all the other points in one single line, assuming you have the x and y coordinates. (Don't be afraid - the main code is only 4 lines long and is at the center between the ----- lines. The rest of it is just to make it fancy by displaying and printing the points.)
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
% Initialization code
numberOfCoords = 20;
% Plot the points so we can see them.
x = rand(1, numberOfCoords);
y = rand(1, numberOfCoords);
plot(x, y, 'b*');
hold on;
for k = 1 : numberOfCoords
% Label the kth point in the plot.
text(x(k)+0.01, y(k), num2str(k));
end
maxDistance = zeros(1, numberOfCoords);
indexOfMax = zeros(1, numberOfCoords, 'int32');
%-----------------------------------------------
% Main engine:
% Find the furthest away points.
for k = 1 : numberOfCoords
distances = sqrt((x-x(k)).^2 + (y-y(k)).^2);
[maxDistance(k), indexOfMax(k)] = max(distances);
end
%-----------------------------------------------
% Done! Now show out results in the command window.
% Display in command window.
for k = 1 : numberOfCoords
thisDistance = maxDistance(k);
thisIndex = indexOfMax(k);
fprintf('Point #%d at (%.1f, %.1f) is farthest away from point #%d at (%.1f, %.1f) and is at a distance of %f\n',...
thisIndex, x(thisIndex), y(thisIndex),...
k, x(k), y(k), thisDistance);
end

Alaa
Alaa le 4 Mar 2015
Please, I have the same problem here:
And your help will be highly appreciated...

Catégories

En savoir plus sur 3-D Volumetric Image Processing dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by