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 combine or merge multiple images vertically using OpenCV python.
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
imshow()
None=cv.imshow(winname, mat) |
Displays an image in the specified window.
- Parameters
-
winname Name of the window. mat Image to be shown.
imwrite()
retval=cv.imwrite(filename, img[, params]) |
Saves an image to a specified file.
- Parameters
-
filename Name of the file. img Image to be saved. params Format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, … .) see cv::ImwriteFlags
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 list of images using cv2.imread()
- Get the max_width between all the images
- Also need to calcuate the max height by adding all the images’ height
- Then need to define the new image height using the max width and total height
- Also padding is added between the images which can be adjusted easily
- Display the combined image using cv2.imshow()
- Save the output image using cv2.imwrite()
- 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 def combine_vertically(image_names,padding=40): images = [] max_width = 0 # find the max width of all the images total_height = 0 # the total height of the images (vertical stacking) for name in image_names: # open all images and find their sizes img=cv2.imread(name) images.append(img) image_width=img.shape[1] image_height=img.shape[0] if image_width > max_width: max_width = image_width #add all the images heights total_height += image_height # create a new array with a size large enough to contain all the images # also add padding size for all the images except the last one final_image = np.zeros((total_height+(len(image_names)-1)*padding,max_width,3),dtype=np.uint8) current_y = 0 # keep track of where your current image was last placed in the y coordinate for image in images: # add an image to the final array and increment the y coordinate height = image.shape[0] width = image.shape[1] final_image[current_y:height+current_y,:width,:] = image # add the padding between the images current_y += height+ padding return final_image if __name__ == '__main__': image_names = ['./opencv.jpg','./python.jpg','./windows.jpg'] final_image=combine_vertically(image_names,padding=0) cv2.imshow('out',final_image) cv2.imwrite('vertical.PNG',final_image) cv2.waitKey(0) 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