Select points on plot and calculate slope

I have been trying to select two points on an image and then calculate the slope based on the location of those two points. I have been using getpts but keeping "Error using getpts line 174 Interruption during mouse point selection"
I have tried a few versions of the code:
In version three I get an output which I don't fully understand
m =
-0.7083 0
-0.7500 0
%Version 1
X=imread('dog.jpg');
figure,imshow(X)
[x,y] = getpts;
%Version 2
read and display image to add markers
X=imread('dog.jpg');
figure,imshow(X)
[x1,y1] = getpts(get(imshow(X),'Parent'));
[x2,y2] = getpts(get(imshow(X),'Parent'));
hold on
p1 = [x1,y1];
p2 = [y1,y2];
%Version 3
X=imread('dog.jpg');
figure,imshow(X)
[x1,y1] = ginput(1);
hold on; % Prevent image from being blown away.
plot(x1,y1,'r+', 'MarkerSize', 25);
[x2,y2] = ginput(2);
hold on; % Prevent image from being blown away.
plot(x2,y2,'r+', 'MarkerSize', 25);
m = (y2-y1)/(x2-x1)

1 Comment

Is this your question?
  1. Select any two points on the image
  2. Draw Line between those selected two points
  3. Calculate the slope of that line
Clarify?

Sign in to comment.

Answers (1)

im=imread('1.png');
[rows colm]=size(im);
point1=[randi(rows),randi(colm)];
point2=[randi(rows),randi(colm)];
imshow(im);
hold on;
line(point1,point2,'linewidth',2);
slope=(point2(2)-point1(2))/(point2(1)-point1(1));
fprintf('The slope is %f',slope);

Asked:

AP
on 5 Jun 2019

Answered:

on 5 Jun 2019

Community Treasure Hunt

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

Start Hunting!