Ask Your Question

Revision history [back]

Images are numpy arrays. Therefore you can use numpy capabilities to create an image contaning two images. The images to stack/concatenate should have the same size.

# Import required packages:
import cv2
import numpy as np

# Load images
image_1 = cv2.imread('lenna.png')
image_2 = image_1.copy()

# Eliminate blue and read channels:
image_2[:, :, 0] = 0
image_2[:, :, 2] = 0

# Stack horizontally:
# Equivalent to np.concatenate(tup, axis=1)
# images_1_2_h = np.concatenate((image_1, image_2), axis=1)
images_1_2_h = np.hstack((image_1, image_2))

# Stack vertically:
# Equivalent to np.concatenate(tup, axis=0)
# images_1_2_v = np.concatenate((image_1, image_2), axis = 0)
images_1_2_v = np.vstack((image_1, image_2))

# Show images:
cv2.imshow("images_1_2_h", images_1_2_h)
cv2.imshow("images_1_2_v", images_1_2_v)

# Wait until a key is pressed:
cv2.waitKey(0)

# Destroy all created windows:
cv2.destroyAllWindows()

image description image description