I know the transformation matrices for rotation, scaling, translation etc. I also know the matrix for shear transformation. Now, I need to have the shear matrix--
[1 Sx 0]
[0 1 0]
[0 0 1]
in the form of a combination of other aforesaid transformations. Tried searching, tried brainstorming, but unable to strike! Thanks!
The x-shear operation for a shearing angle thetareduces to rotations and scaling as
follows:
(a) Rotate by theta/2 counter-clockwise.
(b) Scale with x-scaling factor = sin(theta/2) and y-scaling factor = cos(theta/2).
(c) Rotate by 45 degree clockwise.
(d) Scale with x-scaling factor = sqrt(2)/sin(theta) , and y-scaling factor= sqrt(2).
Yup it can be done, a rotation followed by non uniform scaling and reverse rotation. You can find the details here in third question http://www.cs.cmu.edu/~djames/15-462/Fall03/assts/15-462-Fall03-wrAssign1-answer.pdf. you can try the following openGL code as well. It rotates a rectangle by 45 degree then scales in x-axis. and then rotates in -26 degree i.e. atan(0.5). 0.5 comes from finding the angle between x-axis and one side after scaling in x-direction.
glRotatef(-26.0, 0.0, 0.0, 1.0);
glScalef(2,1,1);
glRotatef(45.0, 0.0, 0.0, 1.0);
glRectf(0, 0, 25.0, 25.0);
In 3D Graphics we often use a 4x4 Matrix with 16 useful elements. The Identity 4x4 Matrix is as following:
Between those sixteen elements there are 6 different shearing coefficients:
shear XY
shear XZ
shear YX
shear YZ
shear ZX
shear ZY
In Shear Matrix they are as followings:
Because there are no Rotation coefficients at all in this Matrix, six Shear coefficients along with three Scale coefficients allow you rotate 3D objects about X, Y, and Z axis using magical trigonometry (sin and cos).
Here's an example how to rotate 3D object (CCW) about its Z axis using Shear and Scale elements:
Look at 3 different Rotation patterns using Shear and Scale elements:
Shears are an elementary matrix operation, so while you can express them as "a combination of other matrix operations", doing so is really weird. Shears take the two forms:
| 1 V | | 1 0 |
| 0 1 | , | V 1 |
Whereas a rotation matrix is much more involved; the idea of expressing a shear using rotations suggests you haven't actually written these things out yet to see what you need, so let's look at this. A rotation matrix is of the form:
| cos -sin |
| sin cos |
Which can be composed as a sequence of three particular shear matrices, R = Sx x Sy x Sx:
| cos(a) -sin(a) | | 1 0 | | 1 sin(a) | | 1 0 |
| | = | | x | | x | |
| sin(a) cos(a) | | -tan(a/2) 1 | | 0 1 | | -tan(a/2) 1 |
Now, we can do some trivial matrix manipulation to get Sy. First left-multiply:
R = Sx x Sy x Sx
Sx⁻¹ x R = Sx⁻¹ x Sy x Sx
Sx⁻¹ x R = I x Sy x Sx
Sx⁻¹ x R = Sy x Sx
And then right-multiply:
Sx⁻¹ x R x Sx⁻¹ = Sy x Sx x Sx⁻¹
Sx⁻¹ x R x Sx⁻¹ = Sy x I
Sx⁻¹ x R x Sx⁻¹ = Sy
As a trivial rewrite, one shear is now two shears and a rotation.
But the much more important question is: why do you need to express the shear matrix as something else? It's already an elementary matrix form, what bizare computing environment are you in, or what crazy thing are you trying to do, that requires you to express an elementary transform as a way more complex, way slower thing to compute? =)
Related
I have 4 points (black A, black B, black C and black D) which are the vertices of the rotated red square.
Is it possible to determine what were the three angles (x angle, y angle and z angle) used to rotate the red square into the black square?
In this case, the angles were:
X = 1rad
Y = 0.2rad
Z = 0.3rad
EDIT: I just have the four black points, not the red ones.
The only thing I know about the red square is that it is a square
There is method to find affine matrix needed to transform point set into another one.
Having natrix, you can find angles.
Description for 2D case is here, 3D case is similar.
M * A = B
where
| xa xb xc xd|
A =| ya yb yc yd|
| za zb zc zd|
| 1 1 1 1 |
| xa' xb' xc' xd'|
B =| ya' yb' yc' yd'|
| za' zb' zc' zd'|
| 1 1 1 1 |
To find unknown M, we can multiply both sides of the expression by inverse of A matrix
M * A * Inv(A) = B * Inv(A)
M = B * Inv(A)
But solution is unique for non-complanar point quadruplet - in your case points lie in the same plane, so if solution exists, it is really a family of solutions and you have to choose arbitrary one. (Perhaps angles will be defined unambiguously)
I have a transformation matrix constructed as
H = Rz * Ry * Rx. So rotations are performed in xyz order.
Now, given rotation angles around x, y, and z axes, is there a way to find rotation angles to perform inverse operation, such that
v = Rz * Ry * Rx * v0
v0 = Rz' * Ry' * Rx' * v
Just for completion sake. In the end I extracted the Euler angles from transformation matrix as described in:
Computing Euler angles from a rotation matrix - Gregory G. Slabaugh
If your matrices are purely rotation (i.e. no translation), the inverse is simply the transpose:
R-1 = RT
If your transformation includes translation like so:
A =
| R T |
| 0 1 |
Then use the transpose of the rotation matrix as above and for the translation portion, use:
T-1 = -RTT
Then
A-1 =
| R-1 T-1 |
| 0 1 |
Also note that you will have to do the inverse rotations in the inverse order:
v0 = Rx-1 * Ry-1 * Rz-1 * v
I have a collection of 3D points which form an imperfect circle and are stored in the order in which they appear in the circle. I'm positioning an object in the centre of the ring by calculating the mean position of all of the points, which works fine. Now, what I want is for the object in the centre to be facing up/down relative to the rest of the points (i.e perpendicular to the ring).
I've included an image to help clarify what I mean. Does anyone know of an algorithm that would be suitable for this?
You have to compute the plane that your points form and get its normal.
If the points are perfectly coplanar, just get three of them, a, b, and c, and compute two vectors. The normal vector n is the cross product of them:
v1 = b - a;
v2 = c - a;
n = v1 x v2;
If the points are not perfectly coplanar, you can get the plane that best fits the points and then, its normal. You can get the plane by solving a linear equation system of the form Ax=0. Since the general equation of a plane is Ax + By + Cz + D = 0, you get one equation per 3D point, obtaining this system:
| x1 y1 z1 1 | | A | | 0 |
| x2 y2 z2 1 | x | B | = | 0 |
| x3 y3 z3 1 | | C | | 0 |
| ... | | D | | ... |
| xn yn zn 1 | | 0 |
The normal vector is (A, B, C).
Elaborating on a previous answer, you get a system of n equations in 4 unknowns when you solve for the best hyperplane normal vector with n points. You need to set one of the unknown coefficients (say D) to a constant like 1 and move the corresponding data column to the right hand side so that you don't get the trivial solution A=B=C=D=0. You can safely set one coefficient to 1 if it is non-zero because the solution A,B,C,D is still a solution if you scale it. So you get a system of n equations in 3 unknowns where the right hand side is a vector of all -1 instead of the zero vector, and your data matrix is simply your matrix of points and your unknown coefficients are A,B,C. In general such a system is overdetermined if you have more than 3 points so you need to solve it using linear regression. See http://en.wikipedia.org/wiki/Linear_regression to get the matrix formula for solving for the 3 coefficients using least squares best fit.
I have a 2D polygon and a 2D transformation matrix M that I use to transform the vertices of the polygon. The matrix may describe...
rotation around z axis,
scaling along x and y,
sheering along x and y axis,
translation along x and y axis.
Since we are in 2D the transformation matrix is of type 3x3. Here as an example a translation matrix by vector t and a rotation by angle a:
M_t = |1 0 t2| M_r = | cos(a) sin(a) 0|
|0 1 t1| |-sin(a) cos(a) 0|
|0 0 1 | | 0 0 1|
In my custom framework I don't have any access to the matrix values but can apply other matrixes in a row:
vertice = ( M_r * M_t ) * vertice
The above formula rotates the vertice around (0, 0) by angle a and then translates it by vector t. I know that matrix multiplications aren't commutative. So the order of multiplications is important.
My problem is now that I want to get a transformation matrix N that reflects a rotation R around the new center of the polygon, followed by a translation T, after applying an unknown transformation matrix M. Or in other words: I want to rotate and translate the polygon relative to its position and rotation given by M.
I can imagine this way of doing it, incorporating an unknown rotation and translation as part of M:
N = R * M * T
My questions are:
Is that mathematically correct?
What about an unknown sheering and scaling as part of M?
Is there a better way of doing this?
I dunno if this should go in a Math forum or a Programming forums, but I'll post it in both and see where I get.
I have two computer images... one of them is an "original" image (a large TIF file). The other one is a transformed version of the original image... it's been rotated, sheared and translated in a software program. I need to do some work on the transformed image, but I need the (x-y) coordinates of each pixel in the original image to finish my calculations.
I know that the image was rotated and sheared with a 3x3 Transformation matrix. If I had the matrix, I could derive the second image from the first (or vice-versa) myself. I don't know exactly how much it was rotated, sheared, or translated, so I can't just derive the matrices from a set of known transformations. What I do have is a set of corresponding points (the corners, et al) in each image, and their corresponding (x,y) coordinates. So here's my dilemma:
Using a set of corresponding transformed points ((x,y) -> (x',y'), three or more of them), can I derive the Transformation matrix that was used to turn one image into the other? If I can derive the matrix, I can solve for the original coordinates of all the pixels (all 18-million of 'em) and get the calculations done that I need to do.
Can anyone help? I'm familiar with linear algebra... just not familiar enough to derive this without a whole lotta head scratching. Anything is appreciated!
Mike
Not sure if you want manual or automatic.
Manual
If you specify the transformed coordinates of the four corners of your rectangle, then you can derive the transformation equations:
x' = c1 * x + c2 * y + c3 * x * y + c4
y' = c5 * x + c6 * y + c7 * x * y + c8
(From Pierre Wellner's Interacting with Paper on the DigitalDesk, page 67)
Now you just have to solve for the coefficients of the equation.
With four point pairs, the two sets of four simultaneous linear equations can be quickly solved by Gaussian Elimination to find the values of c1-8.
Lastly, you can turn those equations into the 3x3 matrices you want. The above equations are powerful enough to do non-linear transformations and you can simplify it into the 3x3 affine shear matrix.
But I would just stick with the nonliner equations (above) since they can handle perspective distortion.
Automatic
Same method, but you can use an edge-detector comboined with a line detection algorithm to find a set of 4-ish lines that makeup a rectangle.
If your image rectangles really stand out (whiteish images on a dark background), then you can use corner detection available from libraries like OpenCV's Feature Detection (see cv::cornerHarris).
You can intersect those lines to find the four corners and use the transformation equation.
I think you should start by providing a list of, say 3 points (for 6 unknowns) with X/Y coordinates before and after transformation.
Then somebody more clever than I should pop that into a set of linear equations and then feed it to (say) Wolfram Alpha for solving.
The top of Java's documentation for AffineTransform shows how the matrix needs to be set up:
[ x'] [ m00 m01 m02 ] [ x ] [ m00 x + m01 y + m02 ]
[ y'] = [ m10 m11 m12 ] [ y ] = [ m10 x + m11 y + m12 ]
[ 1 ] [ 0 0 1 ] [ 1 ] [ 1 ]
Removing most of the fluff leaves:
[ x'] [ m00 x + m01 y + m02 ]
[ y'] = [ m10 x + m11 y + m12 ]
Then you just set up a set of 6 x 2 equations like this:
m00 x + m01 y + m02 - x' = 0
m10 x + m11 y + m12 - y' = 0
(repeat for 2 other x/y before/after pairs)
and throw them at an equation solver.
You only need 3 points to define a 3x3 transformation matrix. If you have the points (0,0), (0,1) and (1,0) and transform them by the matrix [a b c d e f 0 0 1], you'll get (c,f), (b,e) and (a,d).