Another how to draw a parallel line that pass a specific point question

조회 수: 10 (최근 30일)
as hz
as hz 2013년 10월 28일
댓글: Image Analyst 2013년 10월 29일
Hi,
I cannot find what is the problem with my code as the output is not what I want. This is a follow up question to http://www.mathworks.com/matlabcentral/answers/104062-draw-a-parallel-line-thst-pass-a-specific-point
My aim is to generate a parallel line to another line that pass through a point. At the beginning I choose two points where a line is passed via them, then I choose a third point and a parallel line that passes through it is created. But the parallel line slope is not correct. The parallel line start and end X points are the beginning and end of the image dimensions.
I have attached the output which visually shows the problem. Therefore, what is the problem and why it does not work? (I now its a simple geometry and that’s what frustrate me).
Thanks a lot.
Code:
clc;
clear;
I = imread('pout.tif');
figure, imshow(I);
[ximage yimage] = size(I)
b1 = impoint(gca,[]);
b2 = impoint(gca,[]);
b3 = impoint(gca,[]);
pos1 = getPosition(b1);
x1=pos1(1,1);
y1=pos1(1,2);
pos2 = getPosition(b2);
x2=pos2(1,1);
y2=pos2(1,2);
pos3 = getPosition(b3);
x3=pos3(1,1);
y3=pos3(1,2);
slope = ((y1-y2)/(x2-x1))
hold on
plot([x1,x2],[y1,y2],'Color','r','LineWidth',2);
x4=0;
x5=ximage;
y4 = slope * (x4 - x3) + y3
y5 = slope * (x5 - x3) + y3
impoint(gca,x4,y4);
impoint(gca,x5,y5);
plot([x4,x5 ],[y4,y5],'Color','r','LineWidth',2);

답변 (1개)

Image Analyst
Image Analyst 2013년 10월 28일
Your slope is wrong, plus I made numerous other improvements. See this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
grayImage = imread('pout.tif');
imshow(grayImage);
% 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')
[rows, columns] = size(grayImage)
message = sprintf('Click and drag the endpoints of the line\nand double-click the double arrows to finish.');
uiwait(helpdlg(message));
h = imline;
lineEndPoints = wait(h)
delete h;
x1 = lineEndPoints(1,1);
y1 = lineEndPoints(1,2);
x2 = lineEndPoints(2,1);
y2 = lineEndPoints(2,2);
hold on
plot([x1,x2],[y1,y2], 'r-', 'LineWidth',2); % Make line.
plot([x1,x2],[y1,y2], 'yo', 'LineWidth',2,'MarkerSize', 12); % Make circles.
message = sprintf('Click a third point.');
uiwait(helpdlg(message));
[x3, y3] = ginput(1);
plot(x3, y3,'yo','MarkerSize', 12);
% Calculate the slope.
slope = (y2 - y1)/ (x2 - x1)
% Draw second line.
x4=0;
x5=rows;
y4 = slope * (x4 - x3) + y3
y5 = slope * (x5 - x3) + y3
plot([x4,x5 ],[y4,y5],'r-','LineWidth', 2);
  댓글 수: 4
as hz
as hz 2013년 10월 29일
Thanks. I saw the error.
what will you adivse to do if I draw a vertical line which results with no slope (infinity)?
As always thanks.
Image Analyst
Image Analyst 2013년 10월 29일
That needs to be detected in advance and treated as a special case.

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

카테고리

Help CenterFile Exchange에서 Explore and Edit Images with Image Viewer App에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by