1

I am using Python 3.7.3 and I am trying to initiate to OpenCV and video capture and modification. But I am stuck with following problem: When I run the code below, my webcam data are well captured and displayed in imshow window but I am not able to get a proper Numpy array:

import cv2

first_frame = None

video = cv2.VideoCapture(0)
cpt = 0

while True:
    check, frame = video.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray,(21,21), 0)

    if cpt < 40:
        first_frame = gray
        cpt = cpt + 1
        print(cpt)
        continue

    delta_frame = cv2.absdiff(first_frame, gray)

    cv2.imshow("Gray frame", gray)

    cv2.imshow("Delta frame", delta_frame)

    key = cv2.waitKey(1)
    print(frame)
    if key == ord('q'):
        break

video.release()
cv2.destroyAllWindows()

My webcam images are well captured because I can see the result on the window opened by imshow().

But the numpy array return by print(frame) is made of zero values only while displayed images are not fully black, I can see my face:

>>> frame
array([[[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],
       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],
       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],
       ...,
       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],
       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],
       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]], dtype=uint8)
nathancy
  • 42,661
  • 14
  • 115
  • 137
Patinside
  • 13
  • 3
  • What does print(np.sum(frame)) return? Also you are showing `gray` and `delta_frame` so you might want to print those or `imshow(frame)`. Nonetheless it should have non-zero values – Julian Jul 01 '19 at 23:41

1 Answers1

0

Frame should be a proper np array. Have you checked the entire frame? Print only shows a tiny proportion of the frame. You can print np.nonzero(frame) to get the index of all populated points.

lmielke
  • 135
  • 8
  • Thanks to your comment, I succeed to print non zero values. I also printed like so the full numpy array: a = tabulate (frame) print(a) And I saw all the data, not only zero values. – Patinside Jul 02 '19 at 20:13