在给定索引列表的情况下更新RGB图像值

时间:2018-03-26 15:17:25

标签: arrays python-3.x numpy python-imaging-library

我的图片为(240,320,3)

我想将一些值设置为[255,0,0]。

给出如下索引:

indexes
[array([0, 0]),
 array([0, 1]),
 array([0, 2]),
 array([0, 3]),
 array([0, 4]),
 array([0, 5]),
 array([0, 6]),
 array([0, 7]),
 array([0, 8]),
 array([0, 9]),
 array([ 0, 10]),
 array([ 0, 11]),
 array([ 0, 12]),
 array([ 0, 13]),
 array([ 0, 14]),
 array([ 0, 15]),
 array([ 0, 16]),
 array([ 0, 17]),
 array([ 0, 18]),
 array([ 0, 19]),
 array([ 0, 20]),
 array([ 0, 21]),
 array([ 0, 22]),
 array([ 0, 23]),
 array([ 0, 24]),
 array([ 0, 25]),
 array([ 0, 26]),
 array([ 0, 27]),
 array([ 0, 28]),
 array([ 0, 29]),
 array([ 0, 30]),
 array([ 0, 31]),
 array([ 0, 32]),
 array([ 0, 33]),
 array([ 0, 34]),
 array([ 0, 35]),
 array([ 0, 36]),
 array([ 0, 37]),
 array([1, 0]),
 array([1, 1]),
 array([1, 2]),
 array([1, 3]),
 array([1, 4]),
 array([1, 5]),
 array([1, 6]),
 array([1, 7]),
 array([1, 8]),
 array([1, 9]),
 array([ 1, 10]),
 array([ 1, 11]),
 array([ 1, 12]),
 array([ 1, 13]),
 array([ 1, 14]),
 array([ 1, 15]),
 array([ 1, 16]),
 array([ 1, 17]),
 array([ 1, 18]),
 array([ 1, 19]),
 array([ 1, 20]),
 array([ 1, 21]),
 array([ 1, 22]),
 array([ 1, 23]),
 array([ 1, 24]),
 array([ 1, 25]),
 array([ 1, 26]),
 array([ 1, 27]),
 array([ 1, 28]),
 array([13, 34]),
 array([13, 35]),
 array([13, 36]),
 array([13, 37]),
 array([14,  0]),
 array([14,  1]),
 array([14,  2]),
 array([14,  3]),
 array([14,  4]),
 array([14,  5]),
 array([14,  6]),
 array([14,  7]),
 array([14,  8]),
 array([14,  9]),
 array([14, 10]),
 array([14, 11]),
 array([14, 12]),
 array([14, 13]),
 array([14, 14]),
 array([14, 15]),
 array([14, 16]),
 array([14, 17]),
 array([14, 18]),
 array([14, 19]),
 array([14, 20]),
 array([14, 21]),
 array([14, 22]),
 array([14, 23]),
 array([14, 24]),
 array([14, 25]),
 array([14, 26]),
 array([14, 27]),
 array([14, 28]),
 array([14, 29]),
 array([14, 30]),
 array([14, 31]),
 array([14, 32]),
 array([14, 33]),
 array([14, 34]),
 array([14, 35]),
 array([14, 36]),
 array([14, 37]),
 array([15,  0]),
 array([15,  1]),
 array([15,  2]),
 array([15,  3]),
 array([15,  4]),
 array([15,  5]),
 array([16, 13]),
 array([16, 14]),
 array([16, 15]),
 array([16, 16]),
 array([16, 17]),
 array([16, 18]),
 array([16, 19]),
 array([17, 24]),
 array([17, 25]),
 array([17, 26]),
 array([17, 27]),
 array([17, 28]),
 array([17, 29]),
 array([17, 30]),
 array([17, 31]),
 array([17, 32]),
 array([18,  0]),
 array([18,  1]),
 array([22, 18]),
 array([22, 19]),
 array([22, 20]),
 array([22, 21]),
 array([22, 22]),
 array([22, 23]),
 array([22, 24]),
 array([22, 25]),
 array([22, 26]),
 array([22, 27]),
 array([22, 28]),
 array([22, 29]),
 array([22, 30]),
 array([22, 31]),
 array([22, 32]),
 array([22, 33]),
 array([22, 34]),
 array([22, 35]),
 array([22, 36]),
 array([22, 37]),
 array([22, 38]),
 array([22, 39]),
 array([22, 40]),
 array([22, 41]),
 array([22, 42]),
 array([22, 43]),
 array([22, 44]),
 array([22, 45]),
 array([22, 46]),
 array([26,  1]),
 ...]

现在我加载图片

from PIL import Image
img=Image.open(image)
img=np.array(img)

鉴于indexes列表,我想设置img[indexes]=[255,0,0]

img[indexes]=[255,0,0]有什么问题?

它似乎没有完成这项工作:

img[indexes[0]]其中indexes[0]=[0,0]

返回一个数组数组,但是我应该在索引(0,0)处得到RGB向量。

比我尝试img[[0,0]]我得到了相同的结果。

这意味着img [[0,0]] == img [indexes [0]]返回一个数组数组而不是索引处的RGB向量(0,0)

但是,img[indexes[0,0],indexes[0,1]]会返回正确的RGB矢量。

我的问题

如何将索引列表传递给我的图像以更新给定索引的值,如下所示;

img[indexes]=[255,0,0]

谢谢

编辑1 IMG [指数[0]] 返回

array([[[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ..., 
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ..., 
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]], dtype=uint8)

其中l' m应该得到对应于索引(0,0)的RGB矢量[223,15,78]

和img [indexes]重新演绎

array([[[[  0,   0,   0],
         [  0,   0,   0],
         [  0,   0,   0],
         ..., 
         [  0,   0,   0],
         [  0,   0,   0],
         [  0,   0,   0]],

        [[  0,   0,   0],
         [  0,   0,   0],
         [  0,   0,   0],
         ..., 
         [  0,   0,   0],
         [  0,   0,   0],
         [  0,   0,   0]]],


       [[[  0,   0,   0],
         [  0,   0,   0],
         [  0,   0,   0],
         ..., 
         [  0,   0,   0],
         [  0,   0,   0],
         [  0,   0,   0]],

        [[  0,   0,   0],
         [  0,   0,   0],
         [  0,   0,   0],
         ..., 
         [  0,   0,   0],
         [  0,   0,   0],
         [  0,   0,   0]]],


       [[[  0,   0,   0],
         [  0,   0,   0],
         [  0,   0,   0],
         ..., 
         [  0,   0,   0],
         [  0,   0,   0],
         [  0,   0,   0]],

        [[  0,   0,   0],
         [  0,   0,   0],
         [  0,   0,   0],
         ..., 
         [  0,   0,   0],
         [  0,   0,   0],
         [  0,   0,   0]]],


       ..., 
       [[[  0,   0,   2],
         [  0,   0,   2],
         [  0,   0,   2],
         ..., 
         [  0,   2,   0],
         [  0,   2,   0],
         [  0,   2,   0]],

        [[108, 106,  93],
         [108, 106,  93],
         [109, 105,  93],
         ..., 
         [170, 165, 143],
         [177, 175, 150],
         [173, 171, 146]]],


       [[[  0,   0,   2],
         [  0,   0,   2],
         [  0,   0,   2],
         ..., 
         [  0,   2,   0],
         [  0,   2,   0],
         [  0,   2,   0]],

        [[129, 127, 114],
         [129, 127, 114],
         [130, 126, 114],
         ..., 
         [155, 150, 128],
         [164, 164, 138],
         [172, 172, 146]]],


       [[[  0,   0,   2],
         [  0,   0,   2],
         [  0,   0,   2],
         ..., 
         [  0,   2,   0],
         [  0,   2,   0],
         [  0,   2,   0]],

        [[150, 133, 126],
         [149, 132, 125],
         [147, 130, 123],
         ..., 
         [162, 135, 124],
         [179, 152, 143],
         [184, 157, 148]]]], dtype=uint8)

图片样本:

img[140:200]
array([[[159, 146, 114],
        [115, 100,  71],
        [ 90,  73,  45],
        ..., 
        [245, 187, 183],
        [252, 197, 194],
        [253, 198, 195]],

       [[206, 193, 159],
        [164, 149, 118],
        [119, 102,  72],
        ..., 
        [243, 188, 185],
        [246, 192, 190],
        [251, 197, 195]],

       [[195, 182, 148],
        [182, 167, 134],
        [150, 134, 101],
        ..., 
        [241, 185, 184],
        [245, 191, 189],
        [251, 200, 197]],

       ..., 
       [[251, 234, 244],
        [251, 234, 244],
        [251, 234, 244],
        ..., 
        [104,  77,  70],
        [102,  75,  68],
        [102,  75,  68]],

       [[251, 234, 244],
        [251, 234, 244],
        [251, 234, 244],
        ..., 
        [ 94,  69,  62],
        [ 91,  66,  59],
        [ 89,  64,  57]],

       [[251, 234, 244],
        [251, 234, 244],
        [251, 234, 244],
        ..., 
        [ 85,  62,  56],
        [ 81,  58,  52],
        [ 78,  58,  51]]], dtype=uint8)

1 个答案:

答案 0 :(得分:1)

我认为您遇到的问题是 advanced indexing 与简单索引,如numpy documentation中所述。

给出一个虚拟img形状的数组(240,320,3),我们有类似的东西:

img = np.random.randint(1,255, (240,320,3))
print img

array([[[ 25, 160, 160],
        [238, 222, 252],
        [  7,  73,  81],
        ...,
        [144, 198,  83],
        [186, 204, 150],
        [249, 234, 105]],

       [[ 52, 242, 214],
        [230, 139, 165],
        [ 95,  69, 168],
        ...,
        [ 40, 226, 111],
        [190, 114, 165],
        [235, 189, 108]],

       [[146, 245,  22],
        [ 88, 156,  27],
        [120, 112,  13],
        ...,
        [220,  31, 119],
        [ 67, 117,  65],
        [108, 145, 196]],
     ...,

如果我们想要访问第一个嵌套数组([25, 160, 160])中的第一个元素,我们可以通过简单的索引来调用它:

print img[0,0]   # array([ 25, 160, 160])

但是,当您传入索引数组时,numpy会将索引解释为高级:img[[0,0]]两次返回img[0]img[[0,0,0]]三次返回img[0]!这就是为什么当你拨打img[indexes]时,你会看到形状奇特的数组。

要获得img列表中指示的将indexes值更新为[255,0,0]所需的结果,您需要先将indexes内的每个索引转换为元组;这将指示numpy将您的数组解释为简单的索引:

an_index = np.array([0,0])
( img[0,0] == img[tuple(an_index)] ).all()  # True

要将indexes转换为元组,您可以使用map

indexes = np.random.randint(0,10,(100,2))
index_of_tuples = map(tuple, indexes)
img[index_of_tuples] = [255,0,0]

现在,img的值将更新为[255,0,0],如原始indexes中所示。