title: Non local maxima suppression in python
body: <p><strong>Goal:</strong> To input an image (2d numpy array) and a window size, and output the same array with the local maxima remaining, but 0 elsewhere. </p>
  
  <p><strong>What I am struggling with:</strong> I think I made a stupid mistake in my code, maybe a few typos in my loop but I am not sure (the local maxima are only on the left side of the image, which is not true). As I note below I would also welcome any easy tricks with OpenCV or numpy to make this solution shorter. </p>
  
  <p>similar questions: <a href="https://stackoverflow.com/questions/23680073/finding-local-maxima-in-an-image">Finding Local Maxima in an Image</a></p>
  
  <p>and <a href="https://stackoverflow.com/questions/5550290/find-local-maxima-in-grayscale-image-using-opencv?rq=1">Find local maxima in grayscale image using OpenCV</a></p>
  
  <p><strong>Mine is different because:</strong> I am looking to surpress all but local maxima. I don't have to use my code below, I haven't been able to find a built in function of opencv or numpy to do what I need to (harris corners in cv will implicitly do this as one of the steps, but I need something to perform the sole operation I need). I read a little bit on dilate and wasn't sure if that would be useful here as well. </p>
  
  <p>What I have tried so far. </p>
  
  <pre><code>def nonMaximalSupress(image,NHoodSize):
      #For
      for x in range(0,image.shape[0]-1):
  
          if x+NHoodSize[0]&lt;image.shape[0]:
              #while we can still take a square
              #print "AHH ", image.shape
              startWindow=0
              for y in range(startWindow,image.shape[1]-NHoodSize[1]):
                  #try:
                  if np.sum(image[x:x+NHoodSize[0]][y:y+NHoodSize[1]])==0:
                      localMax=0
                  else:
                      localMax = np.amax(image[x:x+NHoodSize[0]][y:y+NHoodSize[1]])
                  #except ValueError:
                      #localMax=0
                  #print "local max is ", localMax
  
                  maxCoord=np.unravel_index(np.argmax((image[x:x+NHoodSize[0],y:y+NHoodSize[1]])),
                                            image.shape)+np.array((x,y))
  
                  #print "X is %r, Y is %r, max coord is %r \n y+nhood is %r" %(x,y,maxCoord,y+NHoodSize[1])
                  #suppress everything
                  image[x:x+NHoodSize[0]][y:y+NHoodSize[1]]=0
  
                  #reset only the max
                  #print maxCoord
                  if localMax &gt; 0:
                      print localMax
                      print "max coord is ", maxCoord[0], maxCoord[1]
                  image[maxCoord[0]][maxCoord[1]]=localMax
                  #increment y
  
  
  
          x+=NHoodSize[0]
  
      return image
  </code></pre>