Life2Coding
How to Display Multiple Images in One Window using OpenCV Python

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 a detail explanation related to the OpenCV functions.

Requirements:

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 display multiple images in one window using 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

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:

  • Load the Multiple images using cv2.imread()
  • Then concatenate the images using np.concatenate()
  • In order to concatenate horizontally we need to use axis=1
  • And if we want to concatenate vertically then we need to use axis=0
  • Display all the images 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
#Read First Image
imgBGR=cv2.imread('hanif.jpg')
#Read Second Image
imgRGB=cv2.imread('hanif2.jpg')

cv2.imshow('bgr',imgBGR)
cv2.imshow('rgb',imgRGB)
#concatanate image Horizontally
img_concate_Hori=np.concatenate((imgBGR,imgRGB),axis=1)
#concatanate image Vertically
img_concate_Verti=np.concatenate((imgBGR,imgRGB),axis=0)
cv2.imshow('concatenated_Hori',img_concate_Hori)
cv2.imshow('concatenated_Verti',img_concate_Verti)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

5cd8bda898b61 How to Display Multiple Images in One Window using OpenCV Python
life2coding_icon [] How to Display Multiple Images in One Window using OpenCV Python

One thought on “How to Display Multiple Images in One Window using OpenCV Python

  1. Rashika

    Suppose i wrote something on white image…after sometime if condition satisfies i want erase that. Word and replace that place with new word

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.