获得“你的”蓝色调涉及很多试验和错误。这就是为什么我做了这个小程序来获取这些值。把它想象成你想要显示的范围的颜色选择器。
将此例程视为对该主题的所有其他响应的协作,以创建一个允许对您的颜色选择过程进行一些自定义的工具
import cv2
import numpy as np
cap = cv2.VideoCapture(1, cv2.CAP_DSHOW)
cap.set(3,1280)
cap.set(4,1024)
cv2.namedWindow("Hsv Capture")
# create trackbars for color change
# IMPORTANT: You have to define the correct HSV opencv range hence 179,255,255
cv2.createTrackbar('H', 'Hsv Capture', 0, 179, nothing)
cv2.createTrackbar('S', 'Hsv Capture', 0, 255, nothing)
cv2.createTrackbar('V', 'Hsv Capture', 0, 255, nothing)
cv2.createTrackbar('H1', 'Hsv Capture', 0, 179, nothing)
cv2.createTrackbar('S1', 'Hsv Capture', 0, 255, nothing)
cv2.createTrackbar('V1', 'Hsv Capture', 0, 255, nothing)
while(True):
ret, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Trackbars realtime position
h1 = cv2.getTrackbarPos('H', 'Hsv Capture')
s1 = cv2.getTrackbarPos('S', 'Hsv Capture')
v1 = cv2.getTrackbarPos('V', 'Hsv Capture')
h2 = cv2.getTrackbarPos('H1', 'Hsv Capture')
s2 = cv2.getTrackbarPos('S1', 'Hsv Capture')
v2 = cv2.getTrackbarPos('V1', 'Hsv Capture')
#How to store the min and max values from the trackbars
blue_MIN = np.array([h1, s1, v1], np.uint8)
blue_MAX = np.array([h2, s2, v2], np.uint8)
#After finding your values, you can replace them like this
#blue_MIN = np.array([102, 73, 145], np.uint8)
#blue_MAX = np.array([123, 182, 242], np.uint8)
#Using inRange to find the desired range
hsvCapture = cv2.inRange(frame, blue_MIN, blue_MAX)
cv2.imshow('Hsv Capture', hsvCapture)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()