Ask Your Question

How to create my own filter to detect a pixel is a local maximum?

asked 2014-02-23 23:43:16 -0500

fbr gravatar image


I am interested into doing this from the Python cv2 wrapper of OpenCV.

I'd like to test each pixel of a gray image to see if it has a value >= to the value of its 9 neighbors.

The output should be a binary mask, preferably of the same size than the input image.

Thanks, F.

edit retag flag offensive close merge delete


You may need to see minMaxLoc()

Haris gravatar imageHaris ( 2014-02-24 00:31:38 -0500 )edit

I am looking for something local, not global.

fbr gravatar imagefbr ( 2014-02-24 01:46:57 -0500 )edit

You can set ROI of your kernel size and perform minMaxLoc()

Haris gravatar imageHaris ( 2014-02-24 02:02:14 -0500 )edit

That sound highly unefficient. A dumb way would be to test explicitely all the neighbor pixels. I'll try that since I don't have a better idea.

fbr gravatar imagefbr ( 2014-02-24 02:31:37 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-02-24 03:29:11 -0500

kbarni gravatar image

You might simply try to implement it yourself, without opencv functions:

Mat image, result;

Sorry for the C code, it's untested and not optimized, but it's easy to understand: you take every pixel (x,y) (except at the border), and you compare it to the 8 neighbors (x-1,y-1 ; x,y-1 ; x+1,y-1 ; ...). If it's bigger, than it's a local maximum.

You can adapt and optimize the code according to your needs. With a little work you can process the borders, too.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2014-02-23 23:43:16 -0500

Seen: 223 times

Last updated: Feb 24 '14