There are four image arrays like this:
image1 = np.array([
[
# [R G B]
# | | |
[1, 2, 3], [11, 22, 55], [12, 45, 56]
],
[
[1, 2, 3], [56, 55, 13], [12, 45, 56]
],
[
[11, 22, 55], [56, 55, 13], [12, 45, 56]
],
])
image2 = np.array([
[
[91, 72, 33], [111, 222, 155], [212, 245, 156]
],
[
[100, 200, 113], [56, 255, 213], [112, 145, 156]
],
[
[113, 223, 255], [156, 55, 113], [212, 245, 156]
],
])
image3 = np.array([
[
[9, 2, 3], [111, 222, 255], [22, 25, 16]
],
[
[10, 20, 13], [156, 25, 23], [12, 45, 16]
],
[
[13, 23, 155], [56, 255, 13], [222, 235, 216]
],
])
image4 = np.array([
[
[29, 22, 23], [111, 222, 255], [223, 125, 216]
],
[
[210, 220, 13], [156, 252, 232], [122, 145, 216]
],
[
[123, 232, 155], [56, 255, 213], [222, 235, 216]
],
])
For each rows and columns, I want to compute the max RGB
value of sum of Red & Green & Blue
channel of the image arrays and use the max to build a new image array.
So after tried the following code, It got duplicate indices, because in each rows and columns the max RGB
of three image arrays may be duplicate. But I just want to get the first match index.
array = np.array([image1, image2, image3, image4])
array_sum = array.sum(axis=3)
# array_sum_max_index = array_sum.argmax(axis=0)
indices = np.where(array_sum == array_sum.max(axis=0))
print(indices)
Output
(array([1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3]), array([0, 0, 2, 0, 2, 0, 1, 1, 1, 2, 2]), array([0, 2, 0, 1, 2, 1, 0, 1, 2, 1, 2]))
There is a similar answer in this link, but it cannot resolve my question. So how to get the index when it first match.
Edit:
The actual number of input images is more than 32
. (Solutions involving numpy.choose()
may not work because of this)