I'm using OpenCV to create a color mask of all the red pixels on the screen and I'm facing an issue where simply 'hsv' has an empty src I think but I'm not sure
import cv2
import numpy as np
cap = cv2.VideoCapture(1000)
while(1):
_, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_red = np.array([30,150,50])
upper_red = np.array([255,255,180])
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('res',res)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
cap.release()
The error I keep getting is:
Traceback (most recent call last): File "index.py", line 8, in <module> hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) cv2.error: OpenCV(4.5.5) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
Can anyone help me out?
CodePudding user response:
That error happens because it can't open your camera. The integer you pass to VideoCapture()
is the camera id, if you just pass '0' it will open the default camera. Other than that, make sure to allow apps to use your camera in privacy settings.
cap = cv2.VideoCapture(0)
CodePudding user response:
I solved your problem. What I did, copied and pasted. Then run it. I got same as first error. I changed this underscore to replaced value.
_, frame = cap.read()
to:
ret, frame = cap.read()
Then run it. If it is Ok. Then go back and change underscore again. Then run it. You should be ok.