代码示例
#color filtering
import cv2
import numpy as np
#use camera
cap = cv2.VideoCapture(1)
while True:
_, frame = cap.read()
`this line seem to be the source-->` hsv=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# hsv hue sat value
# try to get the value of the color that you want
lower_red = np.array([150,150,150])
upper_red = np.array([180,255,255])
mask = cv2.inRange(hsv, lower_red, upper_red)
res = cv2.bitwise_and(frame,frame, mask = mask)
cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.imshow('result',res)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
#release camera
cap.release()
错误
Traceback (most recent call last):
File "D:/Program_Files/Python/legit8.py", line 10, in <module>
hsv=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
cv2.error: C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:10705: error: (-215) (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) in function cv::cvtColor
答案 0 :(得分:3)
你的框架可能是None
。这可能是因为VideoCapture(1)
。如果您的网络摄像头是连接到计算机的唯一凸轮,请使用VideoCapture(0)
!