Python OpenCv 颜色识别

By qq84628151 没有评论

原图

1.BGR转HSV,然后用[H-10, 5, 50]和[H+10, 255, 255]范围识别

2.上面得到HSV值为[126, 9, 136],然后根据范围用inRange识别颜色

import cv2
import numpy

img = cv2.imread("D:/test222.png")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

lowerColor = numpy.array([126 - 10, 5, 50])
upperColor = numpy.array([126 + 10, 255, 255])
mask = cv2.inRange(hsv, lowerColor, upperColor)
mask = cv2.bitwise_and(img, img, mask = mask)

cv2.imshow("测试1", img)
cv2.imshow("测试2", mask)

cv2.waitKey()
cv2.destroyAllWindows()

至此结束,这图用颜色识别不太理想,算是熟悉inRange的API用法吧!