Posted Friday, June 17, 2005 1:53 PM
|
|
|
|
Hi again :-)
My next problem is to rotate as fast as possible a 512 square matrix, around its center,
for a given angle.
After some attempts using cosinus and for...next loops, I wonder if something easier and
faster exists in native bluebit functions ?
Thx for any idea about ...
el doud
|
|
Posted Saturday, June 18, 2005 3:15 AM
|
|
|
|
Hi el doud,
Did you try using the RotateTransform method of the System.Drawing.Graphics object? That is part of the .NET Framework.
In case you need to have your image data in Matrix object, the problem here is that you don't need to calculate new values for the data itself but new locations (x,y coordinates) for the data. If there will be a fixed number of rotation angles (eg every 5 degrees) you can precalculate the new locations and then move every pixel to its new location without recalculating.
Another alternative:
Create a 3 column matrix containing the x,y locations of every pixel in the original image:
Matrix A =
0 0 1
0 1 1
0 2 1
0 3 1
0 4 1
...
...
0 511 1
1 0 1
1 1 1
1 2 1
1 3 1
...
...
511 509 1
511 510 1
511 511 1
Multiply this matrix by a 3x3 transformation matrix. The resulted matrix will contain the new locations of every pixel in the rotated image.
There are many articles online of how to calculate the transformation matrix but here is what I can remember:
In order to rotate around the (0,0) point the transormation matrix will be:
Matrix B =
| cos(theta) sin(theta) 0 |
|-sin(theta) cos(theta) 0 |
| 0 0 1 |
"theta" above is the angle of rotation. In order to rotate around any other arbitary point (x,y) first transform the coordinates of the first matrix by multiply by this transformation matrix:
Matrix C =
| 1 0 0 |
| 0 1 0 |
|-x -y 1 |
At the end move all points to their original coordinates by multiplying by D:
Matrix D =
| 1 0 0 |
| 0 1 0 |
| x y 1 |
So the final result will be R = A*C*B*D. In order to save computation time first calculate E=(C*B*D) and then multiply by E. R = A*E.
Please note that the matrix R will contain not image data itself but the positions where the image data should be moved to.
Matrices A, C and D will be always the same and have to be created just once. Matrix B will be created for every rotation angle. It will be very fast to compute matrix E since the matrices C,B,D are very small (3x3). The only operation that will count in timings will be that of A*E.
Please let me know how it works.
Trifon Triantafillidis | Lead Developer |
|
|
|
|
|