How can I calcuate the distance from two selected points?

Is there a simple way (a command) to be able to identify in a grafical way the distance beetween two points on a figure? If I have a figure with some points I wonder if There is a way that allow you to have in ouput the distance between the two selected points? For example a sort of ginput command. Have you got an idea?

 Akzeptierte Antwort

Amit
Amit am 1 Feb. 2014
Bearbeitet: Amit am 1 Feb. 2014

9 Kommentare

I want to obtain the distance from two points that I selected by mouse and I want also to approximate the distance
gplot(ones(rows), dati, '--rs');
x=ginput(1);
y=ginput(1);
h1 = imdistline(gca,[y(1) x(1)],[y(2) x(2)]);
distanza= round(h1)
This routine is a good idea, how do you think?
Actually, the ginput is your choice. However, the line produce by imdistline is draggable.
You can do something as following:
h1 = imdistline;
api = iptgetapi(h);
distanza = round(api.getDistance);
But imdistile it's not very useful because I have to selected the two points and as ouput I have to obtain the distance. Is there another method?
The you can use the:
x=ginput(1);
y=ginput(1);
to get your points and then use imdistline. rest will be same and distanza is the variable that will have your distance.
You said this
x=ginput(1);
y=ginput(1);
h1 = imdistline(gca,[y(1) x(1)],[y(2) x(2)]);
distanza = round(api.getDistance);
api = iptgetapi(distanza);
But there is an error.
??? Undefined variable "api" or class "api.getDistance".
That's not the right code. You do
[x, y] = ginput(2); % x and y are both 2 element arrays.
But Francesco, with both imdistline() and ginput(2) you have to select the two points . I'm totally baffled when you say "it's not very useful because I have to selected the two points". Well yeah . How can you have a distance between two points if you don't select the two points somehow? Awaiting your answer to that....
But with the
[x, y] = ginput(2); % x and y are both 2 element arrays.
I don't have in output the distance between points.
If you notice, there is a major difference between what I told you and what you implemented.
In your version of code, you put api variable afterwards.
imdistline give you the value of distance on the figure. However the output h1 is not the distance but a handle for iptgetapi().
You need to do this exactly the way I am writing below:
h1 = imdistline(gca,[y(1) x(1)],[y(2) x(2)]);
api = iptgetapi(h1);
distanza = round(api.getDistance);
Francesco, can you answer my question about how you want to do it without selecting points?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 1 Feb. 2014
Try this method with improfile:
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 long g;
format compact;
fontSize = 20;
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
filePattern = fullfile(folder, '*.jpg')
[f p] = uigetfile(filePattern);
subplot(2, 1, 1);
originalImage = imread([p f]);
grayImage = rgb2gray(originalImage);
imshow(grayImage);
title('Original Image', 'FontSize', fontSize);
axis on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
uiwait(msgbox('Left click, then right click'));
[x, y, intensityProfile] = improfile();
% Plot crosses over where you clicked.
hold on;
plot(x, y, 'r-', 'LineWidth', 3);
% Plot line
plot([x(1), x(end)], [y(1), y(end)], 'r+', 'LineWidth', 3, 'MarkerSize', 20);
% Plot intensity profile in another plot.
subplot(2, 1, 2);
plot(intensityProfile, 'LineWidth', 3);
grid on;
title('Intensity Profile', 'FontSize', fontSize);
distance = sqrt((x(1) - x(end))^2 + (y(1) - y(end))^2)
message = sprintf('The distance = %.1f pixels.', distance);
uiwait(helpdlg(message));
Note: you still have to select the two points.

2 Kommentare

[x, y, intensityProfile] = improfile();
error in this
Sorry but my Crystal Ball Toolbox is still on order, and, until then, I can't see your monitor. Can you tell me the error? Because when I copied and pasted the code above, it ran just fine. So post your modified code in your own question and I'll fix it there.

Melden Sie sich an, um zu kommentieren.

Tags

Gefragt:

am 1 Feb. 2014

Kommentiert:

am 17 Jan. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by