Ask Your Question
0

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

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

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
5

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

Missing gravatar image

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

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

Comments

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

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

Question Tools

1 follower

Stats

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

Seen: 24,073 times

Last updated: Jul 26 '16