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?
Asked
Active
Viewed 3,179 times
1 Answers
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
-
Couldn't you just invert the image then use the method? – rayryeng May 11 '19 at 03:04
-
No, if you invert the image first and then use `countNonZero` you get all pixels but the completly white pixels (as white is now the new black/zero). – Mailerdaimon May 13 '19 at 05:26
-
Right, I assumed a completely binary image. Never mind. – rayryeng May 13 '19 at 05:41
-
1For a complete binary image this would work exactly as you described :) – Mailerdaimon May 13 '19 at 05:44