从NumPy ndarrays中存储的图像中查找特定(R,G,B)颜色值的(x,y)索引

fis*_*000 6 python rgb numpy image multidimensional-array

我有一个存储在numpy数组中的图像,如下所示imread():

>>> ndim
array([[[  0,   0,   0],
        [  4,   0,   0],
        [  8,   0,   0],
        ..., 
        [247,   0,  28],
        [251,   0,  28],
        [255,   0,  28]],

       [[  0, 255, 227],
        [  4, 255, 227],
        [  8, 255, 227],
        ..., 
        [247, 255, 255],
        [251, 255, 255],
        [255, 255, 255]]], dtype=uint8)
>>> ndim.shape
(512, 512, 3)
Run Code Online (Sandbox Code Playgroud)

我想有效地找到具有特定颜色值的像素的(x,y)坐标(或坐标),例如

>>> c
array([ 32,  32, 109], dtype=uint8)

>>> ndim[200,200]
array([ 32,  32, 109], dtype=uint8)

>>> ndim.T[0, 200, 200]
32
>>> ndim.T[1, 200, 200]
32
>>> ndim.T[2, 200, 200]
109
Run Code Online (Sandbox Code Playgroud)

...在这种情况下,我知道(200,200)处的像素具有RGB值(32,32,109) - 我可以测试它.

我想要做的是查询ndarray的像素值并获取坐标.在上面的情况中,推定的函数find_pixel(c)将返回(200,200).

理想情况下,此find_pixel()函数将返回坐标元组列表,而不仅仅是它找到的第一个值.

我看过numpy的"花式索引",这让我非常困惑......我尝试解决这个问题的大多数尝试都是过度的,而且不必要的是巴洛克式的.

我确信有一个非常简单的方法,我在这里忽略.这样做的最佳方法是什么 - 获得这些价值的机制是否比我概述的更好?

Hen*_*all 15

对于某些数组颜色数组a和颜色元组c:

indices = numpy.where(numpy.all(a == c, axis=-1))
Run Code Online (Sandbox Code Playgroud)

indices现在应该是一个2元组的数组,第一个包含第一维中的索引,第二个包含第二维中与像素值对应的索引c.

如果你需要这个作为坐标元组的列表,请使用zip:

coords = zip(indices[0], indices[1])
Run Code Online (Sandbox Code Playgroud)

例如:

import numpy
a = numpy.zeros((4, 4, 3), 'int')    

for n in range(4):
    for m in range(4):
        a[n, m, :] = n + m
        if (n + m) == 4:
            print n, m

c = (4, 4, 4)
indices = numpy.where(numpy.all(a == c, axis=-1))
print indices
print zip(indices[0], indices[1])
Run Code Online (Sandbox Code Playgroud)

将输出:

1 3
2 2
3 1
(array([1, 2, 3]), array([3, 2, 1]))
[(1, 3), (2, 2), (3, 1)]
Run Code Online (Sandbox Code Playgroud)

它对应于所有有价值的像素(4,4,4).

  • 而不是zip(...),可以使用[docs]中提到的numpy.transpose(indices)(https://docs.scipy.org/doc/numpy/reference/generated/numpy.nonzero.html# numpy.nonzero).这可能会略微提高性能.... (3认同)
  • 就目前而言,它应该适用于最后一个轴包含颜色信息的任何数组.因此,您可以创建一个4-D数组,其中第一个轴索引图像 - 类似于`a = numpy.empty((5,512,512,3),dtype ='int'); a [0,:,:,] = image0; a [1,:,:,]] = image1 ...`.`indices`现在将包含一个额外的条目(在开头),它们是图像索引.如果你想让颜色轴成为最后一个轴,你可能需要使用`c`的结构(提高它的维度或其他东西). (2认同)