1 | initial version |
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 100100 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
2 | No.2 Revision |
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 100100 100x100 pixel black image where a little square in the upper left is set to white. By using the *numpy.where()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