4

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 :)

pbu
  • 2,982
  • 8
  • 44
  • 68
  • [numpy.argmax](http://docs.scipy.org/doc/numpy/reference/generated/numpy.argmax.html#numpy.argmax) – fhdrsdg Mar 30 '15 at 14:35

1 Answers1

6

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.

Oliver W.
  • 13,169
  • 3
  • 37
  • 50
  • But still this does not explain how to find all the indices with maximum occuring values. How do i loop it? – pbu Apr 02 '15 at 12:22
  • @pbu, it sounds to me like you are trying to find *several* peaks, not just one (as I understood from your question). In that case, you should have a look at [this thread](https://stackoverflow.com/questions/1713335/peak-finding-algorithm-for-python-scipy). One of the answers there also links to [scipy.signal.find_peaks_cwt](http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks_cwt.html). – Oliver W. Apr 02 '15 at 13:31