Rotate2d
Applies a rotation of a given angle on a two-dimensional image around a given point or the center of the image.
Access to parameter description
For an introduction: section 2D Rotations.
This algorithm performs a rotation of an image by a user-defined angle θ and a center C.
The new coordinates (x′,y′) can be expressed as a function of the old coordinates (x,y): {x′=(x−x0)⋅cosθ−(y−y0)⋅sinθ+x0y′=(x−x0)⋅sinθ+(y−y0)⋅cosθ+y0} where (x0,y0) are the coordinates of the center of the rotation. Or in matrix notation: [x′y′]=[cosθ−sinθsinθcosθ]⋅[x−x0y−y0]+[x0y0] If the dimensions of the output image are the same as those of the input, the destination pixels may be outside the image, and these pixels are then ignored. An output resizing option automatically modifies the dimensions of the image to make all the input data visible in the output image. This option is enabled by default.
On the other hand, some pixels in the output image may not have a corresponding pixel in the input image. In this case, a user-defined padding value is assigned to these pixels.
Furthermore, the (x,y) coordinates obtained are not always integers, even though an image is a discrete space. The following figure illustrates the grid of the resultant image, the point M′ being a pixel in this image. The points are generated from the rotation of the original image and do not fit on the grid.

Figure 1. Image grid rotation
To calculate the intensity of each pixel M′, two methods are possible:
If dj denotes the distance M′jM′, fi is defined as: fi=j=4∏j=1j≠idj e.g., f1=d2d3d4.
The choice of the fi's ensures that the interpolated value is equal to g′j if M′ matches M′j. This method gives better results but requires more computation time.
Figure 1. Centered 30 degrees rotation of a 1024x1024 image: (a) the input image,
(b) rotation without output resizing (1024x1024 image), (c) rotation with output resizing (1398x1398 image).
See also
Access to parameter description
For an introduction: section 2D Rotations.
This algorithm performs a rotation of an image by a user-defined angle θ and a center C.
The new coordinates (x′,y′) can be expressed as a function of the old coordinates (x,y): {x′=(x−x0)⋅cosθ−(y−y0)⋅sinθ+x0y′=(x−x0)⋅sinθ+(y−y0)⋅cosθ+y0} where (x0,y0) are the coordinates of the center of the rotation. Or in matrix notation: [x′y′]=[cosθ−sinθsinθcosθ]⋅[x−x0y−y0]+[x0y0] If the dimensions of the output image are the same as those of the input, the destination pixels may be outside the image, and these pixels are then ignored. An output resizing option automatically modifies the dimensions of the image to make all the input data visible in the output image. This option is enabled by default.
On the other hand, some pixels in the output image may not have a corresponding pixel in the input image. In this case, a user-defined padding value is assigned to these pixels.
Furthermore, the (x,y) coordinates obtained are not always integers, even though an image is a discrete space. The following figure illustrates the grid of the resultant image, the point M′ being a pixel in this image. The points are generated from the rotation of the original image and do not fit on the grid.

Figure 1. Image grid rotation
To calculate the intensity of each pixel M′, two methods are possible:
- Take the gray level of the nearest neighbor. For the above example M′ would be given the intensity of M′1:g′=g′1
- Take into account the four nearest neighbors and calculate the gray level based on the average of the four points, weighted by their distance, as in: g′=f1×g′1+f2×g′2+f3×g′3+f4×g′4f1+f2+f3+f4
If dj denotes the distance M′jM′, fi is defined as: fi=j=4∏j=1j≠idj e.g., f1=d2d3d4.
The choice of the fi's ensures that the interpolated value is equal to g′j if M′ matches M′j. This method gives better results but requires more computation time.
![]() (a) |
![]() (b) |
![]() (c) |
(b) rotation without output resizing (1024x1024 image), (c) rotation with output resizing (1398x1398 image).
See also
Function Syntax
This function returns outputImage.
// Function prototype
std::shared_ptr< iolink::ImageView > rotate2d( std::shared_ptr< iolink::ImageView > inputImage, double rotationAngle, Rotate2d::CenterMode centerMode, iolink::Vector2d rotationCenter, Rotate2d::InterpolationType interpolationType, bool outputResizing, double paddingValue, std::shared_ptr< iolink::ImageView > outputImage = nullptr );
Class Syntax
Parameters
Parameter Name | Description | Type | Supported Values | Default Value | |||||
---|---|---|---|---|---|---|---|---|---|
![]() |
inputImage |
The input image. | Image | Binary, Label, Grayscale or Multispectral | nullptr | ||||
![]() |
rotationAngle |
The angle of the rotation in degrees. | Float64 | Any value | 90 | ||||
![]() |
centerMode |
The way to define the rotation center
|
Enumeration | IMAGE_CENTER | |||||
![]() |
rotationCenter |
The rotation center coordinates. This parameter is ignored in IMAGE_CENTER mode. | Vector2d | Any value | {0.f, 0.f} | ||||
![]() |
interpolationType |
The interpolation mode. Method used to calculate the intensity of each pixel in the result image.
|
Enumeration | NEAREST_NEIGHBOR | |||||
![]() |
outputResizing |
Resize the output image to make all input data visible in the output image if set to True. Preserve the input dimension if set to False. | Bool | true | |||||
![]() |
paddingValue |
The background value for pixels with no correpondant point in the input image. | Float64 | Any value | 0 | ||||
![]() |
outputImage |
The output image. Its type is forced to the same values as the input. Its dimensions are identical to or greater than the input image, according to the outputResizing parameter. | Image | nullptr |
Object Examples
auto polystyrene = ioformat::readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "polystyrene.tif" ); Rotate2d rotate2dAlgo; rotate2dAlgo.setInputImage( polystyrene ); rotate2dAlgo.setRotationAngle( 40.5 ); rotate2dAlgo.setCenterMode( Rotate2d::CenterMode::IMAGE_CENTER ); rotate2dAlgo.setRotationCenter( {15.5, 32} ); rotate2dAlgo.setInterpolationType( Rotate2d::InterpolationType::NEAREST_NEIGHBOR ); rotate2dAlgo.setOutputResizing( true ); rotate2dAlgo.setPaddingValue( 0 ); rotate2dAlgo.execute(); std::cout << "outputImage:" << rotate2dAlgo.outputImage()->toString();
Function Examples
auto polystyrene = ioformat::readImage( std::string( IMAGEDEVDATA_IMAGES_FOLDER ) + "polystyrene.tif" ); auto result = rotate2d( polystyrene, 40.5, Rotate2d::CenterMode::IMAGE_CENTER, {15.5, 32}, Rotate2d::InterpolationType::NEAREST_NEIGHBOR, true, 0 ); std::cout << "outputImage:" << result->toString();