我有 100 张小图像,我想将它们组合成一张大 (10x10) 网格图像,以便与 imshow 一起显示。每个图像(作为一个 numpy 数组)都在一个单元格对象的变量中。目前我正在使用 concatenate 首先创建垂直 strip ,然后使用 concatenate 连接所有这些 strip ,但它看起来有点笨拙。有一个更好的方法吗?我觉得我应该能够创建一个最终图像大小 (800 x 600) 的 numpy 数组,然后将每个图像放入其中,但这似乎超出了我对语法的理解能力。
def stackImages(im1, im2, axisToStack):
newImage = np.concatenate((im1, im2), axis = axisToStack)
return newImage
def compileCells():
#Make a list of strips
strips = [(np.zeros((0,cellWidth,3), np.uint8)) for i in range(numberOfCells)]
for x in range(numberOfCells):
for i in range(numberOfCells):
strips[x] = stackImages(cellArray[i+(x*numberOfCells)].image, strips[x], 0)
display = strips[0]
for c in range(1,numberOfCells):
display = stackImages(strips[c], display, 1)
return display
最佳答案
在使用 NumPy 时,复制数组可能是真正的速度 killer 。每次
np.concatenate
被调用,为新数组分配空间,所有旧数组
数据被复制到新数组中。使代码更快的一种方法是减少
复制量。
所以正如你所建议的,更快的方法是为最终数组分配空间,
从一开始就显示
:
display = np.empty((cellHeight*nrows, cellWidth*ncols, 3), dtype=np.uint8)
然后将数据从 cellArray
复制到 display
一次:
for i, j in IT.product(range(nrows), range(ncols)):
arr = cellArray[i*ncols+j].image
x, y = i*cellHeight, j*cellWidth
display[x:x+cellHeight, y:y+cellWidth, :] = arr
例如,
import numpy as np
import matplotlib.pyplot as plt
import itertools as IT
def compileCells(cellArray, nrows, ncols, cellHeight, cellWidth):
display = np.empty((cellHeight*nrows, cellWidth*ncols, 3), dtype=np.uint8)
for i, j in IT.product(range(nrows), range(ncols)):
# arr = cellArray[i*ncols+j].image # you may need this
arr = cellArray[i*ncols+j] # my simplified cellArray uses this
x, y = i*cellHeight, j*cellWidth
display[x:x+cellHeight, y:y+cellWidth, :] = arr
return display
cellHeight, cellWidth = 80, 60
nrows = ncols = numberOfCells = 10
cellArray = [np.full((cellHeight, cellWidth, 3), i)
for i in np.linspace(0, 255, nrows*ncols)]
display = compileCells(cellArray, nrows, ncols, cellHeight, cellWidth)
plt.imshow(display)
plt.show()
产量
请注意,您的代码暗示 cellArray
是一个对象列表,其 image
属性是 NumPy 数组。要使上面的示例代码可运行且简单(r),
我已将上面的 cellArray
定义为 NumPy 数组列表。
您可能需要取消注释
# arr = cellArray[i*ncols+j].image
并注释掉
arr = cellArray[i*ncols+j]
符合您对 cellArray
的定义。
让我们比较一下两种方法完成的复制量:
使用原来的方法,如果我们说图像数组的大小为 1,那么构建一个 strip
需要
分配大小为 1、2、...、10 的数组。因此,一个 strip 需要分配数组
总大小为 1+2+...+10 = 10(11)/2 = 55。构建 display
需要分配
总大小为 55(1+2+..+10) = 55*55 = 3025 的数组。每个空间分配是
伴随着复制操作。复制量随最终阵列中的单元格数量二次方增长。
相反,如果我们只为最后的display
分配一次空间,那么我们
只需要分配总大小 10*10 = 100。这里,复制量随单元格数量线性增长。
关于python - 比在 python 中组合多个 cv2 图像(numpy 数组)更快的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29956677/