Ask Your Question

Finding local maximum in grayscale image

asked 2018-09-13 11:46:10 -0500

Machi gravatar image


I'm new to OpenCV and still learning so I'm looking for an as simple as possible solution to my problem.

I have a sample picture that looks like this


I want to find the local maximums in the picture using Python and OpenCV and anything else that could possibly be useful. Scikit-image possibly?

I have researched and found people saying that first I need to dilate the image using a 5x5 kernel with a 0 pixel in the middle and then replace every pixel with the maximum of it's neighbors, although I'm not sure how I should do the 2nd part.

Anyone here willing to help out a noob with a noob-friendly answer? :)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-09-13 12:38:57 -0500

matman gravatar image

updated 2018-09-13 12:43:42 -0500

dilate is a maximum filter. Just use it with the desired size and form (MORPH_RECT, MORPH_ELLIPSE, or a custom kernel). If you want to fill gaps use morphologyEx with MORPH_CLOSE as op flag.

edit flag offensive delete link more


Hmm I have tried

result = cv2.dilate(img, cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)))

but I only get this as the result. It just makes the maximum blobs bigger, I am not sure how to use that for what I need to do.

Machi gravatar imageMachi ( 2018-09-13 14:19:05 -0500 )edit

I don't know exactly what you want. If you try to do a NonMaximumSupression try something like (For C++):

cv::Mat imgMax = dilate(img, getStructuringElement(MORPH_RECT, cv::Size (5, 5));
cv::Mat imgResult.create(img.size(), img.type(), cv::Scalar::all(0));
img.copyTo(imgResult, compare(img, imgMax, CMP_GE);
matman gravatar imagematman ( 2018-09-15 08:32:53 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-09-13 11:46:10 -0500

Seen: 679 times

Last updated: Sep 13 '18