Python 关于分析图像数组的唯一值

Python 关于分析图像数组的唯一值,python,numpy,image-processing,scipy,pillow,Python,Numpy,Image Processing,Scipy,Pillow,有下面的函数读取图像,我添加了几行重新输出图像,并输出图像数组的不同像素值。图像看起来像这样。然而 a,index=np.unique(img,return\u index=True) 给予 a[01] 指数[0 879385] 似乎图像数组有两个唯一的值[01],这是有意义的,但是索引指示什么呢 def _get_image_data_pil(image_id, image_type, return_exif_md=False, return_shape_only=False): fn

有下面的函数读取图像,我添加了几行重新输出图像,并输出图像数组的不同像素值。图像看起来像这样。然而 a,index=np.unique(img,return\u index=True)

给予

a[01]

指数[0 879385]

似乎图像数组有两个唯一的值[01],这是有意义的,但是索引指示什么呢

def _get_image_data_pil(image_id, image_type, return_exif_md=False, return_shape_only=False):
    fname = get_filename(image_id, image_type)
    try:
        img_pil = Image.open(fname)
    except Exception as e:
        assert False, "Failed to read image : %s, %s. Error message: %s" % (image_id, image_type, e)

    if return_shape_only:
        return img_pil.size[::-1] + (len(img_pil.getbands()),)
    # -----
    # this is what I adde
    # -----

    img = np.asarray(img_pil)
    plt.imshow(img)
    plt.show()
    a,indices =np.unique(img,return_index=True)
    print('a ',a)
    print('indices ',indices)

    assert isinstance(img, np.ndarray), "Open image is not an ndarray. Image id/type : %s, %s" % (image_id, image_type)
    if not return_exif_md:
        return img
    else:
        return img, img_pil._getexif()

索引给出展平输入数组中首次出现的唯一值。要将这些索引转换为输入的二维索引,可以使用
np.unravel\u index

例如,假设
img
的形状为(10001600):

np.unlavel\u索引
返回两个数组,每个维度一个。也就是说,第一个数组保存第一维度的索引(即行),第二个数组保存第二维度的索引(即列)。要将它们放入坐标数组,可以使用
np.column\u stack

In [113]: np.column_stack(np.unravel_index(indices, shape))
Out[113]: 
array([[  0,   0],
       [549, 985]])

可能是每个值的第一个像素的索引。
In [113]: np.column_stack(np.unravel_index(indices, shape))
Out[113]: 
array([[  0,   0],
       [549, 985]])