简体   繁体   中英

Python openCV - error displaying an image from webcam

I am a beginner with OpenCV and Python, doing my first steps... I am trying to run a test code written in Python using OpenCv. I am getting an error saying :

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in unknown function, file ..\..\..\modules\imgproc\src\color.cpp, line 3402
Traceback (most recent call last):
File "C:\cam.py", line 11, in <module>
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: ..\..\..\modules\imgproc\src\color.cpp:3402: error: (-215) scn == 3 || scn == 4

It will run in the IDLE(Python GUI) but, I am trying to run it in VS2010 with the Python plugin - that is where i get the error. Any advice on how to handle this error highly appreciated.

CODE:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

figured it out...

adding

cap.read()

after the

cap = cv2.VideoCapture(0)

fixes the problem...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM