这是相同的 Python 代码。它可以接收任何通道的图像,但必须像 hcontat、vcontat、hstack、vstack 一样保持宽度和高度。
- scale:按给定数字缩放最终图像
- imgArray:是您要连接的所有图像的数组,无论每个图像中的通道如何
import numpy as np
import cv2
def stackImages(scale,imgArray):
rows = len(imgArray)
cols = len(imgArray[0])
rowsAvailable = isinstance(imgArray[0],list)
width = imgArray[0][0].shape[1]
height = imgArray[0][0].shape[0]
if rowsAvailable:
for x in range(0,rows):
for y in range(0, cols):
if imgArray[x][y].shape[:2] == imgArray[x][y].shape[:2]:
imgArray[x][y] = cv2.resize(imgArray[x][y],(0,0),None,scale,scale)
else:
imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[0]), None, scale, scale)
if len(imgArray[x][y].shape) == 2: imgArray[x][y] =cv2.cvtColor(imgArray[x][y],cv2.COLOR_GRAY2BGR)
imageBlank = np.zeros((height,width,3), np.uint8)
hor = [imageBlank]*rows
hor_con = [imageBlank]*rows
for x in range(0, rows):
hor[x] = np.hstack(imgArray[x])
ver = np.vstack(hor)
else:
for x in range(0, rows):
if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
imgArray[x] = cv2.resize(imgArray[x],(0,0),None,scale,scale)
else:
imgArray[x] = cv2.resize(imgArray[x],(imgArray[0].shape[0],imgArray[0].shape[1]),None,scale,scale)
if len(imgArray[x].shape) == 2: imgArray[x] =cv2.cvtColor(imgArray[x],cv2.COLOR_GRAY2BGR)
hor = np.hstack(imgArray)
ver = hor
return ver
函数 stackedImages(scale, imageArray) 属于提供轨迹栏和活体检测教程的人创建的包。在这里查看:https://www.youtube.com/watch?v=Fchzk1lDt7Q&t=25s