Opencv 拼接图片
读取文件夹下图片,并拼接图片
代码示例
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import cv2
import numpy as np
import os
import os.path
def imgmatrix_from_dir(path):
"""将图像存储到矩阵内"""
shape_x, shape_y = 256, 144
img_nms = [img_name for img_name in os.listdir(path) if img_name.upper().endswith("JPG")]
print(len(img_nms))
image_arr = cv2.resize(cv2.imread(os.path.join(path, img_nms[0]), flags=cv2.IMREAD_COLOR), (shape_x, shape_y))
print(image_arr.size)
print(image_arr.shape)
for index, img_nm in enumerate(img_nms):
image1 = cv2.resize(cv2.imread(os.path.join(path, img_nm), flags=cv2.IMREAD_COLOR), (shape_x, shape_y))
if image1 is not None:
# 数组拼接
#img = np.vstack((img, img2)) # vstack按垂直方向,hstack按水平方向
image_arr = np.concatenate((image_arr, image1), axis=1)
# 数组切片
return image_arr[:,shape_x:,:]
if __name__ == '__main__':
img_dir = r"F:\tte\test\mix"
img_martix = imgmatrix_from_dir(img_dir)
print(img_martix)
print(img_martix.size)
print(img_martix.shape)
cv2.imwrite(os.path.join(img_dir,"res.jpg"), img_martix)