从存储在 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

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

问题描述

我有一个图像存储在一个 numpy 数组中,由 imread() 生成:

<预><代码>>>>丁数组([[[ 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)>>>形状(512, 512, 3)

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

<预><代码>>>>C数组([ 32, 32, 109], dtype=uint8)>>>ndim[200,200]数组([ 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) -- 我可以对此进行测试.

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

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

我看过 numpy 的花式索引",这让我非常困惑......我试图解决这个问题的大部分尝试都过度紧张和不必要的巴洛克式.

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

解决方案

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

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

indices 现在应该是一个二元组数组,其中第一个包含第一维中的索引,第二个包含与 <代码>c.

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

coords = zip(索引[0],索引[1])

例如:

导入numpya = numpy.zeros((4, 4, 3), 'int')对于范围内的 n(4):对于范围内的 m(4):a[n, m, :] = n + m如果 (n + m) == 4:打印 n, mc = (4, 4, 4)指数 = numpy.where(numpy.all(a == c,axis=-1))打印索引打印 zip(索引 [0],索引 [1])

将输出:

1 32 23 1(数组([1, 2, 3]), 数组([3, 2, 1]))[(1, 3), (2, 2), (3, 1)]

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

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

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

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

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

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?

解决方案

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

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

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.

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

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

For example:

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

will output:

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

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

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

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