How to dertmine the DirectionVector of a Line? - image-processing

I have a programming problem , in the context of a geometric shape recognition(Rectangles, ovals etc).
In this context, if I have a a simple line, from say (x1,y1) to (x2,y2) - made up of a series of points(x-y pairs) -
How would I calculate the DIRECTION VECTOR for this line? I understand the math behind it, but I'm finding the algorithm provided by my client a bit vague. I'm stuck at step 3) of this algorithm.
The following is the algorithm(in English as opposed ot psedocode), exactly as provided by my client.
1) Brake the points that make up a "stroke" or "line" up in to sets of X(where by default X= 20 - we will adjust) points = a PointSet
2) For Each PointSet, find the EndPouint(average of the points at the ends) for the first and last Y points(where by default Y= X/5).
3) Find the DirectionVector of the PointSet= Subtract the CentrePoints
4) For each pair of PointSets, find the AngleChange = the angle between the DirectionVectors of the PointSets.
and so on.......
I am trying to figure out what point (3) means......
Any help would be DEEPLy appreciated folks! THANKS in advance.

If the segment from (x1,y1) to (x2,y2) is short, then you can approximate its direction vector simply by: (x2-x1)*i + (y2-y1)*j.
Otherwise, you could use PCA to estimate the direction vector as the principal axis of individual points forming the segment,

Related

Compute Tangent vector over 2D Points

I have computed the contours of an object in an image. Now I have a 2D array, each of element representing X & Y coordinates of a contour point.
Now, I want to compute a tangent vector over each point and angle between them (contour point and tangent vector).
My points are ordered. i.e. p[i+1,] is next to p[i,] and my path is closed. i.e. p[0] is next to p[N-1] (If I consider N points. The image of contour points is attached below.
I have done a lot of search but never find any clue. Any help would be highly appreciated. Thanks.
The trivial way is :
Tangent[i] = Normalize(Contour[i+1] - Contour[i-1])
You would simply need to take care of boundary conditions if any!

How to find perpendicular lines in polar coordinates?

Say I have the lines shown in the image below, represented in polar coordinate format (rho and theta). These lines are the output of OpenCV's HoughLines function after some post processing. (Sorry I'm not allowed to embed images yet.)
What I want to do is, given any one line, find all of the lines that are perpendicular to that line, as shown in the second image below.
I understand how to do this with Cartesian lines, but I'm having trouble wrapping my mind around what properties of rho and theta the two lines would have to have to be perpendicular, although I understand how polar lines work at least fundamentally. Sorry if this is elementary stuff, but I'm having trouble finding any explanation of this online anywhere. Do I need to first convert the lines to Cartesian coordinates, or is there some simpler way to do this? Any help would be much appreciated, thanks!
To get perpendicular lines in polar coordinates, you simply take the theta for the first line, and find all lines whose theta = +/- 90° of the first theta.
You have to normalize the angles to be within 0°-360° or some other range, when comparing them.
So if line 1 has a theta line1.Theta
Then the angle to another line is a = (line2.Theta - line1.Theta)
and you want all lines where a is close to -90°, 90°, 270°, -270°, ...
depending on how you normalize your angles

How do I get a 3 dimension velocity vector from a speed and 3 dimension angle vector

So I have a function called
function AngleToVector(speed,xAngle,yAngle,zAngle)
local angle = Vector3.new(xAngle,yAngle,zAngle)
--
-- calculations
--
local position = Vector3.new(SOMETHING,SOMETHING,SOMETHING)
return position
end
I decided to rewrite the entire question. In the picture, sqrt(27) is the distance a bullet travels in one second. Assume that I know the 3 angles that dictate where this line is pointing. I'm trying to find the length of the 3, red green and blue, dotted lines using my "speed" scalar, and my 3 angles which show the direction of my scalar.
You state that the final Vector3 that is returned should contain the values that the subject should step in to travel a certain speed. This is actually quite straight forward. The returned Vector3 should be the unit vector of the angle vector multiplied by the speed scalar.
This is more of a Math question than a programming question, and I am not familiar with the library you are using, so I will answer it mathematically. First, divide each component of the angle vector by the magnitude of the angle vector. This gives you a unit vector in that direction (e.g. 1 meter in that direction). Multiply this unit vector by the speed given, and that will give you speed steps in that direction. You can then add this new vector to the current position for the object to move.
I recommend doing some reading on Vectors and Matrices.
velocityVector = CFrame.Angles(xAngle,yAngle,zAngle).lookVector*speed
Is probably what you want. CFrame.lookVector gives you the unit Vector that Luke was talking about. CFrame makes everything a ton easier when you get used to it.

Find slope (angle) of each line of the image

I have to calculate a slope (or an angle) of every single detectable line of the image. And even to detect the changes of the slope of the line, if it is possible.
I've performed 2D Fourier and I know a neighborhood averege angle at every area (sets of 64x64px). I even try a Hough transform, but neither sobel nor prewitt edge detection seems to detect these lines appropriately.
Please note that some of the lines are crossing each other, and some aren't straight.
Is there a method to detect the slope of each line? Or to detect these lines in order to perform an usefull Hough transform?
If you need the full image I can upload it somewhere.
Image
Greets Adamek,
I hope it is not too late. Here some quick ideas:
1) Using Hough trafo to detect lines is a good idea as a first step
2) Second step would be some kind of labeling to really know what lines there are. The most difficult problem to address is probably how to determine start and end of lines and seperate potentially connected ones. Search for the labeling keyword in this context, that should give some results.
3) Afterwards, having end and startpoint, I would
a) calculate for each line a regression line if you need more exact data in further analysis
b) just compute slope and intercept via f(x)=mx+n, where m is the slope and n the intercept. Given two points in 2D this is easily done as follows:
slope = (yRight - yLeft)/(xRight - xLeft);
m_oIntercept = ((yLeft - slope*xLeft) + (yRight - slope*xRight))*0.5;
and don't forget to test for (xRight-xLeft) < eps before to avoid zero division.
Hope that helps,
G.

Math/OpenGL ES: Draw 3D bezier curve of varying width

I've been working on a problem for several weeks and have reached a point that I'd like to make sure I'm not overcomplicating my approach. This is being done in OpenGL ES 2.0 on iOS, but the principles are universal, so I don't mind the answers being purely mathematical in form. Here's the rundown.
I have 2 points in 3D space along with a control point that I am using to produce a bezier curve with the following equation:
B(t) = (1 - t)2P0 + 2(1 - t)tP1 + t2P2
The start/end points are being positioned at dynamic coordinates on a fairly large sphere, so x/y/z varies greatly, making a static solution not so practical. I'm currently rendering the points using GL_LINE_STRIP. The next step is to render the curve using GL_TRIANGLE_STRIP and control the width relative to height.
According to this quick discussion, a good way to solve my problem would be to find points that are parallel to the curve along both sides taking account the direction of it. I'd like to create 3 curves in total, pass in the indices to create a bezier curve of varying width, and then draw it.
There's also talk of interpolation and using a Loop-Blinn technique that seem to solve the specific problems of their respective questions. I believe that the solutions, however, might be too complex for what I'm going after. I'm also not interested bringing textures into the mix. I prefer that the triangles are just drawn using the colors I'll calculate later on in my shaders.
So, before I go into more reading on Trilinear Interpolation, Catmull-Rom splines, the Loop-Blinn paper, or explore sampling further, I'd like to make sure what direction is most likely to be the best bet. I think I can say the problem in its most basic form is to take a point in 3D space and find two parallel points along side it that take into account the direction the next point will be plotted.
Thank you for your time and if I can provide anything further, let me know and I'll do my best to add it.
This answer does not (as far as I see) favor one of the methods you mentioned in your question, but is what I would do in this situation.
I would calculate the normalized normal (or binormal) of the curve. Let's say I take the normalized normal and have it as a function of t (N(t)). With this I would write a helper function to calculate the offset point P:
P(t, o) = B(t) + o * N(t)
Where o means the signed offset of the curve in normal direction.
Given this function one would simply calculate the points to the left and right of the curve by:
Points = [P(t, -w), P(t, w), P(t + s, -w), P(t + s, w)]
Where w is the width of the curve you want to achieve.
Then connect these points via two triangles.
For use in a triangle strip this would mean the indices:
0 1 2 3
Edit
To do some work with the curve one would generally calculate the Frenet frame.
This is a set of 3 vectors (Tangent, Normal, Binormal) that gives the orientation in a curve at a given parameter value (t).
The Frenet frame is given by:
unit tangent = B'(t) / || B'(t) ||
unit binormal = (B'(t) x B''(t)) / || B'(t) x B''(t) ||
unit normal = unit binormal x unit tangent
In this example x denotes the cross product of two vectors and || v || means the length (or norm) of the enclosed vector v.
As you can see you need the first (B'(t)) and the second (B''(t)) derivative of the curve.

Resources