Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Can't see right now why your way isn't working. For me it works:

Example:

>>> b
array([[[ 0,  1,  2]],
       [[ 3,  4,  5]],
       [[ 6,  7,  8]],
       [[ 9, 10, 11]]])

>>> c = cv2.inRange(b, np.array([3,4,5]), np.array([6,7,8]))
>>> c
array([[  0],
       [255],
       [255],
       [  0]], dtype=uint8)
>>> cv2.countNonZero(c)
2

another way would be to use more the numpy-functionality (probably there exist sth more elegant than this)

>>> np.count_nonzero((b[:,:,0] >= 3) & (b[:,:,1] >= 4) &\
   (b[:,:,2] >= 5) & (b[:,:,0] <= 6) & (b[:,:,1] <= 7) &\
   (b[:,:,2] <= 8))
2