Find pixels at given distance along line

Hello, as you can see on image attached I´ve computed perpendicular lines to red lines. I know the slope of red line, the slope of green line and I know the coordinates of the cross-section also. So I computed x and y coordinates of the green line as follows:
slope = (r1 - r2) / (s1 - s2); %red line slope
slRec = -1 / slope; %perpendicular green line slope
ym = (y1 + y2)/2; xm = (x1 + x2)/2; %cross-section point
according to well known line equation y = ax + b I´ve counted y when x was created as:
b = ym - a*xm;
length = 5;
y = ym - length : 0.25 : ym + length;
x = (y - b) / a;
coordinates = ([y; x])';
But as you can see on my image, there is problem that when the length of the line is given by y-axis range it is different length itself. How can I find pixel value at unit distance from the red line (and along green line)?

Answers (1)

I've done this successfully before. What you need to do, so that the cross sectional line doesn't wildly waver all over the place is to take several points along the red line, not just 2. Then use polyfit() to fit a line or quadratic. (The line is easy. Using a quadratic of cubic is also easy but requires calculus.) Then compute your perpendicular line assuming some fixed length, like 30 pixels or whatever.

4 Comments

Image analyst, I tried your suggestion, but probably I don´t understand completely what you ment, because it doesn´t work. Instead of two points (6 pixels far from each other), which were endpoints of red lines, I used six consecutive pixels (or their coordinates) and used them for polyfit(x,y,1). First coefficient I used for computing reciprocal negative slope. But what is the second coeff. good for? I did not use it and maybe it is why it doesn´t work. My code bellow:
p = polyfit(x,y,1);
slope = p(1);
SlopeRec = -1 / slope; %slope of perpendicular line
length = 10;
x = sm - length : sm + length; %xm is for midpoint(rm,sm)
y = SlopeRec*(x - sm) + rm;
My code is 342 lines long, not including some helper functions. I'll see if I can take out some proprietary/special-case information and make it simpler and more general. I think your first part is right but you're not calculating the endpoints of the green (perpendicular) lines correctly. You don't just have a fixed delta x between the endpoints - it changes depending on what the slope is. If the slope is steep, x1 and x2 might only be 4 apart but if the line is flat they might be around 20 apart.
By the way, don't use length as the name of a variable . It will blow away a built-in function and that's not good.
ok. actually I don´t use length for name of variable in my code, because I´m not english speaking and I changed only for the question purpose. My delta x not fixed is the problem I´m trying to solve. I´d be happiest if the length of green line would be such that it touches the object border. Thank you for all your answers.
I've started working on a general purpose demo but it's not done yet.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 1 May 2014

Commented:

on 3 May 2014

Community Treasure Hunt

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

Start Hunting!