python图片拼接:如何使用OpenCV连接图像?

2021年11月7日19:33:28 发表评论 5,001 次浏览

如何使用OpenCV连接图像?为了使用 Python 垂直和水平连接图像,cv2 库提供了两个函数实现python图片拼接

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

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

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

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

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

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

输出:python图片拼接:如何使用OpenCV连接图像?

python图片拼接 - 水平连接: cv2.hconcat() 用于水平连接相同高度的图像。

# 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] 代表宽度。


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

输出:python图片拼接:如何使用OpenCV连接图像?

vconcat_resize.jpg

水平拼接不同高度的图片: 用于拼接不同高度的图片,如下Python合并多张图片实现代码:

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

输出:python图片拼接:如何使用OpenCV连接图像?

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

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

输出:python图片拼接:如何使用OpenCV连接图像?

python图片拼接 - 在垂直和水平平铺中连接不同大小的图像:上面定义的调整大小和连接功能用于在垂直和水平平铺中组合不同大小的图像。

下面是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)

输出:python图片拼接:如何使用OpenCV连接图像?

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: