📜  在Python中使用 OpenCV 连接图像

📅  最后修改于: 2022-05-13 01:55:12.105000             🧑  作者: Mango

在Python中使用 OpenCV 连接图像

为了使用Python垂直和水平连接图像,cv2 库带有两个函数:

  1. hconcat():用作 cv2.hconcat() 水平拼接图像。这里 h 表示水平。
  2. vconcat():它被用作 cv2.vconcat() 来垂直连接图像。这里 v 表示垂直。

在数组上使用 hconcat() 和 vconcat() 实现

传递作为 n 维数组的图像列表,其中列表中的图像垂直或水平连接。可以调整不同大小的图像大小。以下连接图像的方法通过下面的代码解释为:

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


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


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


Python3
# 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)


Python3
# 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)


Python3
# 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)


Python3
# 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)


垂直连接: cv2.vconcat() 用于垂直组合相同宽度的图像。

Python3

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

输出:

sea_image.jpg

水平连接: cv2.hconcat() 用于水平组合相同高度的图像。

Python3

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

输出:

man_image.jpeg

垂直拼接不同宽度的图像:用于组合不同宽度的图像。这里 shape[0] 代表高度,shape[1] 代表宽度。

Python3

# 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)

输出:

vconcat_resize.jpg

水平拼接不同高度的图像:用于组合不同高度的图像。

Python3

# 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)

输出:

hconcat_resize.jpg

垂直和水平连接相同大小的图像:可以使用 cv2.hconcat() 和 cv2.vconcat() 使用 2D 列表以平铺形式组合图像。

Python3

# 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)

输出:

concat_vh.jpg

将不同大小的图像拼接在垂直和水平的瓦片中:上面定义的调整大小和连接函数用于在垂直和水平瓦片中组合不同尺寸的图像。

Python3

# 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)

输出:

concat_tile_resize.jpg