imgHsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) cv2.error: OpenCV(4.0.0)
imgHsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) cv2.error: OpenCV(4.0.0)
我一直在尝试使用 OpenCV 运行 在我的网络摄像头上进行颜色检测,但它不断出现如标题和下方所示的错误:
imgHsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
我想要 运行 的代码是:
import cv2
import numpy as np
frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture(1)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
def empty(a):
pass
cv2.namedWindow("HSV")
cv2.resizeWindow("HSV",640,240)
cv2.createTrackbar("HUE Min","HSV",0,179,empty)
cv2.createTrackbar("HUE Max","HSV",179,179,empty)
cv2.createTrackbar("SAT Min","HSV",0,255,empty)
cv2.createTrackbar("SAT Max","HSV",255,255,empty)
cv2.createTrackbar("VALUE Min","HSV",0,255,empty)
cv2.createTrackbar("VALUE Max","HSV",255,255,empty)
while True:
_, img = cap.read(0)
imgHsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
h_min = cv2.getTrackbarPos("HUE Min","HSV")
h_max = cv2.getTrackbarPos("HUE Max", "HSV")
s_min = cv2.getTrackbarPos("SAT Min", "HSV")
s_max = cv2.getTrackbarPos("SAT Max", "HSV")
v_min = cv2.getTrackbarPos("VALUE Min", "HSV")
v_max = cv2.getTrackbarPos("VALUE Max", "HSV")
print(h_min)
lower = np.array([h_min,s_min,v_min])
upper = np.array([h_max,s_max,v_max])
mask = cv2.inRange(imgHsv,lower,upper)
result = cv2.bitwise_and(img,img, mask = mask)
mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
hStack = np.hstack([img,mask,result])
cv2.imshow('Original', img)
cv2.imshow('HSV Color Space', imgHsv)
cv2.imshow('Mask', mask)
cv2.imshow('Result', result)
cv2.imshow('Horizontal Stacking', hStack)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
我不确定这是OpenCV版本的问题,还是与代码或smth有关的任何问题。有什么想法吗?
可能的问题 #1:摄像头设置
按Windows Key
,键入camera
。点击 Camera privacy settings
.
在打开屏幕上,确保 Allow apps to access your camera
已打开。
现在,使用您的代码重试!
可能的问题 #2:错误的索引
查找相机索引:
max_cam_number = 10
for i in range(-1, max_cam_number):
cap = cv2.VideoCapture(i)
test, frame = cap.read()
if test:
print("i : "+str(i)+" /// result: "+str(test))
大多数时候,如果您使用笔记本电脑的网络摄像头,您应该尝试 0:
cap = cv2.VideoCapture(0)
我一直在尝试使用 OpenCV 运行 在我的网络摄像头上进行颜色检测,但它不断出现如标题和下方所示的错误:
imgHsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
我想要 运行 的代码是:
import cv2
import numpy as np
frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture(1)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
def empty(a):
pass
cv2.namedWindow("HSV")
cv2.resizeWindow("HSV",640,240)
cv2.createTrackbar("HUE Min","HSV",0,179,empty)
cv2.createTrackbar("HUE Max","HSV",179,179,empty)
cv2.createTrackbar("SAT Min","HSV",0,255,empty)
cv2.createTrackbar("SAT Max","HSV",255,255,empty)
cv2.createTrackbar("VALUE Min","HSV",0,255,empty)
cv2.createTrackbar("VALUE Max","HSV",255,255,empty)
while True:
_, img = cap.read(0)
imgHsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
h_min = cv2.getTrackbarPos("HUE Min","HSV")
h_max = cv2.getTrackbarPos("HUE Max", "HSV")
s_min = cv2.getTrackbarPos("SAT Min", "HSV")
s_max = cv2.getTrackbarPos("SAT Max", "HSV")
v_min = cv2.getTrackbarPos("VALUE Min", "HSV")
v_max = cv2.getTrackbarPos("VALUE Max", "HSV")
print(h_min)
lower = np.array([h_min,s_min,v_min])
upper = np.array([h_max,s_max,v_max])
mask = cv2.inRange(imgHsv,lower,upper)
result = cv2.bitwise_and(img,img, mask = mask)
mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
hStack = np.hstack([img,mask,result])
cv2.imshow('Original', img)
cv2.imshow('HSV Color Space', imgHsv)
cv2.imshow('Mask', mask)
cv2.imshow('Result', result)
cv2.imshow('Horizontal Stacking', hStack)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
我不确定这是OpenCV版本的问题,还是与代码或smth有关的任何问题。有什么想法吗?
可能的问题 #1:摄像头设置
按
Windows Key
,键入camera
。点击Camera privacy settings
.在打开屏幕上,确保
Allow apps to access your camera
已打开。
现在,使用您的代码重试!
可能的问题 #2:错误的索引
查找相机索引:
max_cam_number = 10
for i in range(-1, max_cam_number):
cap = cv2.VideoCapture(i)
test, frame = cap.read()
if test:
print("i : "+str(i)+" /// result: "+str(test))
大多数时候,如果您使用笔记本电脑的网络摄像头,您应该尝试 0:
cap = cv2.VideoCapture(0)