1 回答
TA贡献1786条经验 获得超11个赞
您将值 125 用于掩码中的所有点:
output_img[np.where(mask!=0)] = 125
如果要更改为彩色像素,只需为该像素分配一个颜色数组,例如:
output_img[np.where(mask!=0)] = [125, 50, 50] #RGB array
完整代码:
import numpy as np
import cv2
import matplotlib.pyplot as plt
img2 = cv2.imread('sample.png')
img_hsv=cv2.cvtColor(img2, cv2.COLOR_BGR2HSV)
# lower mask (0-10)
lower_red = np.array([50,50,0])
upper_red = np.array([255,255,50])
mask0 = cv2.inRange(img_hsv, lower_red, upper_red)
# upper mask (170-180)
lower_red = np.array([50,50,50])
upper_red = np.array([255,255,180])
mask1 = cv2.inRange(img_hsv, lower_red, upper_red)
# join my masks
mask = mask0+mask1
output_img = img2.copy()
output_img[np.where(mask!=0)] = [125, 50, 50]
plt.imshow(img2)
plt.title('Original')
plt.show()
plt.imshow(output_img)
plt.title('After')
plt.show()
添加回答
举报