【发布时间】:2014-02-03 19:05:31
【问题描述】:
我想使用 Opencv 绘制一些图像,为此我想将图像粘合在一起。
假设我有 4 张图片。最好的方法是将它们粘合在 2x2 图像矩阵中。
a = img; a.shape == (48, 48)
b = img; b.shape == (48, 48)
c = img; c.shape == (48, 48)
d = img; d.shape == (48, 48)
我现在使用 np.reshape,它采用 [a,b,c,d] 之类的列表,然后我手动放置尺寸以获得以下结果:
np.reshape([a,b,c,d], (a.shape*2, a.shape*2)).shape == (96, 96)
当我有 3 张图片时,问题就开始了。我有点想我可以取列表长度的平方根,然后取最大值,这将产生 2 的方阵维度 (np.ceil(sqrt(len([a,b,c]))) == 2)。然后,我必须将具有第一个元素尺寸的白色图像添加到列表中,然后就可以了。但我想必须有一种更简单的方法来完成这个绘图,很可能已经在某个地方定义了。
那么,如何轻松地将任意数量的方阵组合成一个大方阵?
编辑:
我想出了以下内容:
def plotimgs(ls):
shp = ls[0].shape[0] # the image's dimension
dim = np.ceil(sqrt(len(ls))) # the amount of pictures per row AND column
emptyimg = (ls[1]*0 + 1)*255 # used to add to the list to allow square matrix
for i in range(int(dim*dim - len(ls))):
ls.append(emptyimg)
enddim = int(shp*dim) # enddim by enddim is the final matrix dimension
# Convert to 600x600 in the end to resize the pictures to fit the screen
newimg = cv2.resize(np.reshape(ls, (enddim, enddim)), (600, 600))
cv2.imshow("frame", newimg)
cv2.waitKey(10)
plotimgs([a,b,d])
不知何故,即使尺寸还可以,它实际上还是多克隆了一些图片:
When I give 4 pictures, I get 8 pictures. When I give 9 pictures, I get 27 pictures. When I give 16 pictures, I get 64 pictures.
所以事实上,我不是平方,而是以某种方式得到图像的三次方。虽然,例如
plotimg([a]*9) 给出的图片尺寸为44*3 x 44*3 = 144x144,对于 9 张图片应该是正确的?
【问题讨论】:
标签: python numpy opencv matrix