This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply them in various aspects using some great examples. Then the output will be visualized along with the comparisons.
We will also discuss the basic of image processing and provide the detail explanation related to the OpenCV functions.
Requirements:
- OpenCV 3.4+
- Python 3.6+
- Numpy
- Image, Webcam or Video input
- Documentation Source: OpenCV Official Documentation
First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV with Python 3
Goals:
The goal is to make you understand how to rotate image in opencv
Documentation:
imread()
retval=cv.imread(filename[, flags]) |
Loads an image from a file.
- Parameters
-
filename Name of file to be loaded. flags Flag that can take values of cv::ImreadModes
getRotationMatrix2D()
retval=cv.getRotationMatrix2D(center, angle, scale) |
Calculates an affine matrix of 2D rotation.
- Parameters
-
center Center of the rotation in the source image. angle Rotation angle in degrees. Positive values mean counter-clockwise rotation (the coordinate origin is assumed to be the top-left corner). scale Isotropic scale factor.
warpAffine()
dst=cv.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]) |
Applies an affine transformation to an image.
- Parameters
-
src input image. dst output image that has the size dsize and the same type as src . M \(2\times 3\) transformation matrix. dsize size of the output image. flags combination of interpolation methods (see InterpolationFlags) and the optional flag WARP_INVERSE_MAP that means that M is the inverse transformation ( \(\texttt{dst}\rightarrow\texttt{src}\) ). borderMode pixel extrapolation method (see BorderTypes); when borderMode=BORDER_TRANSPARENT, it means that the pixels in the destination image corresponding to the “outliers” in the source image are not modified by the function. borderValue value used in case of a constant border; by default, it is 0.
putText()
img=cv.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) |
Draws a text string.
- Parameters
-
img Image. text Text string to be drawn. org Bottom-left corner of the text string in the image. fontFace Font type, see HersheyFonts. fontScale Font scale factor that is multiplied by the font-specific base size. color Text color. thickness Thickness of the lines used to draw a text. lineType Line type. See LineTypes bottomLeftOrigin When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.
imshow()
None=cv.imshow(winname, mat) |
Displays an image in the specified window.
- Parameters
-
winname Name of the window. mat Image to be shown.
waitKey()
retval=cv.waitKey([, delay]) |
Waits for a pressed key.
- Parameters
-
delay Delay in milliseconds. 0 is the special value that means “forever”.
destroyAllWindows()
None=cv.destroyAllWindows() |
Destroys all of the HighGUI windows.
Steps:
- Open the image using cv2.imread()
- Get the height and width of the image using image.shape[:2]
- Get the rotation martrix using cv2.getRotationMatrix2D()
- Apply the rotation to the image using cv2.wardAffline()
- Display all the images using cv2.imshow()
- Wait for keyboard button press using cv2.waitKey() to check the ‘r’ key for changing the rotation in the loop.
- Check for escape key to get out of the while loop
- Exit window and destroy all windows using cv2.destroyAllWindows()
Example Code:
import cv2 image=cv2.imread('hanif.jpg') height, width=image.shape[:2] count=1 while True: rotation_matrix=cv2.getRotationMatrix2D((width/2, height/2),count*90,1) rotated_image=cv2.warpAffine(image,rotation_matrix,(width,height)) cv2.putText(image, 'Press "r" to rotate', (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1) cv2.imshow('original image',image) cv2.imshow('New image', rotated_image) key = cv2.waitKey(1) if key==ord('r'): count+=1 if count>4: count=1 elif key==27: #esc key is pressed break cv2.destroyAllWindows()
Output:
- How to Create a RGB Color Picker for Images using OpenCV Python - 22 April, 2022
- Combine Several Images Vertically with Padding using OpenCV Python - 21 April, 2022
- Combine Several Images Horizontally with Padding using OpenCV Python - 21 April, 2022