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

asked 2016-06-06 15:03:24 -0500

YaddyVirus gravatar image

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?

edit retag flag offensive close merge delete

Comments

1

a proper roi would look like: img[0:480, 320:640]

berak gravatar imageberak ( 2016-06-06 18:46:42 -0500 )edit

@berak it works! Thanks a ton! But I don't understand in what order you've written the coordinates

YaddyVirus gravatar imageYaddyVirus ( 2016-06-07 02:20:15 -0500 )edit

these are actually numpy ranges `image[ ymin : ymax, xmin : xmax ]

(not sure, what your original statement tried to do.)

berak gravatar imageberak ( 2016-06-07 02:38:31 -0500 )edit

@berak ohh the minimum and max ranges of the y and x axis respectively right?

YaddyVirus gravatar imageYaddyVirus ( 2016-06-07 02:44:57 -0500 )edit