-2

I have a video file for image processing. In the video I've captured samples in exact times like figure, thresholded by using OpenCV. Now I want to find differences in the number of black pixels in order to reach the graphic of time vs. difference. How can I find the number of black pixels in each image in Python?

https://i.stack.imgur.com/Nfgrb.png
(Click image to enlarge)

karel
  • 5,489
  • 46
  • 45
  • 50

1 Answers1

1

OpenCV does not offer a function do directly count black pixels but a function to count all pixels that are not black: cv2.countNonZero(img)

As you have not posted your code here is a sample how you could use this:

# get all non black Pixels
cntNotBlack = cv2.countNonZero(img)

# get pixel count of image
height, width, channels = img.shape
cntPixels = height*width

# compute all black pixels
cntBlack = cntPixels - cntNotBlack

Note that this will only find pure black pixel (meaning all channels are exactly zero).

Mailerdaimon
  • 6,003
  • 3
  • 35
  • 46