Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good way to rotate a pixel array?

Tags:

c#

.net

Does the .NET framework provide a good way to rotate a pixel array? To be more clear, I am not just looking for a way to rotate an image. Instead I want to rotate the underlying pixel array that the image is/will be based on in such a way that I have access to the modified array. I would rather not implement a rotation algorithm or wrap another library if the .NET framework already provides a good way to do this. I need to be able to support any rotations, not merely 90/180/270/etc. rotations, which would be simple to implement.

I have thought about using one of the image classes that supports this type of rotation and accessing the modified array from it afterward. I am not sure if this is a good idea in the first place and also not sure which image class would be the best choice for this. So far I haven't had much luck finding a class or combination of classes that supports the rotations I need and enables access to the pixel array.

Update:

I'm starting to think I either asked the wrong question or asked the right question poorly. If I understand what Cosmin meant about using the Matrix class correctly, I don't think this will work for what I am actually trying to do. I'll try to do a better job of explaining my problem.

Basically, I have a 2D array of data and I want to rotate it. What I mean by this is that I want the values of the elements to be rotated to the appropriate elements based on the rotation.

For example:

-----------
|A|A|A|A|A|
-----------
|B|B|B|B|B|
-----------
|C|C|C|C|C|
-----------

If I were to rotate this array around the center element by 90 degrees counterclockwise, I should end up with something like this:

-----------
| |A|B|C| |
-----------
| |A|B|C| |
-----------
| |A|B|C| |
-----------

Unfortunately, I have to support any rotation, not just 90 degree rotations, which means a more complicated rotation algorithm is needed, since the rotated element indexes won't always be integers and may overlap as well. This seems like a problem that has probably been solved in the field of computer graphics, which led to me ask the question the way I did.

like image 487
foven Avatar asked Mar 08 '11 15:03

foven


People also ask

How do you rotate a pixel in Java?

The simplest way to rotate an image in Java is to use the AffineTransformOp class. You can load an image into Java as a BufferedImage and then apply the rotate operation to generate a new BufferedImage. You can use Java's ImageIO or a third-party image library such as JDeli to load and save the image.

How do I rotate an image in Matlab?

J = imrotate( I , angle ) rotates image I by angle degrees in a counterclockwise direction around its center point. To rotate the image clockwise, specify a negative value for angle . imrotate makes the output image J large enough to contain the entire rotated image.


1 Answers

Yes, you can use the Matrix class: http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.matrix.aspx

like image 70
Cosmin Avatar answered Oct 16 '22 22:10

Cosmin