How do I write a computer program that counts number of black pixels?

I want to write a computer program, that will analyze a random color photo, and count the number of pixels that appear black to human eyes. How do I do that?

First, learn to program and how to create programs.

Next, figure out how to create programs that use OpenCV.

Then, google “how to count pixels in opencv” and apply what you find for your problem.

1 Like

I already write computer programs in python a lot. I already use opencv a lot. I already know how to write computer programs. I already know how to use opencv. So how do I write a computer program that will determine which pixels in a random color photo appear black to human eyes?

have a look at inRange(), countNonZero()

and the tutorials on

https://docs.opencv.org/4.x/

This is the general problem of choosing an appropriate threshold. Please, update your question to make it clear that you’re seeking a code for detecting black pixels according to human eyes, and not real black pixels, i.e. those with 0 value in the matrix.

Anyhow, if you’re using python, I’d rather question this issue on numpy topic instead of opencv. Onces you have the image, you can work on the traverse 3-dimensional pixels and look for all those which are 0 or less than a custom threshold.