有一個 opencv 圖像,我將其分為 3 個通道: image #opencv image img_red = image[:, :, 2] img_green = image[:, :, 1] img_blue = image[:, :, 0]然后是三個過濾器:red_filtergreen_filterblue_filter它們都是 numpy 數組,但大部分由零填充,因此格式如下所示:[0, 0, 0, 132, ... 0, 15, 0, 230, 0] ... [32, 0, 5, 0, ... 0, 2, 150, 0, 0]我想使用這些過濾器中的每個非零值來覆蓋通道中的相同索引。像這樣的東西:img_red[index] = red_filter[index] if red_filter != 0img_green[index] = green_filter[index] if green_filter != 0img_blue[index] = blue_filter[index] if blue_filter != 0final_img = cv2.merge(img_red, img_green, img_blue)例如,如果頻道看起來像這樣:[44, 225, 43, ... 24, 76, 56]和過濾器:[0, 0, 25 ... 2, 0, 91]那么結果應該是:[44, 225, 25 ... 2, 76, 91]我嘗試過使用 for 循環和列表理解,但此代碼必須在視頻中的每一幀上運行,所以我想知道是否有更快的方法來實現相同的結果。opencv 或 numpy 操作中是否有某種圖像過濾可以有效地完成此過程?
1 回答

牧羊人nacy
TA貢獻1862條經驗 獲得超7個贊
看起來你正在尋找np.where方法:
channel = np.array([44, 225, 43, 24, 76, 56])
filter = np.array([0, 0, 25, 2, 0, 91])
#result = np.array([44, 225, 25, 2, 76, 91])
>>> np.where(filter==0, channel, filter)
array([ 44, 225, 25, 2, 76, 91])
添加回答
舉報
0/150
提交
取消