Ask Your Question

In Python, is there a method or operator to determine if a frame is empty or an all zero-matrix?

asked 2018-02-28 02:12:26 -0600

masterenol gravatar image

updated 2018-02-28 02:12:58 -0600

For example, I would like to finish this code:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while True:
    ret, frame =

    if # frame is empty or all zeros #:
        print("Empty Frame")

    if cv2.waitKey(33) & 0xFF == ord('q'):


The line, 'if # frame is empty or all zeros #:', is there an operator I can use to determine if a frame is empty?

edit retag flag offensive close merge delete


a frame with all pixels set to 0 is not an empty frame : it's a black image. You can use countNonZero function.

read doc is here

LBerger gravatar imageLBerger ( 2018-02-28 02:40:31 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-02-28 02:40:45 -0600

berak gravatar image

updated 2018-02-28 02:41:22 -0600

"empty" or "all black" are 2 entirely different things !

you can check for "empty" like:

if frame == None:

or, in later numpy versions:

if np.shape(frame) == ():

you can check, for an "all black" frame with:

if np.sum(frame) == 0:
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-02-28 02:12:26 -0600

Seen: 20,661 times

Last updated: Feb 28 '18