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 ceate a multicolor pattern background image using OpenCV in Python
Documentation:
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:
- First we will create a image array using np.zeros()
- After that for create a pattern, we will divide the whole image by horizontally or vertically
- We will make divide it in 3 parts and then put three colors in the 3 parts of the image
- Then display the image using cv2.imshow()
- Wait for keyboard button press using cv2.waitKey()
- Exit window and destroy all windows using cv2.destroyAllWindows()
Example Code:
import cv2 import numpy as np height=512 width=512 blank_image = np.zeros((height,width,3), np.uint8) blank_image2 = np.zeros((height,width,3), np.uint8) blank_image[:,0:width//3] = (255,0,0) # (B, G, R) blank_image[:,width//3:2*width//3] = (0,255,0) blank_image[:,2*width//3:width] = (0,0,255) cv2.imshow('Pattern 1 Window', blank_image) blank_image2[0:height//3,:] = (255,0,0) # (B, G, R) blank_image2[height//3:2*height//3,:] = (0,255,0) blank_image2[2*height//3:height,:] = (0,0,255) cv2.imshow('Pattern 2 Window', blank_image2) cv2.waitKey(0) cv2.destroyAllWindows()
Output:
Latest posts by Life2Coding (see all)
- 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