In this blog, we will learn how to create subplots using OpenCV-Python. We know that cv2.imshow() only shows 1 image at a time. Displaying images side by side helps greatly in analyzing the result. Unlike Matlab, there is no direct function for creating subplots. But since OpenCV reads images as arrays, we can concatenate arrays using the inbuilt cv2.hconcat() and cv2.vconcat() functions. After that, we display this concatenated image using cv2.imshow().
cv2.hconcat([ img1, img2 ]) —– horizontally concatenated image as output. Same for cv2.vconcat().
Below is the sample code where i displayed 2 gamma corrected images using this method
import numpy as np
# Load the image
img = cv2.imread('D:/downloads/forest.jpg')
# Perform Operations you want. Here, I did Gamma Correction of 2.2 and 0.4
gamma_two_point_two = np.array(255*(img/255)**2.2,dtype='uint8')
gamma_point_four = np.array(255*(img/255)**0.4,dtype='uint8')
# Horizontally concatenate the 2 images
img3 = cv2.hconcat([gamma_two_point_two,gamma_point_four])
# Display the concatenated image
The output looks like this
To put the text on images, use cv2.puttext() and if you want to leave spacing between the images shown, use cv2.copyMakeBorder(). You can play around with many other OpenCV functions.
Note: Array dimensions must match when using cv2.hconcat(). This means you cannot display color and greyscale images side by side using this method.
I hope this information will help you. Hope you enjoy reading.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.