拼接
import cv2
img =cv2.imread(file_path[i])
img=cv2.hconcat([img,img,img])#水平拼接
img=cv2.vconcat([img,img,img])#垂直拼接
合并
# 生成一个值为0的单通道数组
zeros = np.zeros(image.shape[:2], dtype = "uint8")
# 分别扩展B、G、R成为三通道。另外两个通道用上面的值为0的数组填充
cv2.imshow("Blue", cv2.merge([B, zeros, zeros]))
cv2.imshow("Green", cv2.merge([zeros, G, zeros]))
cv2.imshow("Red", cv2.merge([zeros, zeros, R]))
cv2.waitKey(0)
拼接原图以及其他的带alpha通道的图
images = os.listdir(path1)
for i in range(len(images)):
img1 = cv2.imread(os.path.join(path1, images[i]), cv2.IMREAD_UNCHANGED)
h, w, c = img1.shape
alpha = np.ones((h, w, 1), dtype = np.uint8) * 255
img1 = cv2.merge([img1, alpha])
print("img1.shape:", img1.shape)
img2 = cv2.imread(os.path.join(path2, images[i].replace(".jpg", ".png")), cv2.IMREAD_UNCHANGED)
img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0]))
img3 = cv2.imread(os.path.join(path3, images[i].replace(".jpg", "_merge.png")), cv2.IMREAD_UNCHANGED)
print("img2.shape:", img2.shape)
print("img3.shape:", img3.shape)
img4 = cv2.hconcat([img1, img2, img3])
cv2.imwrite(os.path.join(path4, images[i].replace(".jpg", "_merge.png")), img4)
拆分
# 导入库
import numpy as np
import argparse
import cv2
# 构造参数解析器
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path to the image")
args = vars(ap.parse_args())
# 加载图像
image = cv2.imread(args["image"])
# 通道分离,注意顺序BGR不是RGB
(B, G, R) = cv2.split(image)
# 显示各个分离出的通道
cv2.imshow("Red", R)
cv2.imshow("Green", G)
cv2.imshow("Blue", B)
cv2.waitKey(0)