# How can I count how many black and white pixels are on a picture?

**URL:** <https://forum.opencv.org/t/how-can-i-count-how-many-black-and-white-pixels-are-on-a-picture/2936>\
**Category:** Uncategorized\
**Tags:** mat, core\
**Created:** [April 23, 2021, 8:55am UTC](https://forum.opencv.org/t/how-can-i-count-how-many-black-and-white-pixels-are-on-a-picture/2936 "2021-04-23T08:55:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zedespook](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/zedespook/32/991_2.png) [@Zedespook](https://forum.opencv.org/u/Zedespook)\
**Post date:** [April 23, 2021, 8:55am UTC](https://forum.opencv.org/t/how-can-i-count-how-many-black-and-white-pixels-are-on-a-picture/2936/1 "2021-04-23T08:55:46Z")

</div>

Hello!

After turning an image grayscale I figured the best solutions for one of my projects would be if I could count the amount of white pixels on a picture, is there a way to loop through the pixels on the picture and get their colour?

I would appreciate some kind intellect to help me find a solution! 🙂

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 23, 2021, 9:11am UTC](https://forum.opencv.org/t/how-can-i-count-how-many-black-and-white-pixels-are-on-a-picture/2936/2 "2021-04-23T09:11:23Z")

</div>

look at [countNonZero()](https://docs.opencv.org/master/d2/de8/group __core__ array.html#gaa4b89393263bb4d604e0fe5986723914)

if you have a _grayscale_ image, not a binary one, you probably have to add some condition:

```
n = cv.countNonZero(gray == 255) # count only "full-on" pixels
```
