필터 지우기
필터 지우기

Finding Max Distance between coordinates

조회 수: 38 (최근 30일)
Prabs
Prabs 2013년 10월 20일
답변: Alaa 2015년 3월 4일
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
sixwwwwww
sixwwwwww 2013년 10월 20일
Can you please share your code and image to sort out the solution?
Prabs
Prabs 2013년 10월 20일
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

댓글을 달려면 로그인하십시오.

답변 (3개)

sixwwwwww
sixwwwwww 2013년 10월 20일
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
Image Analyst
Image Analyst 2013년 10월 20일
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.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2013년 10월 20일
편집: Image Analyst 2013년 10월 20일
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 2015년 3월 4일
Please, I have the same problem here:
And your help will be highly appreciated...

카테고리

Help CenterFile Exchange에서 Feature Detection and Extraction에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by