Kunal Kabi
Kunal Kabi

Reputation: 21

How to rotate an Image with nearest neighbor and bilinear interpolations without using any OpenCv library?

I want to know any basic mathematics based algorithm to rotate an image using the nearest neighbor and bilinear interpolation without using OpenCV library and imrotate.

The image won't be cropped after rotation and full image must be displayed.

Upvotes: 1

Views: 2471

Answers (1)

user1196549
user1196549

Reputation:

A rotation corresponds to an affine transformation of the coordinates and is easily described using matrix/vectors. It is no great deal to find the formulas on the Web.

Now the important thing to know, is that rather than taking the pixels of the original image an mapping them to the transformed image, you must work backwards.

Scan every pixel of the transformed image and by applying the inverse transform, find the corresponding coordinates in the original image. You need to do this using real coordinates.

Then

  • for the nearest-neighbor method, round the coordinates and just copy the source pixel value to the destination;

  • for the bilinear method, consider the four pixels around the obtained coordinates (you will perform a truncation to integer). Finally, compute the destination pixel as a bilinear combination of the four source pixels, using the fractional part of the coordinates as the weights to perform the interpolation.

Check the figures here: http://wtlab.iis.u-tokyo.ac.jp/wataru/lecture/rsgis/rsnote/cp9/cp9-7.htm

Upvotes: 2

Related Questions