Damanpreet kaur
Damanpreet kaur

Reputation: 303

How to select the channel with maximum value at every pixel in a multichannel image?

I want to fetch the channel with maximum value at every pixel in a multichannel image. I want to do with numpy without using any loop. Is there a shortcut to fetch these values?

I can do the same by looping over every pixel value as below:

label_list = []
for i in range(height):
    for j in range(width):
        label = np.where(img[i,j,:] == np.max(img[i,j,:]))
        if label not in label_list:
            label_list.append(label)

Each channel in my image corresponds to one object. I want to know which objects are present in the image.

Upvotes: 1

Views: 2435

Answers (1)

Damanpreet kaur
Damanpreet kaur

Reputation: 303

Argmax can solve the purpose. It returns the indices of the maximum value along an axis.

np.unique(np.argmax(img, axis=2))

Maybe it can help someone else.

Upvotes: 2

Related Questions