# Count white pixels in each column in contours (with cv2.countNonZero()?)

**URL:** <https://forum.opencv.org/t/count-white-pixels-in-each-column-in-contours-with-cv2-countnonzero/11092>\
**Category:** Python\
**Created:** [November 24, 2022, 3:09pm UTC](https://forum.opencv.org/t/count-white-pixels-in-each-column-in-contours-with-cv2-countnonzero/11092 "2022-11-24T15:09:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![digerid](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/digerid/32/5690_2.png) [@digerid](https://forum.opencv.org/u/digerid)\
**Post date:** [November 24, 2022, 3:09pm UTC](https://forum.opencv.org/t/count-white-pixels-in-each-column-in-contours-with-cv2-countnonzero/11092/1 "2022-11-24T15:09:25Z")

</div>

In a picture i find two contours among each other which look like this:

 ![grafik](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/f/f9a9018bdcfd1541aba4d245ee65c72ef507ded9.png)  
I use cv.OTSU\_THRESHOLD to find them with `cv.findCountours()` or `cv.connectedComponentsWithStats()` without a problem. I will use whatever is best for the actual problem.

Now i want to measure the height of each column for each of the contours, and display the minimum height, maximum height and maybe even the height every n pixel. I already use cv.minAreaRect and display it, but its still not as accurate as i would like it. I am also able to find pixel\_per\_mm\_ratio with an ArUco Marker without problems. From theory, it cant be that hard…

I thought i can use cv.countNonZero() but i dont know how to apply this function for contours and for each column. I found some topics going in this direction but nothing helped me to the end. I even posted a topic here where i got great help but i couldnt do the really last step i guess. There you can even find some simplified code with connected components: [Height of contour at different positions - #10 by digerid](https://forum.opencv.org/t/height-of-contour-at-different-positions/10882/10)

Thanks for your help!
