Ask Your Question

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

asked 2016-07-26 05:28:18 -0500

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.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-07-26 06:41:36 -0500

Missing gravatar image

updated 2016-07-26 06:43:30 -0500

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

edit flag offensive delete link more


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

bharath95 gravatar imagebharath95 ( 2016-07-27 10:13:09 -0500 )edit

Question Tools

1 follower


Asked: 2016-07-26 05:28:18 -0500

Seen: 15,664 times

Last updated: Jul 26 '16