图像的 NumPy 切片

NumPy slicing of an image

我一直想复制一张图片覆盖另一张图片this link

我写了一个很简单的python函数:

def fromCopyTo(target, destination, x, y):
    h, w = target.shape

    destination[y:y+h,x:x+w] = target

    return destination

概念是得到一个目标图像和XY坐标并复制target 图像到 position - 不考虑目标图像的大小(假设 destination图片够大)

我正在使用空掩码复制并尝试复制大小为 20x20 的图像 - target 图像仅(0,0,0)(255,255,255) 颜色

问题是 y:y+h, returns 最大尺寸 11 而不是尺寸 20。

ValueError: could not broadcast input array from shape (20,20) into shape (11,20)

如果我翻转函数 destination[x:x+w, y:y+h] = target(据我所知这是错误的)我得到:

ValueError: could not broadcast input array from shape (20,20) into shape (20,0)

通过测试我 运行 print len(destination[y_position:y_position+30, 0]) 其中 returns 直到 11 但在 11[=47= 之后] 它只是将它最大化到 11。例如returns11.

我做错了什么?

事实证明,如果我尝试切割的数量超过 mask 矩阵中可用的数量,我不会收到一条错误消息说我溢出了,但不是这样只是将其限制为最大可能的大小。