应用n.where后如何重塑Numpy数组?

时间:2019-07-08 12:35:12

标签: python numpy opencv numpy-broadcasting

我有outputimarray,它们的形状相同。在以下代码中,我要将特定像素的颜色设置为colors[0]

colors = [[0,0,255],[0,255,0]]
output[np.where((imarray >= values[0])&(imarray <= values[1]))] = colors[0]

,但是由于output数组被展平为(4413,)的形状而产生错误。错误是:

ValueError: shape mismatch: value array of shape (3,) could not be broadcast to indexing result of shape (4413,)

1 个答案:

答案 0 :(得分:0)

制作3色图像阵列:

In [453]: arr = np.random.randint(0,256,(10,10,3))                                                           
In [454]: arr1=arr.copy()

现在应用where遮罩,将值与某些“颜色”进行比较:

In [455]: arr1[np.where(arr >= [100,100,100])].shape                                                         
Out[455]: (188,)
In [456]: 188/3                                                                                              
Out[456]: 62.666666666666664

在您的示例中,遮罩选择了一个模块3组元素,但实际情况并非如此。它从像素混合中选择数组元素。我怀疑您/我们真的想要满足某种颜色组合的像素。为此,我们需要使用allany将蒙版缩小到2d-像素化

In [457]: np.where(np.all(arr >= [100,100,100],2))                                                           
Out[457]: 
(array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 7, 7, 8, 8, 8, 9, 9]),
 array([2, 4, 6, 9, 1, 3, 5, 6, 3, 8, 4, 8, 3, 1, 3, 5, 3, 4, 5, 4, 6]))

In [458]: arr1[_457] = [0,0,255]   

之所以有效,是因为arr1[Out[457]]是(21,3)-21像素。 np.array([0,0,255])是(3,),广播到(21,3)。


比较用all掩码选择的像素集:

In [460]: arr1[_457]                                                                                         
Out[460]: 
array([[  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255],
       [  0,   0, 255]])

使用3d蒙版选择的一组数组值:

In [461]: arr1[np.where(arr >= [100,100,100])]                                                               
Out[461]: 
array([145, 250, 198,   0,   0, 255,   0,   0, 255, 249, 208, 179, 184,
       100, 128, 219, 217, 128, 202, 200, 127, 118, 149, 236, 199, 136,
       110, 170, 199,   0,   0, 255, 128, 252, 193, 210,   0,   0, 255,
         0,   0, 255, 151, 163,   0,   0, 255, 212, 109, 187, 202, 129,
       110, 206, 137, 219, 227, 115, 130, 150, 201, 190, 231,   0,   0,
       255,   0,   0, 255, 236, 146, 113, 204, 221, 144, 128, 205, 181,
       114,   0,   0, 255, 219, 200, 197, 189, 141, 226, 248,   0,   0,
       255, 218, 245, 180, 251, 140, 253,   0,   0, 255, 143, 147, 143,
       245,   0,   0, 255, 235, 253, 165, 234, 205, 105, 122,   0,   0,
       255, 162, 149, 200, 202, 142, 208, 133, 254, 119, 205, 111,   0,
         0, 255,   0,   0, 255, 180, 158,   0,   0, 255, 170, 205, 103,
       122, 238, 104, 177, 172, 189, 192, 194,   0,   0, 255,   0,   0,
       255,   0,   0, 255, 143, 224, 213, 159, 182, 148, 168, 237, 127,
       233, 230, 244, 172, 216, 107,   0,   0, 255, 111, 254,   0,   0,
       255, 246, 148, 223, 174, 199])

除了我们修改的值外,所有值都等于或大于100,但是我们无法确定哪个属于哪个像素。