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 件のコメント

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 6 月 5 日
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?

サインインしてコメントする。

回答 (1 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 6 月 5 日

0 投票

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);

質問済み:

AP
2019 年 6 月 5 日

回答済み:

2019 年 6 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by