I need to get some pixels that are in range of RGB color values
I use inRange function:
mask = cv2.inRange(image, lower, upper)
Now I get this mask and I need to do some operations with this filtered pixels - i.e. find distance between two random pixels
How I can make this?
If I call print(image) I get following array:
[[[ 78 94 107]
[ 82 97 113]
[ 87 102 118]
...
[101 114 116]
[108 120 122]
[109 121 123]]
As I understand this is array where keys are x and y pixel position and value is RGB code. But If I call print(mask.nonzero()) I see
(array([126, 126, 126, ..., 168, 168, 168], dtype=int64),...)
And I don't really undestand what is in this array and in docs I didn't find type of returned value
Since the mask is a 2d array, the numpy.nonzero(..) method will return a 2-tuple, the first item is an array of indices of the first coordinate that is non-zero, and the second tuple is an array of indices of the second coordinate that is non-zero.
You can np.transpose(..) this to obtain an n×2 matrix with for each row the two coordinates of the pixel that is not zero, so:
print(np.transpose(mask.nonzero()))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With