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

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