'Python trace line on figure that records coordinates of every pixel it passes through

I am working with Satellite images and I am recovering transition lines between snow and ice.

I have a reasonably big dataset (100 images) and I picked by hand points along the line of transition (see picture). clicks

I have a little program that allows me to open an image, pick points, and store the coordinates of each click in an array (called "coords", dimensions 100x2). Then, I select the corresponding altitude for each of these clicks to do some calculations.

The issue is that my clicking rate is not homogeneous, and I can "oversample" when picking the points manually (see above). Eventually that impacts the calculations I do over the altitudes recovered: if I oversample an area at lower elevation, then my mean and std will be biased.

Is there a way I could trace a line on the figure that stores the indices of every pixel it passes through (see picture below)? This way if I calculate a mean for the line, I could bypass the sampling problem I face when picking individual points.

Transition zone

Here's how I fill my "coords" array:

def onclick(event):
    global ix, iy
    ix, iy = event.xdata, event.ydata
    
    global coords
    coords.append((ix, iy))

And how I trigger the picking:

coords = []

fig, ax = plt.subplots(constrained_layout=True)
ax.imshow(data)

cid = fig.canvas.mpl_connect('button_press_event', onclick)

plt.show()

Here is an example of what happens: enter image description here

By using the following code:

array = np.zeros((20,40))
array[0:10, 10:40] = 1

coords = np.zeros((100,2))
coords[coords==0] = np.nan
coords[0,:] = (9,0)
coords[1,:] = (9,3)
coords[2,:] = (9,4)
coords[3,:] = (9,10)
coords[4,:] = (28,10)
coords[5,:] = (30,10)
coords[6,:] = (32,10)
coords[7,:] = (34,10)
coords[8,:] = (35,10)
coords[9,:] = (39,10)

plt.imshow(array)
plt.scatter(coords[:,0], coords[:,1])


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source