如何更改 Python 中相机识别的颜色
How to change colours identified by the camera in Python
此程序用于从相机识别红色和蓝色。
有谁知道如何更改程序以识别绿色而不是蓝色?
提前致谢!
import numpy as np
import cv2
red_lower = np.array([-10, 100, 100])
red_upper = np.array([10, 255, 255])
blue_lower = np.array([100, 43, 46])
blue_upper = np.array([124, 255, 255])
cap = cv2.VideoCapture(0)
cap.set(3, 320)
cap.set(4, 240)
def ChestRed():
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, red_lower, red_upper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.GaussianBlur(mask, (3, 3), 0)
return mask
def ChestBule():
ret, frame = cap.read()
frame = cv2.GaussianBlur(frame, (5, 5), 0)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, blue_lower, blue_upper)
mask = cv2.GaussianBlur(mask, (3, 3), 0)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if 25 < len(cnts) < 29:
print("Blue!")
while 1:
ret, frame = cap.read()
frame = cv2.GaussianBlur(frame, (5, 5), 0)
hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
mask = ChestRed()
ChestBule()
res = cv2.bitwise_and(frame,frame,mask=mask)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if 20<len(cnts)<30:
print("Red!")
cv2.imshow("mask", mask)
cv2.imshow("res", res)
if cv2.waitKey(5) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
您想知道如何更改程序以识别绿色而不是蓝色。
要做到这一点,只需 将 blue_lower 和 blue_upper 更改为相应的 HSV 值,并将其设为您要考虑的绿色阴影 。
对于浅绿色,使用较低的值作为 [50, 242, 220],较高的值作为 [80, 255, 255]
我们最好使用 np.array([x,y,z],dtype='uint8') 其中 x,y ,z 是 HSV 值,具体取决于您要屏蔽的颜色
此程序用于从相机识别红色和蓝色。
有谁知道如何更改程序以识别绿色而不是蓝色?
提前致谢!
import numpy as np
import cv2
red_lower = np.array([-10, 100, 100])
red_upper = np.array([10, 255, 255])
blue_lower = np.array([100, 43, 46])
blue_upper = np.array([124, 255, 255])
cap = cv2.VideoCapture(0)
cap.set(3, 320)
cap.set(4, 240)
def ChestRed():
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, red_lower, red_upper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.GaussianBlur(mask, (3, 3), 0)
return mask
def ChestBule():
ret, frame = cap.read()
frame = cv2.GaussianBlur(frame, (5, 5), 0)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, blue_lower, blue_upper)
mask = cv2.GaussianBlur(mask, (3, 3), 0)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if 25 < len(cnts) < 29:
print("Blue!")
while 1:
ret, frame = cap.read()
frame = cv2.GaussianBlur(frame, (5, 5), 0)
hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
mask = ChestRed()
ChestBule()
res = cv2.bitwise_and(frame,frame,mask=mask)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if 20<len(cnts)<30:
print("Red!")
cv2.imshow("mask", mask)
cv2.imshow("res", res)
if cv2.waitKey(5) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
您想知道如何更改程序以识别绿色而不是蓝色。 要做到这一点,只需 将 blue_lower 和 blue_upper 更改为相应的 HSV 值,并将其设为您要考虑的绿色阴影 。
对于浅绿色,使用较低的值作为 [50, 242, 220],较高的值作为 [80, 255, 255]
我们最好使用 np.array([x,y,z],dtype='uint8') 其中 x,y ,z 是 HSV 值,具体取决于您要屏蔽的颜色