我想将任意九张图片按照九宫格排列方式合并成一张大图,使用openCV实现。
如果用画图工具来实现的话,需要事先准备一个600 X 600像素的画布。用openCV实现也是同理,准备一张600 X 600的图片。然后将图片划分成9份,每一份替换成小图片。九张小图片的大小相同,都是200 X 200像素。
import numpy as np
import cv2 as cv
import matplotlib.pyplot as pltif __name__ == '__main__':img = cv.imread("C:/Users/zhour/Downloads/jiasu.png", cv.IMREAD_UNCHANGED)img2 = cv.imread("C:/Users/zhour/Downloads/qianbao.png", cv.IMREAD_UNCHANGED)img3 = cv.imread("C:/Users/zhour/Downloads/luyin.png", cv.IMREAD_UNCHANGED)img4 = cv.imread("C:/Users/zhour/Downloads/kaipiao.png", cv.IMREAD_UNCHANGED)img5 = cv.imread("C:/Users/zhour/Downloads/anli.png", cv.IMREAD_UNCHANGED)img6 = cv.imread("C:/Users/zhour/Downloads/shangchuan.png", cv.IMREAD_UNCHANGED)img7 = cv.imread("C:/Users/zhour/Downloads/qingchu.png", cv.IMREAD_UNCHANGED)img8 = cv.imread("C:/Users/zhour/Downloads/fenlei.png", cv.IMREAD_UNCHANGED)img9 = cv.imread("C:/Users/zhour/Downloads/chaxun.png", cv.IMREAD_UNCHANGED)merge=cv.imread("C:/Users/zhour/Pictures/merge.png", cv.IMREAD_UNCHANGED)row1,col1,channel1=img.shaperow2,col2,channel2=img2.shaperow3,col3,channel3=img3.shaperow4,col4,channel4=img4.shaperow5,col5,channel5=img5.shaperow6,col6,channel6=img6.shaperow7,col7,channel7=img7.shaperow8,col8,channel8=img8.shaperow9,col9,channel9=img9.shapefor i in range(row1):for j in range(col1):merge[i,j]=img[i,j]for i in range(row2):for j in range(col2):merge[i,col1+j]=img2[i,j] for i in range(row3):for j in range(col3):merge[i,col1+col2+j]=img3[i,j] for i in range(row4):for j in range(col4):merge[row1+i,j]=img4[i,j] for i in range(row5):for j in range(col5):merge[row1+i,col1+j]=img5[i,j] for i in range(row6):for j in range(col6):merge[row1+i,col1+col2+j]=img6[i,j] for i in range(row7):for j in range(col7):merge[row1+row2+i,j]=img7[i,j] for i in range(row8):for j in range(col8):merge[row1+row2+i,col1+j]=img8[i,j]for i in range(row9):for j in range(col9):merge[row1+row2+i,col1+col2+j]=img9[i,j] cv.imwrite("C:/Users/zhour/Pictures/merge2.png",merge) #plt.imshow(merge)#plt.show()