Image rotation using OpenCV
by vigneshchennai74 Updated: Feb 17, 2023
Attempting to create a new image from an input image by swapping the positions of the rows and columns using nested loops, and then writing the resulting image to a file using the OpenCV library
OpenCV (Open Source Computer Vision) and NumPy are two powerful libraries in Python that are widely used in computer vision, image processing, and machine learning applications. Here's a brief overview of how each library can be used. OpenCV provides a variety of computer vision algorithms and functions for image and video processing.
- These functions range from basic image filtering, resizing, and rotation to advanced feature detection, object recognition, and video analysis.
- OpenCV can read and write a variety of image and video formats, making it easy to work with different types of media.
- OpenCV has interfaces for several programming languages, including
h, w, c where h, w, and c represent the height, width, and number of color channels in the new array. A nested loop is a loop inside another loop. It is a common programming construct used to iterate over multiple levels of data, such as two-dimensional arrays or matrices. cv2.imwrite is a function provided by the OpenCV library that is used to write an image to a file on a disk. The function takes two arguments: the filename of the image to be saved, and the image data to be written.
Here is the example how to rotate the image:
Preview of the output that you will get on running this code from your IDE
In this solution we use the Imread function of the OpenCV.
img = cv2.imread('texas_longhorns_log.png') h,w,c = img.shape empty_img = np.zeros([h,w,c], dtype=np.uint8) for i in range(h): for j in range(w): empty_img[i,j] = img[j-1,i-1] empty_img = empty_img[0:h,0:w] cv2.imwrite("tester1.png", empty_img) h,w,c = img.shape empty_img = np.zeros([h,w,c], dtype=np.uint8) for i in range(h): for j in range(w): empty_img[i,j] = img[h-j-1,w-i-1] empty_img = empty_img[0:h,0:w] cv2.imwrite("tester2.png", empty_img) h,w,c = img.shape empty_img = np.zeros([h,w,c], dtype=np.uint8) for i in range(h): for j in range(w): empty_img[i,j] = img[h-i-1,w-j-1] empty_img = empty_img[0:h,0:w] cv2.imwrite("tester3.png", empty_img)
- Copy the code using the "Copy" button above, and paste it in a Python file in your IDE.
- Import open Cv and Numpy library
- Modify the name, location of the image to be rotate in the code.
- Run the file to rotate the image.
I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.
i found this code snippet by searching for "Image rotation using OpenCV" in kandi. You can try any such use case!
The fundamental package for scientific computing with Python.
Python 23587 Version:v1.24.3 License: Permissive (BSD-3-Clause)
I tested this solution in the following versions. Be mindful of changes when working with other versions
- The solution is created and executed in python version 3.7.15 .
- The solution is tested on OpenCV 4.6.0
Using this solution, we are able to rotate an image using the OpenCv library in Python with simple steps. This process also facilities an easy to use, hassle free method to create a hands-on working version of code which would help us rotate an image in Python
- For any support on kandi solution kits, please use the chat
- For further learning resources, visit the Open Weaver Community learning page.