Creating Subplots in OpenCV-Python

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

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.

Leave a Reply