当尝试将帧转换为灰度时如何解决错误?
错误消息:cv2.cvtColor(图像,cv2.COLOR_BGR2GRAY)cv2。错误:OpenCV(4.0.0)c:\ projects \ opencv-python \ opencv \ modules \ imgproc \ src \ color.hpp:259:错误:函数'__cdecl cv :: CvtHelper中的(-2:Unspecified error),结构cv :: Set <1,-1,-1>,结构cv :: Set <0,2,5>,2> :: CvtHelper (const class cv :: _ InputArray&,const class cv :: _ OutputArray&,int)'>输入图像中无效的通道数:>'VScn :: contains(scn)'>其中>'scn'为1
class OpenCVCapture(object):
def __init__(self, device_id=0):
"""Create an OpenCV capture object associated with the provided webcam
device ID.
"""
# Open the camera.
self._camera = cv2.VideoCapture(device_id)
if not self._camera.isOpened():
self._camera.open()
# Start a thread to continuously capture frames.
# This must be done because different layers of buffering in the webcam
# and OS drivers will cause you to retrieve old frames if they aren't
# continuously read.
self._capture_frame = None
# Use a lock to prevent access concurrent access to the camera.
self._capture_lock = threading.Lock()
self._capture_thread = threading.Thread(target=self._grab_frames)
self._capture_thread.daemon = True
self._capture_thread.start()
def _grab_frames(self):
while True:
retval, frame = self._camera.read()
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
with self._capture_lock:
self._capture_frame = None
if retval:
self._capture_frame = frame
time.sleep(1.0/CAPTURE_HZ)
def read(self):
"""Read a single frame from the camera and return the data as an OpenCV
image (which is a numpy array).
"""
frame = None
with self._capture_lock:
frame = self._capture_frame
# If there are problems, keep retrying until an image can be read.
while frame is None:
time.sleep(0)
with self._capture_lock:
frame = self._capture_frame
# Save captured image for debugging.
cv2.imwrite(config.DEBUG_IMAGE, frame)
# Return the capture image data.
return frame