How to Stitch Images in Python with OpenCV

How do I connect images using OpenCV? In order to use Python to connect images vertically and horizontally, the cv2 library provides two functions to implement python image stitching:

  1. hconcat(): Used as a cv2.hconcat() horizontally connected image. Here h denotes the level.
  2. vconcat(): Used as a cv2.vconcat() vertically connected image. Here v denotes vertical.

Use hconcat() and vconcat() on arrays

Python Merge Multiple Pictures Implementation: Pass a list of images as an n-dimensional array, where the images in the list are connected vertically or horizontally. Images can be resized in different sizes. The following code explains how to connect images:

# import cv2 library
import cv2
  
# read the images
img1 = cv2.imread('sea.jpg')
img2 = cv2.imread('man.jpeg')

How do I connect images using OpenCV? Vertical connection: cv2.vconcat() is used to vertically combine images of the same width.

# vertically concatenates images 
# of same width 
im_v = cv2.vconcat([img1, img1])
  
# show the output image
cv2.imshow('sea_image.jpg', im_v)

Output:

Python image stitching – Horizontal joining: cv2.hconcat() is used to horizontally connect images of the same height.

# horizontally concatenates images
# of same height 
im_h = cv2.hconcat([img2, img2])
  
# show the output image
cv2.imshow('man_image.jpeg', im_h)

Output:

man_image.jpeg

Stitch images of different widths vertically: Used to stitch images of different widths. Here shape[0] stands for height and shape[1] stands for width.


# define a function for vertically 
# concatenating images of different
# widths 
def vconcat_resize(img_list, interpolation 
                   = cv2.INTER_CUBIC):
      # take minimum width
    w_min = min(img.shape[1] 
                for img in img_list)
      
    # resizing images
    im_list_resize = [cv2.resize(img,
                      (w_min, int(img.shape[0] * w_min / img.shape[1])),
                                 interpolation = interpolation)
                      for img in img_list]
    # return final image
    return cv2.vconcat(im_list_resize)
  
# function calling
img_v_resize = vconcat_resize([img1, img2, img1])
  
# show the output image
cv2.imwrite('vconcat_resize.jpg', img_v_resize)

Output:

vconcat_resize.jpg

Stitching pictures of different heights horizontally: Used to stitch pictures of different heights, the following Python implementation code is used to merge multiple images:

# define a function for horizontally 
# concatenating images of different
# heights 
def hconcat_resize(img_list, 
                   interpolation 
                   = cv2.INTER_CUBIC):
      # take minimum hights
    h_min = min(img.shape[0] 
                for img in img_list)
      
    # image resizing 
    im_list_resize = [cv2.resize(img,
                       (int(img.shape[1] * h_min / img.shape[0]),
                        h_min), interpolation
                                 = interpolation) 
                      for img in img_list]
      
    # return final image
    return cv2.hconcat(im_list_resize)
  
# function calling
img_h_resize = hconcat_resize([img1, img2, img1])
  
# show the Output image
cv2.imshow('hconcat_resize.jpg', img_h_resize)

Output:

How do I connect images using OpenCV? Connect images of the same size vertically and horizontallyYou can use cv2.hconcat() and cv2.vconcat() to combine images in a tiled form using a 2D list.

# define a function for vertically 
# concatenating images of the 
# same size  and horizontally
def concat_vh(list_2d):
    
      # return final image
    return cv2.vconcat([cv2.hconcat(list_h) 
                        for list_h in list_2d])
# image resizing
img1_s = cv2.resize(img1, dsize = (0,0),
                    fx = 0.5, fy = 0.5)
  
# function calling
img_tile = concat_vh([[img1_s, img1_s, img1_s],
                      [img1_s, img1_s, img1_s],
                      [img1_s, img1_s, img1_s]])
# show the output image
cv2.imshow('concat_vh.jpg', img_tile)

Output:

Python Image Stitching – Connect images of different sizes in vertical and horizontal tiles: The resize and connect features defined above are used to combine images of different sizes in vertical and horizontal tiles.

Here’s the detailed code for merging multiple images in Python:

# define a function for concatenating
# images of different sizes in
# vertical and horizontal tiles
def concat_tile_resize(list_2d, 
                       interpolation = cv2.INTER_CUBIC):
      # function calling for every 
    # list of images
    img_list_v = [hconcat_resize(list_h, 
                                 interpolation = cv2.INTER_CUBIC) 
                  for list_h in list_2d]
      
    # return final image
    return vconcat_resize(img_list_v, interpolation=cv2.INTER_CUBIC)
  
# function calling
im_tile_resize = concat_tile_resize([[img1],
                                     [img1, img2,
                                      img1, img2, img1],
                                     [img1, img2, img1]])
# show the image
cv2.imshow('concat_tile_resize.jpg', im_tile_resize)

Output: