Friedrich-Alexander-Universität
Friedrich-Alexander-Universität
Stanford Radiology

Rotate a 2D image

Here is a simple example to show how to rotate a 2D image.

 

import edu.stanford.rsl.conrad.geometry.Rotations;

import edu.stanford.rsl.conrad.geometry.transforms.ScaleRotate;

import edu.stanford.rsl.conrad.numerics.SimpleMatrix;

 

int imgSize=512;

//Create a phantom

Phantom phan = new SheppLogan(imgSize, false);

//reset the origin for the image as the rotation axis

phan.setOrigin(-imgSize/2, -imgSize/2);

//the angle to rotate, in radians unit

float angle=0.1745f;

//create the rotation matrix in 3D form

SimpleMatrix rotation = Rotations.createBasicZRotationMatrix(angle);

//in 2D case we only need 2 by 2 submatrix

ScaleRotate rot = new ScaleRotate(rotation.getSubMatrix(2, 2));

//Apply the rotation transform

phan.applyTransform(rot);

//Set the origin back

phan.setOrigin(imgSize/2, imgSize/2);