Ask Your Question

Laftors's profile - activity

2017-05-06 12:29:36 -0600 asked a question cap.read(0) does not work well

Hi everyone,

I'm a begginer with opencv and I need a program that uses a webcam. I found the basic code en Google :

import numpy as np
import cv2

cv2.namedWindow("preview")

cap = cv2.VideoCapture(0)


while True:

    ret, frame = cap.read()

    cv2.imshow("preview", frame)



    key = cv2.waitKey(1)
    if key == 27: # exit on ESC
        break


cap.release()
cv2.destroyWindow("preview")

My laptop cam is defined by index 0 and it is switch on by this code (a green LED tells me this). This cam also works for general use.

The problem is that 'frame' is an array that contains mostly black pixels, but not all (ie ~50 of them are non-zeros).

Does someone have an idea of the problem ? Does just OpenCV not support my webcam (..but then where do the few colors come from ?) ?

Thanks !