Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non local maxima suppression in python

Goal: To input an image (2d numpy array) and a window size, and output the same array with the local maxima remaining, but 0 elsewhere.

What I am struggling with: 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.

similar questions: Finding Local Maxima in an Image

and Find local maxima in grayscale image using OpenCV

Mine is different because: 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.

What I have tried so far.

def nonMaximalSupress(image,NHoodSize):
    #For
    for x in range(0,image.shape[0]-1):

        if x+NHoodSize[0]<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 > 0:
                    print localMax
                    print "max coord is ", maxCoord[0], maxCoord[1]
                image[maxCoord[0]][maxCoord[1]]=localMax
                #increment y



        x+=NHoodSize[0]

    return image
like image 386
jfalkson Avatar asked Apr 23 '26 23:04

jfalkson


1 Answers

How about something like this:

# Use the max filter to make a mask
roi = 3
size = 2 * roi + 1
image_max = ndimage.maximum_filter(image, size=size, mode='constant')
mask = (image == image_max)
image *= mask

# Remove the image borders
image[:roi] = 0
image[-roi:] = 0
image[:, :roi] = 0
image[:, -roi:] = 0

# Optionally find peaks above some threshold
image_t = (image > peak_threshold) * 1

# get coordinates of peaks
f = np.transpose(image_t.nonzero())
like image 135
Maurits Avatar answered Apr 26 '26 14:04

Maurits



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!