Image rotation & tracking locations - math

I am rotating an image around it center point but need to track a location on the image as it rotates.
Given:
Origin at 0,0
Image width and height of 100, 100
Rotation point at C(50,50)
Angle of "a" (say 90 degrees in this example)
Point P starts at (25,25) and after the rotation it is at Pnew(75,25)
I haven't touched trig in 20 years but guessing the formula is simple...

I haven't touched trig in 20 years but guessing the formula is simple...
Yep, fairly. To map the point (x1,y1) via rotation around the origin:
x2 = cos(a) * x1 + sin(a) * y1;
y2 = cos(a) * y1 - sin(a) * x1;
... so you just first need to translate to the origin i.e. (-50,-50) in your example and translate back after rotation.
x2 = cos(a) * (x1 - 50) + sin(a) * (y1 - 50) + 50;
y2 = cos(a) * (y1 - 50) - sin(a) * (x1 - 50) + 50;
... Or something like that. (You can generate a matrix which will do all three transformations, I'll leave that for another answer or as an exercise...)

Related

Warpping a camera image into 2D Quad without opencv

I'm doing AR application where I have camera pose orientation and position.
Given 4 points in world coordinates, how would I wrap the camera image into 2D quad ?
So given the Right image, I would like to get a 2D quad as shown in the left.
Parameters of perspective transformation matrix could be calculated using system of 8 equations for initial and warped coordinates of points:
x1' = (A * x1 + B * y1 + C) / (G * x1 + H * y1 + 1.0)
y1' = (D * x1 + E * y1 + F) / (G * x1 + H * y1 + 1.0)
You can find description of perspective transformation math in in Paul Heckbert article.
Example of implementation (C++): Antigrain library (file agg_trans_perspective.h)

Explain - Formula to curve through a control point

I have a question regarding formula curving through a control point.
As you know, HTML Canvas has quadraticCurveTo(x1, y1, x2, y2) with x1 and x2 being the control point.
However when you try to draw a stroke using it, the stroke will never touch the control point.
So we have this formula:
x1 = xt * 2 - (x0 + x2) / 2;
y1 = yt * 2 - (y0 + y2) / 2;
(xt, yt) = the point you want to curve through. t for tangent as it is 90 degrees perpendicular at that point.
This recalculates the control point position.
I got this formula from a book, however the book doesn't explain how it is been derived. I tried google around but in vain.
Anyone knows how this formula is derived?
Thanks,
Venn.
Quadratic Bezier curve is described by equations:
x(t) = x0 * (1-t)^2 + 2 * x1 * t * (1 - t) + x2 * t^2 (and similar for y(t)).
If we apply parameter value t = 1/2 (in some way - middle of the curve), we will get your formula:
x(t=1/2) = xt = x0 * 1/4 + 2 * x1 * 1/4 + x2 * 1/4
then
x1/2 = xt - (x0 + x2)/4
x1 = 2 * xt - (x0 + x2)/2
This is called a Spline. More to the point, it appears that they are using a Bezier Curve.

turn a line into a rectangle

I have a method that draws a line between two points. This works pretty well, but now I want to make this line into a rectangle.
How can I get the points on the left and right side of each of the line points to make it into a rectangle that I can draw?
It is almost as though I need to somehow figure out how to get perpendicular lines programatically....
I'm guessing you basically want fat lines? Lets assume the line is specified by two points (x0, y0) and (x1, y1) we then have:
float dx = x1 - x0; //delta x
float dy = y1 - y0; //delta y
float linelength = sqrtf(dx * dx + dy * dy);
dx /= linelength;
dy /= linelength;
//Ok, (dx, dy) is now a unit vector pointing in the direction of the line
//A perpendicular vector is given by (-dy, dx)
const float thickness = 5.0f; //Some number
const float px = 0.5f * thickness * (-dy); //perpendicular vector with lenght thickness * 0.5
const float py = 0.5f * thickness * dx;
glBegin(GL_QUADS);
glVertex2f(x0 + px, y0 + py);
glVertex2f(x1 + px, y1 + py);
glVertex2f(x1 - px, y1 - py);
glVertex2f(x0 - px, y0 - py);
glEnd();
Since you're using OpenGL ES I guess you'll have to convert the immediate mode rendering (glBegin, glEnd, etc) to glDrawElements. You'll also have to convert the quad into two triangles.
One final thing, I'm a little tired and uncertain if the resulting quad is counterclockwise or clockwise so turn of backface culling when you try this out (glDisable(GL_CULL)).
Since you've asked this question with OpenGL taged I'll assume that you wish for a solution that is close to OpenGL.
As you know you have two points that make up a line. Lets call them x1,y1 and x2, y2
Lets also assume that x1, y1 is the top left point and x2, y2 is the bottom right.
Your 4 points to plot will be [you need to perserve order to make a rectangle with GL_LINES)
Height:y1-y1
Width: x2-x1
(x1, y1)
(x1+width, y1)
(x2, y2)
(x2-width, y2)
drawRect(x,y,w,h)
tl = (x,y)
tr = (x+w, y)
br = (x+w, y+h)
bl = (x, y+h)

Inverse 3D (triangle) projection

I have a 3D math problem which I just can't seem to solve.
I have data of 3 points. The data is a (2D) coordinate on a plane, floating somewhere in 3D space. I also know the (2D) coordinate of the projection. That results in the following array of data:
[[[x1,y1], [px1,py1],
[[x2,y2], [px2,py2],
[[x3,y3], [px3,py3]]
Where the normal (x1 etc.) coordinates stand for the coordinates on the plane and the other (px1 etc.) for the projected coordinates.
What I would like to do is project a new 2D coordinate ([x4,y4]).
.
What I tried so far:
Ofcourse you need an eye for projection, so I set that to [xe,ye,-1]. The xe and ye are known. (It is photo referencing, so I just placed the eye in the center of the photograph.)
Beneath the eye I placed the projection surface (z=0). That gives the following projection coordinates:
[[[x1,y1], [px1,py1,0],
[[x2,y2], [px2,py2,0],
[[x3,y3], [px3,py3,0]]
I can't do the same for the coordinates on the plane, since I don't know anything about that plane.
I also figured that I could make a parameterized formula of the lines running from the eye through the projection coordinates. For line1 that would be:
line1x = xe+(px1-xe)*t1
line1y = ye+(py1-ye)*t1
line1z = -1+t1 // = -1+(0--1)*t1
I also know the distance between the points in 3D. That's the same as in 2D. That means the distance between point1 and point2 would be sqrt((x1-x2)^2+(y1-y2)^2).
I also know the distance between the lines (line1 and line2) at any time. That is sqrt((line1x-line2x)^2+(line1y-line2y)^2+(line1z-line2z)^2).
However, I don't really know how to go from here... Or even whether this is the right route to take.
.
I hope you understand what I want to be able to do, and that you can help me.
Thanks in advance!
There is a function Projection, which can transform points so that Projection([x1, y1]) = [px1, py1] , Projection([x2, y2]) = [px2, py2], Projection([x3, y3]) = [px3, py3]. If I understand correctly, author wants to know how to find this Projection function, so that he can trasnform [x4, y4] into [px4, py4].
Since we are dealing with planes here, the Projection function looks like this:
Proj([ix, iy]) :
return [ax*ix + bx*iy + cx,
ay*iy + by*iy + cy];
Using that we can make 2 equation systems to solve.
The first one
x1 * ax + y1 * bx + cx = px1
x2 * ax + y2 * bx + cx = px2
x3 * ax + y3 * bx + cx = px3
Solving for ax, bx and cx gives us
ax = (px1 * (y3 - y2) - px2*y3 + px3*y2 + (px2 - px3) * y1) /
(x1 * (y3 - y2) - x2*y3 + x3*y2 + (x2 - x3) * y1)
bx = - (px1 * (x3 - x2) - px2*x3 + px3*x2 + (px2 - px3) * x1) /
(x1 * (y3 - y2) - x2*y3 + x3*y2 + (x2 - x3) * y1)
cx = (px1 * (x3*y2 - x2*y3) + x1 * (px2*y3 - px3*y2) + (px3*x2 - px2*x3) * y1) /
(x1 * (y3 - y2) - x2*y3 + x3*y2 + (x2 - x3) * y1)
The second one
x1 * ay + y1 * by + cy = py1
x2 * ay + y2 * by + cy = py2
x3 * ay + y3 * by + cy = py3
Solving for ay, by and cy gives us
ay = (py1 * (y3 - y2) - py2*y3 + py3*y2 + (py2 - py3) * y1) /
(x1 * (y3 - y2) - x2*y3 + x3*y2 + (x2 - x3) * y1)
by = - (py1 * (x3 - x2) - py2*x3 + py3*x2 + (py2 - py3) * x1) /
(x1 * (y3 - y2) - x2*y3 + x3*y2 + (x2 - x3) * y1)
cy = (py1 * (x3*y2 - x2*y3) + x1 * (py2*y3 - py3*y2) + (py3*x2 - py2*x3) * y1) /
(x1 * (y3 - y2) - x2*y3 + x3*y2 + (x2 - x3) * y1)
Note: I used this tool to solve equation systems.
You should use homographic functions and homogeneous coordinates, which are generaly used for 3D perspective operations.
Write
(x4,y4,1) = A1*(x1,y1,1) + A2*(x2,y2,1) + A3*(x3,y3,1),
solving for A1,A2,A3. Then
(xp4,yp4) = A1*(px1,py1) + A2*(px2,py2) + A3*(px3,py3).
1st edit.
(A1,A2,A3) is the solution of the linear system Mat*(A1,A2,A3)=(x4,y4,1).
( x1 x2 x3 )
Mat = ( y1 y2 y3 )
( 1 1 1 )
This can be solved in various ways. For example using Cramer's rules.
2nd edit.
The 1's I inserted are not Z coordinates, but homogeneous extensions of the input coordinates (which must be Euclidean coordinates). (A1,A2,A3) are homogeneous coordinates in the basis formed by the triangle vertices.
3rd edit.
The correspondence between the 3D plane and the projection plane is a projective transformation. It can be defined as a 3x3 matrix T operating on homogeneous coordinates in the input plane (x,y,1) (in your coordinate system) and producing coordinates (u,v,t) in the projection plane. Then px=u/t and py=v/t.
If a point has homogeneous coordinates (A1,A2,A3) in the basis formed by three points of the input plane (not on the same line) then its projection has the same homogeneous coordinates in the projected basis.
It seemed quite clear to me 1 hour ago, but now I'm beginning to doubt: maybe knowing one additional pair of points is needed to have a single solution to the problem... If you can find it, have a look at the book "Algebraic Projective Geometry" by J.G. Semple and G.T. Kneebone.
I don't really understand the problem? Are you trying to locate an object in 3d-space that you know is located on a plane (a wall or floor for example) and the only input you have is 3 points(of which you know the distances between in 3d-space) from a camera-image?
In that case you will have 3 equations like this where localCoordinates is the points coordinates in objectspace(gives the known distance between the points) and world is the objects position in 3d-space.
cameraCoordinates = world*view*projection*localCoordinates
This will yield an equation system with 6 unknown(rotation and position in 3d) and 6 equations (2 for every point). It will however be non linear so you have to solve it using numerical methods. Try the Newton Rapson method.
A "bit" late here, but the highest rated answer doesn't take into account the 3D-space of the problem. We have a perspective projection problem, with three points on a plane (actually any 3 3D points) being projected (as in projective geometry) on the surface of a camera.
It is not possible to give an unambiguous solution to this problem (multiple solutions exist). The general problem of finding a camera position and pose given 3 3D points and their respective 2D perspective projections can be solved using the P3P (Perspective-3-Point) algorithm from the original RANSAC paper, which give up to four possible feasible solutions (with the points in front of the camera).
Given a camera pose, it is trivial to calculate the projection of additional plane points.

algorithm for finding out pixel coordinates on a circumference of a circle

how do i find out pixel value at certain degree on the circumference of a circle if I know the pixel co-ordinates of the center of the circle, radius of the circle ,and perpendicular angle.
Basically, I am trying to draw the hands of a clock at various times ( 1 o clock , 2 o clock etc )
Let h be the hour as a floating point number (h=2.25 would be 02:15, etc.) between 0 and 12. (cX,cY) are the coordinates of the center. hLength and mLength are the lengths of the hour and min hands.
// Hour hand
hAngle = 2.0*Pi*h/12.0; // 0..12 mapped to 0..2*Pi
hX = cX + hLength * sin(hAngle);
hY = cY - hLength * cos(hAngle);
// Min hand
mAngle = 2.0*Pi*h; // 0..1 mapped to 0..2*Pi, etc.
mX = cX + mLength * sin(mAngle);
mY = cY - mLength * cos(mAngle);
Where the centre of the circle is (X0, Y0), the radius is R and the angle with the x-axis is theta:
X1 = (R * cos theta) + X0
and
Y1 = (R * sin theta) + Y0
If (x1,y1) is a point on the circumference and (x,y) is the center, then x1 = x + r * cos(angle) and y1 = y + r * sin(angle)
if center is at x0, y0, and 0,0 iz at bottom-left corner, then 1 o'clock is at x0 + rsin(2π/3), y0+rcos(2π/3).
Draw lines from the center to coordinates computed with sin for the y coordinates and cos for the x coordinates (both multiplied by the length of the hand).
Wikipedia has more information on how sin and cos "work".

Resources