RGB到HSV 色彩空间转换表格;视频中利用 OpenCV的 inRang 函数,来抓取又特征颜色的物体
利用上述的颜色特征,可以抓取有特定颜色的物体:
1. 把图像从 BGR ===>HSV
2. 利用 mask = cv2.inRange(hsv,lowerb,upperb)函数来抓取特定颜色的物体。
import cv2 import numpy as np def extract_object_demo(): """ 绿色:hmin,hmax,smin,smax,vmin,vmax 对应值:35,77,43,255,46,255 :return: """ # capture = cv2.VideoCapture(1, cv2.CAP_DSHOW) capture = cv2.VideoCapture('blue_cap.avi') while True: ret, frame = capture.read() if ret == False: break hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) lower_hsv = np.array([35, 43, 46]) upper_hsv = np.array([77, 255, 255]) mask = cv2.inRange(hsv, lowerb=lower_hsv, upperb=upper_hsv) cv2.imshow("video", frame) cv2.imshow("mask", mask) c = cv2.waitKey(40) if c == 27: break extract_object_demo() cv2.waitKey(0) cv2.destroyAllWindows()