How to Combine Images in OpenCV

Ammar Ali Feb 02, 2024
  1. Use the concatenate() Function of NumPy to Combine Images in Python
  2. Combine Images of Different Sizes in Python
  3. Combine Images Vertically in Python
How to Combine Images in OpenCV

This tutorial will discuss combining two images using Python’s concatenate() function of the NumPy in Python.

Use the concatenate() Function of NumPy to Combine Images in Python

We can read images using the imread() function of OpenCV and store them in a matrix. We can use the concatenate() function of NumPy to concatenate the matrices of the images along different axes.

For example, let’s use the zeros() function of NumPy to create two images with different colors and then combine them horizontally using the concatenate() function. See the code below.

import cv2
import numpy as np

img_1 = np.zeros((312, 312, 3), dtype=np.uint8)
img_1[:, :] = (255, 0, 0)

img_2 = np.zeros((312, 312, 3), dtype=np.uint8)
img_2[:, :] = (0, 255, 0)

img_3 = np.concatenate((img_1, img_2), axis=1)

cv2.imshow("Img_1", img_1)
cv2.imshow("Img_2", img_2)
cv2.imshow("Img_3", img_3)
cv2.waitKey(0)

Output:

combine images of same size

Note that the concatenate() function will only work if the two images have the same size. The first argument of the concatenate() function contains the images that we want to combine.

We only used two images, but we can use as many images as possible, and the concatenate() function will combine them. The axis argument specifies the orientation on which the images will be combined.

We combined the above images horizontally, but we can also combine them vertically using the axis=0 argument. The above code will only combine the images which have the same size, and it will give an error if we try to combine images of different sizes.

Combine Images of Different Sizes in Python

We can also combine two images of different sizes.

For example, to combine two images of different sizes horizontally, we have to create a blank image whose height will be equal to the maximum height from the two given images and width equal to the combined width of the given images.

We can use the shape property to get the height and width of the given images, and we will use them to create the blank images. After creating the blank image, we can give it a color of our own choice.

After that, we have to replace the blank image pixels with the pixels of the given image. For example, we will add the first image first, and then after that, we will add the second image.

If one of the images is small compared to the other image, the rest of the blank image will have the same color that we defined. For example, let’s combine a fruit image with a cat image.

See the code below.

import cv2
import numpy as np

img_1 = cv2.imread("fruit.jpg")

img_2 = cv2.imread("cat.jpg")

h1, w1 = img_1.shape[:2]
h2, w2 = img_2.shape[:2]

img_3 = np.zeros((max(h1, h2), w1 + w2, 3), dtype=np.uint8)
img_3[:, :] = (255, 255, 255)

img_3[:h1, :w1, :3] = img_1
img_3[:h2, w1 : w1 + w2, :3] = img_2

cv2.imshow("Img_1", img_1)
cv2.imshow("Img_2", img_2)
cv2.imshow("Img_3", img_3)
cv2.waitKey(0)

Output:

combine images of different size

The above code will add two images of different sizes horizontally, but we can also change the code to add two images vertically. We have to change the shape of the blank image first.

Combine Images Vertically in Python

In the above code, we specified the height as the maximum height from the two images and the width equal to the combined width of the two images.

If we want to combine the images vertically, we have to create a blank image with a height equal to the combined height of the two images and a width equal to the maximum width of the two images. After that, we also have to change the location of the second image on the blank image.

In the above code, we are adding the second image with a horizontal position starting from the width of the first image, but in this case, we will add the image starting from the height of the first image. For example, let’s combine the above two images vertically.

See the code below.

import cv2
import numpy as np

img_1 = cv2.imread("fruit.jpg")

img_2 = cv2.imread("cat.jpg")

h1, w1 = img_1.shape[:2]
h2, w2 = img_2.shape[:2]

img_3 = np.zeros((h1 + h2, max(w1, w2), 3), dtype=np.uint8)
img_3[:, :] = (255, 255, 255)

img_3[:h1, :w1, :3] = img_1
img_3[h1 : h1 + h2, :w2, :3] = img_2

cv2.imshow("Img_1", img_1)
cv2.imshow("Img_2", img_2)
cv2.imshow("Img_3", img_3)
cv2.waitKey(0)

Output:

combine images vertically

We can see in the above output that the two images are combined vertically.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - OpenCV Image