Numpy:如何找到多个通道之间的比率,并在比率高于阈值时分配特定通道的索引



我有N个通道的numpy数组(维度:N*X*Y(。我需要找到N个通道之间每个X,Y的比值。创建一个新数组(维度:X*Y(,如果某个通道的比率大于阈值,则指定该通道的索引(例如N=1(,否则指定最大值索引。假设有2个通道,如果X、 通道1的Y点大于0.3,我需要将1分配给新数组的(X,Y(,如果小于0.4,则分配最大通道索引。请告知,谢谢。

channel1 = arr[0, :, :]将获得通道1中的值作为X乘Y数组。channel2 = arr[1, :, :]将获得通道2中的值。然后channel1 / channel2 > 0.4将在比率大于0.4的任何位置为True的X乘Y阵列。您可以将其转换为int以获得1和0。

我使用下面的代码来解决这个问题。

import numpy as np
#N * X * Y  => 3 * 2 * 3
inputArray = np.array([[[1,1,1],
[1,1,1]],
[[1,2,1],
[1,2,1]],
[[3,3,3],
[3,3,3]]])
#Get the ratio
ratio = inputArray / inputArray .sum(axis=0)
print(ratio)
# Create result array
result = np.zeros((2,3))
#Check if ratio of Channel N =1 > threshold value 0.3, if true assign 1 else assign channel index of maximum ratio.  
result[:,:] = np.where(ratio[1,:,:] > 0.3, 1 ,np.argmax(ratio, axis=0))
print(result)
------------------------------
>ratio:
[[[0.2        0.16666667 0.2       ]
[0.2        0.16666667 0.2       ]]
[[0.2        0.33333333 0.2       ]
[0.2        0.33333333 0.2       ]]
[[0.6        0.5        0.6       ]
[0.6        0.5        0.6       ]]]
> result:
[[2. 1. 2.]
[2. 1. 2.]]

最新更新