pbu
pbu

Reputation: 3060

XY coordinates in a image stored as numpy?

i have a 96x96 pixel numpy array, which is a grayscale image. How do i find and plot the x,y cordinate of the maximum pixel intensity in this image?

image = (96,96)

Looks simple but i could find any snippet of code. Please may you help :)

Upvotes: 4

Views: 5134

Answers (1)

Oliver W.
Oliver W.

Reputation: 13459

Use the argmax function, in combination with unravel_index to get the row and column indices:

>>> import numpy as np
>>> a = np.random.rand(96,96)
>>> rowind, colind = np.unravel_index(a.argmax(), a.shape)

As far as plotting goes, if you just want to pinpoint the maximum value using a Boolean mask, this is the way to go:

>>> import matplotlib.pyplot as plt
>>> plt.imshow(a==a.max())
<matplotlib.image.AxesImage object at 0x3b1eed0>
>>> plt.show()

In that case, you don't need the indices even.

Upvotes: 6

Related Questions