Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to use np.count_nonzero() within a range of pixels?

Hi there!

I am working on a script that reads the non-zero pixels in a binary image. I am using np.count_nonzero() to count non zero pixels, which works fine until, I specify a range of coordinates.

ele = np.count_nonzero(img)

Works fine
But, when I specify a range of pixels, for example I am working with a 640*480 res image, Now I want that only the pixels falling in the range of (320,0) and (640,480), that is, the right half, be checked for non zero values, so I modify my code to -

ele = np.count_nonzero(((320,0)<gray) & (gray <(640,480)))

But this line gives an error -

ValueError: operands could not be broadcast together with shapes (480,640) (2,)

To me it sounds like the image layers (RGB or similar) might be causing this problem. Can anyone tell me how to fix it?