我想在彩色图像上叠加一个二值蒙版,这样当蒙版是“开”的时候,像素值会改变我可以设置的量。结果应该如下所示:
我使用的是opencv2.4和python2.7.6。我有一种方法很好,但速度很慢,另一种方法很快,但有溢出和下溢的问题。下面是更快的代码的结果,其中包含溢出/下溢瑕疵:
下面是我的代码,显示了快速版本和慢速版本:def superimpose_mask_on_image(mask, image, color_delta = [20, -20, -20], slow = False):
# superimpose mask on image, the color change being controlled by color_delta
# TODO: currently only works on 3-channel, 8 bit images and 1-channel, 8 bit masks
# fast, but can't handle overflows
if not slow:
image[:,:,0] = image[:,:,0] + color_delta[0] * (mask[:,:,0] / 255)
image[:,:,1] = image[:,:,1] + color_delta[1] * (mask[:,:,0] / 255)
image[:,:,2] = image[:,:,2] + color_delta[2] * (mask[:,:,0] / 255)
# slower, but no issues with overflows
else:
rows, cols = image.shape[:2]
for row in xrange(rows):
for col in xrange(cols):
if mask[row, col, 0] > 0:
image[row, col, 0] = min(255, max(0, image[row, col, 0] + color_delta[0]))
image[row, col, 1] = min(255, max(0, image[row, col, 1] + color_delta[1]))
image[row, col, 2] = min(255, max(0, image[row, col, 2] + color_delta[2]))
return
有没有一种快速的方法(可能使用numpy的一些函数)来获得与我的慢代码当前生成的结果相同的结果?在