Rotating images with OpenCV is easy, but sometimes simple rotation tasks cropped/cut sides of an image, which leads to a half image. Now, In this tutorial, We will explore a solution to safely rotate an image without cropping/cutting sides of an image so that the entire image will include in rotation, and also compare the conventional rotation method with the modified rotation version.
- In-order to rotate an image without cutting off sides, we will create an explicit function named ModifedWay() which will take the image itself and the angle to which the image is to be rotated as an argument.
- In the function, first, get the height and width of the image.
- Locate the center of the image.
- Then compute the 2D rotation matrix
- Extract the absolute sin and cos values from the rotation matrix.
- Get the new height and width of the image and update the values of the rotation matrix to ensure that there is no cropping.
- Finally, use the wrapAffine() method to perform the actual rotation of the image.
Below is the implementation of the approach:
Below are some examples which depict how to rotate an image without cutting off sides using the above function:
Below is the implementation of the modified rotation version along with its comparison with normal rotation version:
Here is another example that depicts the modern rotation method:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.