import cv2
import numpy as np
img
= cv2.imread
('test_img/6_1.jpg')
cv2.imshow
("original", img
)
img_hsv
= cv2.cvtColor
(img, cv2.COLOR_BGR2HSV
)
rows, cols, channels
= img.shape
lower_red
= np.array
([0, 43, 46
])
upper_red
= np.array
([10, 255, 255
])
mask0
= cv2.inRange
(img_hsv, lower_red, upper_red
)
lower_red
= np.array
([156, 43, 46
])
upper_red
= np.array
([180, 255, 255
])
mask1
= cv2.inRange
(img_hsv, lower_red, upper_red
)
mask
= mask0 + mask1
cv2.imshow
("hav red", mask
)
img_r
= img.copy
()
img_r
= cv2.add
(img_r, np.zeros
(np.shape
(img_r
),dtype
=np.uint8
), mask
=mask
)
cv2.imshow
("final image", img_r
)
原图: 利用cv2.inrange()函数提取红色后得到的掩膜: 利用掩膜对原图过滤得到的结果图:
参考文章
根据HSV分量模型,提取红色的几种方法介绍