除非值为0,否则将numpy数组中的所有像素替换为单独数组中的像素

问题描述

一个opencv图像,我将其分为3个通道:

 image #opencv image

 img_red = image[:,:,2]
 img_green = image[:,1]
 img_blue = image[:,0]

然后有三个过滤器:

red_filter
green_filter
blue_filter

所有都是numpy数组,但是大多数都是零填充的,因此格式看起来像这样:

[0,132,... 0,15,230,0]
               ...                   
[32,5,2,150,0]

我想使用这些过滤器中的每个非零值来覆盖通道中的相同索引。

类似这样的东西:

img_red[index] = red_filter[index] if red_filter != 0
img_green[index] = green_filter[index] if green_filter != 0
img_blue[index] = blue_filter[index] if blue_filter != 0
final_img = cv2.merge(img_red,img_green,img_blue)

例如,频道是否如下所示:

[44,225,43,... 24,76,56]

还有过滤器:

[0,25   ... 2,91]

那么结果应该是:

[44,25 ...  2,91]

我曾经尝试过使用for循环和列表推导,但是这段代码必须在视频的每一帧上运行,所以我想知道是否有更快的方法来获得相同的结果。

opencv中是否有某种图像过滤或numpy操作可以有效地完成此过程?

解决方法

似乎您正在寻找channel = np.array([44,225,43,24,76,56]) filter = np.array([0,25,2,91]) #result = np.array([44,91]) >>> np.where(filter==0,channel,filter) array([ 44,91]) 方法:

$code = (int) $code;