Ask Your Question
0

how to retrieve all coordinates of pixels of specific colour in an image.

asked Jul 26 '16

bharath95 gravatar image

Hello, I'm new to OpenCV and I'm trying to retrieve the coordinates (preferably in numpy array) of specific colour in my image. I have four different colours in the image and want extract all the four colour points separately in four different array's.

Preview: (hide)

1 answer

Sort by » oldest newest most voted
5

answered Jul 26 '16

Missing gravatar image

updated Jul 26 '16

You can use the numpy.where() method to do that. Here is a minimal example:

import numpy as np

zeros = np.zeros((100, 100), dtype=np.uint8)
zeros[:5,:5] = 255

indices = np.where(zeros == [255])
print indices
coordinates = zip(indices[0], indices[1])
print coordinates

So here you see a 100x100 pixel black image where a little square in the upper left is set to white. By using the numpy.where() method I retrieved a tuple of two array (indices) where the first array contains the x-coordinates of the white points and the second array contains the y-coordinates of the white pixels. By using the zip() method you can get a list of tuples containing the points. You can convert this list to a numpy array by calling the numpy.asarray() function.

Best Regards

Preview: (hide)

Comments

Thanks, that did help. I'm looking for java code though. :)

bharath95 gravatar imagebharath95 (Jul 27 '16)edit

Question Tools

1 follower

Stats

Asked: Jul 26 '16

Seen: 24,745 times

Last updated: Jul 26 '16