Skip to content

Instantly share code, notes, and snippets.

@mdauphin
Last active November 5, 2015 18:38
Show Gist options
  • Save mdauphin/71637b067479d3d3ed89 to your computer and use it in GitHub Desktop.
Save mdauphin/71637b067479d3d3ed89 to your computer and use it in GitHub Desktop.
import numpy as np
def LocalMaximaFinder( im, kernel = [ 32, 32 ], threshold = 0, min_distance = 0 ):
( h, w ) = im.shape
results = []
for y in range(0,h,kernel[0]):
for x in range(0,w,kernel[1]):
sub = im[y:y+kernel[0],x:x+kernel[1]]
max_pxl_value = np.max(sub)
if max_pxl_value > threshold:
max_pos = np.array(np.unravel_index(np.argmax(sub),sub.shape)) + [y,x]
#distance
if len(results) > 0:
all_pos = np.array([ x[1] for x in results ])
print np.min( np.linalg.norm( all_pos - max_pos, axis=1 ) )
if np.min( np.linalg.norm( all_pos - max_pos, axis=1 ) ) > min_distance:
results.append( (max_pxl_value, tuple(max_pos)) )
else:
results.append( (max_pxl_value, tuple(max_pos)) )
#sort
results = sorted( results, key=lambda x: x[0], reverse=True )
return [ x[1] for x in results ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment