How to concatenate images in OpenCV?
Sometimes we need to concatenate images horizontally or vertically. OpenCV provides following two functions that can be used to perform such tasks:
- hconcat(): Concatenate images in horizontal direction
- vconcat(): Concatenate images in vertical direction
The signature of these two methods are given below:
cv2.hconcat(src[, dst])
cv2.vconcat(src[, dst])
The parameters of the above functions are described as below:
Parameter |
Description |
src
|
input array or vector of matrices. all of the matrices must have the same number of rows and the same depth.
|
dst
|
output array. It has the same number of rows and depth as the src, and the sum of cols of the src.
|
These two method will only work when both images have same dimensions. Let us look at an example that performs concatenation of two images. Code 1 shows the concatenation operations on two images with equal size.
Code 1 : Horizontal and Vertical Image Concatenation
1
2
3
4
5
6
7
8
9
10
11
12
13 | import cv2
import numpy as np
try:
path = r'F:\img\lena.jpg'
img = cv2.imread(path, cv2.IMREAD_COLOR)
out1 = cv2.hconcat([img,img])
out2 = cv2.vconcat([img,img])
cv2.imshow('Horizontal Concatenation',out1)
cv2.imshow('Vertical Concatenation',out2)
cv2.waitKey(0)
cv2.destroyAllWindows()
except:
print('Unable to read image.')
|
Line 1 and 2: We start by importing OpenCV and numpy libraries.
Line 4: Define path of the image to read.
Line 5: Read a single color image.
Line 6: Using hconcat() function, we concatenated the img two times in horizontal direction.
Line 7: Using vconcat() function, we concatenated the img two times in vertical direction.
Line 8: The image obtained for horizontal concatenation is shown in Figure 1.
Line 9: The image obtained for vertical concatenation is shown in Figure 2.
Figure 1: Result of horizontal concatenation.
Figure 2: Result of vertical concatenation.
Concatenate Images with different Sizes
If the images are of different size, then we need to define a custom function to deal with the different size. We can resize one of the images to make their size same and then apply concatenation. Let us write a function that can handle image of different size and then concatenate them. The following function shown in Code 2 can be used to concatenate two images of different sizes.
Code 2: Define a function for horizontal and vertical Image Concatenation
1
2
3
4
5
6
7
8
9
10 | import cv2
import numpy as np
def concat(img1,img2,type=0):
#type=0 mean horizontal
if img1.shape !=img2.shape:
img1 = cv2.resize(img1,(img2.shape[0],img2.shape[1]))
if type==0:
return cv2.hconcat([img1,img2])
else:
return cv2.vconcat([img1,img2])
|
In this code we created a function called concat() that takes three parameters:
Img1 and img2: Input images to concatenate
type: it indicates if we want to concatenate in horizontal or vertical direction (default type = 0 means concatenate in horizontal direction and type=1 means in vertical direction).
In Line 5, we check if the images have different width and height then we resize first image according to the size of the second image. Then apply either cv2.concat() or cv2.vconcat() function for concatenation.
The following code shows usage of the concat() function implemented as shown in Code-3:
Code 3: Horizontal or Vertical Image Concatenation for different size images
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | import cv2
import numpy as np
try:
pathLena = r'F:\img\lena.jpg'
pathFish = r'F:\img\fish.jpg'
lena = cv2.imread(pathLena, cv2.IMREAD_COLOR)
fish = cv2.imread(pathFish, cv2.IMREAD_COLOR)
out = concat(lena,fish)
cv2.imshow('Concatenation',out)
cv2.waitKey(0)
cv2.destroyAllWindows()
except Exception as e:
print(str(e))
print('Unable to read image.')
|
We read two images of different sizes and then passed to our previously defined function in Line 8. We did not pass third parameter, so by default it will concatenate in the horizontal direction. The function will resize the first image according to size of second image and then concatenate them. The result obtained for Code - 3 is shown Figure 3.
Figure 3: Original Image (left) is resized and horizontally concatenated with right image to form one image.
Similarly, by changing the parameter type =1 in line 8 for concat() function we get the result shown in Figure 4.
out = concat(lena,fish,1)
Figure 4: Original Image is resized and then vertically concatenated with fish image to form one image.