Image processing (Rotation algorithms)

Default featured post

Again this post is about one of the image processing algorithm. In the last related post to image processing, negative algorithm was discussed and in this post rotation algorithm will be explained.

Firstly, rotation has different angles such as 180, 90 L, 90 R, and so on, The procedure of all rotation algorithms are quite similar to each other. If you understand and apply one algorithm successfully, applying the rest rotation algorithm can be done easily and very fast.

In all rotation algorithms firstly one pixel of the image is read and then that pixel is written in another position.

For instance, in 180 degree rotation picked pixels from top should be written in down and pixels from left should be written in right.

In 90L (left) rotation a pixel is read from position X, Y and that pixel should be written in position (Y, X - Width_Counter).

In 90 R (right) rotation the position of the pixels will be changed like 90 L but this time instead deducting Width - Counter from X, High - Counter should be subtracted from Y. In other word a pixel should be written in (Y - High_Counter, X).

The following sample code contains all mentioned rotation algorithms.

private void rotate180(PictureBox picImp) {
  for (int i = 1; i <= x.Width - 1; i++) {
    for (int j = 1; j <= x.Height - 1; j++) {
      z = x.GetPixel(i, j);
      y.SetPixel(x.Width - i, x.Height - j, z);
    }
  }
  Message.setMessage(PicSave.savechange(picImp, y), "Rotate 180");
}
private void rotateL90(PictureBox picImp) {
  for (int i = 1; i <= x.Width - 1; i++) {
    for (int j = 1; j <= x.Height - 1; j++) {
      z = x.GetPixel(i, j);
      y.SetPixel(j, x.Width - i, z);
    }
  }
  Message.setMessage(PicSave.savechange(picImp, y), "Rotate Left 90");
}
private void rotateR90(PictureBox picImp) {
  for (int i = 1; i <= x.Width - 1; i++) {
    for (int j = 1; j <= x.Height - 1; j++) {
      z = x.GetPixel(i, j);
      y.SetPixel(x.Height - j, i, z);
    }
  }
  Message.setMessage(PicSave.savechange(picImp, y), "Rotate Right 90");
}