【问题标题】:Extract coordinates from image to numpy从图像中提取坐标到numpy
【发布时间】:2026-01-02 04:25:01
【问题描述】:

如何获取如下形式的图像像素坐标:

array([[x1 , y1],
       [ x2, y2],
           ...,])

我需要这种特殊形式的它们作为另一个工具(tda-mapper)的输入。

到目前为止,我已经想到我需要从图像中取样,但我还没有设法做到。我真的很感激任何建议!

附:这只是一个用于测试的玩具示例。

【问题讨论】:

    标签: python numpy image-processing computer-vision pixel


    【解决方案1】:

    您可以使用此处提到的解决方案之一将图像读入 numpy 数组:Importing PNG files into Numpy?

    然后你可以使用numpy.argwhere函数numpy.argwhere(image_array > treshold)返回灰度值大于某个阈值的索引

    import matplotlib.pyplot as plt
    import numpy as np
    
    im = plt.imread('3zu5i.png')
    
    #https://*.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)
    

    这应该返回一个数组,其中包含灰度值大于某个阈值的数组索引

    array([[ 41, 280],
           [ 41, 281],
           [ 41, 282],
           ...,
           [372, 299],
           [372, 300],
           [372, 301]])
    

    【讨论】:

    • 这有帮助吗?