且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Python和&找到红色OpenCV的

更新时间:2023-08-21 18:44:28

我只是将面具添加到一起,并使用 np.where 掩盖原始图像。

I would just add the masks together, and use np.where to mask the original image.

img=cv2.imread("img.bmp")
img_hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# lower mask (0-10)
lower_red = np.array([0,50,50])
upper_red = np.array([10,255,255])
mask0 = cv2.inRange(img_hsv, lower_red, upper_red)

# upper mask (170-180)
lower_red = np.array([170,50,50])
upper_red = np.array([180,255,255])
mask1 = cv2.inRange(img_hsv, lower_red, upper_red)

# join my masks
mask = mask0+mask1

# set my output img to zero everywhere except my mask
output_img = img.copy()
output_img[np.where(mask==0)] = 0

# or your HSV image, which I *believe* is what you want
output_hsv = img_hsv.copy()
output_hsv[np.where(mask==0)] = 0

这应该比l更快,更可读浏览图像的每个像素。

This should be much faster and much more readable than looping through each pixel of your image.