【发布时间】:2018-04-25 16:49:38
【问题描述】:
我正在尝试将 opencv 与 python 一起使用,但我遇到了这个问题:
我有一个图像和一个二进制掩码(具有 0 和 255 的单通道图像) 我想迭代掩码的每个像素,并根据掩码像素的值对原始图像执行一些操作。 我如何使用 numpy 优化来做到这一点?
例如,假设我想创建一个新图像,其中每个像素在掩码中的值为 0 时保持不变,如果掩码中的像素为 255,则将其设置为 (0,0,255),例如:
def inpaint(originalImage, mask):
[rows, columns, channels] = originalImage.shape
result = np.zeros((rows,columns,channels))
for row in range(rows):
for column in range(columns):
if(mask[row,column]==0):
result[row,column] = originalImage[row,column]
else:
result[row,column] = (0,0,255)
return result
如何使用 numpy 进行优化? 非常感谢
【问题讨论】: