从存储在NumPy ndarrays中的图像中查找特定(R,G,B)颜色值的(x,y)索引 [英] Finding the (x,y) indexes of specific (R,G,B) color values from images stored in NumPy ndarrays

查看:284
本文介绍了从存储在NumPy ndarrays中的图像中查找特定(R,G,B)颜色值的(x,y)索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将图像存储在numpy数组中,如imread()所示:

I have an image stored in a numpy array, as yielded by 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)

我想有效地找到具有特定颜色值(例如,

I want to efficiently find the (x, y) coordinate (or coordinates) of pixels with a specific color value, e.g.

>>> 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

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

... in this case, I know the pixel at (200, 200) has the RGB value (32, 32, 109) -- I can test for this.

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

What I want to do is query the ndarray for a pixel value and get back the coordinates. In the above case, the putative function find_pixel(c) would return (200, 200).

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

Ideally this find_pixel() function would return a list of coordinate tuples and not just the first value it finds.

我看过numpy的花式索引",这使我感到非常困惑.我为弄清楚这一点所做的大多数尝试都是过度紧张和不必要的巴洛克式.

I've looked at numpy's "fancy indexing", which confused me greatly... Most of my attempts at figuring this out have been overwrought and unnecessarily baroque.

我确定这里有一个非常简单的方法可以忽略.做到这一点的最佳方法是什么?是否有一种比我概述的更好的机制来获得这些价值?

I am sure there is a very simple method that I am overlooking here. What is the best way to do this -- is there an altogether better mechanism to get these values than that which I have outlined?

推荐答案

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

For some array colour array a and a colour tuple c:

indices = numpy.where(numpy.all(a == c, axis=-1))

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

indices should now be a 2-tuple of arrays, the first of which contains the indices in the first dimensions and the second of which contains the indices in the second dimension corresponding to pixel values of c.

如果需要将其作为坐标元组列表,请使用zip:

If you need this as a list of coordinate tuples, use zip:

coords = zip(indices[0], indices[1])

例如:

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])

将输出:

1 3
2 2
3 1
(array([1, 2, 3]), array([3, 2, 1]))
[(1, 3), (2, 2), (3, 1)]

对应于所有值(4、4、4)的像素.

which corresponds to all the pixels of value (4, 4, 4).

这篇关于从存储在NumPy ndarrays中的图像中查找特定(R,G,B)颜色值的(x,y)索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆