How to get coordinates of image pixels in the following form:
array([[x1 , y1],
[ x2, y2],
...,])
I need them in this particular form as an input for another tool (tda-mapper).
So far, I have come to the idea that I need to sample from the image, but I haven't managed to make it yet. I would really appreciate any advice!
P.S. this is just a toy example for testing.
CodePudding user response:
You could use one of the solutions mentioned here to read in an image to a numpy array: Importing PNG files into Numpy?
And then you could use the numpy.argwhere
function numpy.argwhere(image_array > treshold)
to return the indices where the gray value is larger than some threshold
import matplotlib.pyplot as plt
import numpy as np
im = plt.imread('3zu5i.png')
#https://stackoverflow.com/questions/12201577/how-can-i-convert-an-rgb-image-into-grayscale-in-python
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
grey = rgb2gray(im)
coordinates = np.argwhere(grey < 0.99)
That should return an array containing the array indices with grayscale values larger than some threshold
array([[ 41, 280],
[ 41, 281],
[ 41, 282],
...,
[372, 299],
[372, 300],
[372, 301]])