# How to find whether the position is a Peak position in a kernel window?

I am searching for an OpenCV function that replicates the function of peak_local_max in scikit-image. What this function does is, It compares the values over a window of mentioned size whether the anchor point value is greater than or equal to the maximum of other positions or not. If the case satisfies, it considers it as a local peak.

Example of what I want,

Consider my image is 5x5 and I want to find the local peaks in the window size of 3x3. If you could apply the criteria I explained in the question(It compares the values over a window of mentioned size whether the anchor point value is greater than or equal to the maximum of other positions or not), only one value satisfies 10.79. But in the case of minmaxloc it will find first 10.79 later after the mask is updated it will give 10.6 but 10.6 is definitely not the local peak and we wont know when to stop.

Is there a OpenCV function that could do this job? I know about minmaxLoc and minmaxIdx but it calculates for global frame which I am not interested. I am thinking of taking the filter2D function as reference and create my own functionality to do this. On the parallel side I want your suggestions to go about this problem?

Any help could be really appreciated. Thanks in advance!

edit retag close merge delete

( 2017-09-14 09:59:19 -0500 )edit

I mentioned in the question itself, I know about the minMaxLoc but that is not what I expected. Thanks for your help

( 2017-09-15 04:33:04 -0500 )edit

" but it calculates for global frame" no there is a mask. You have to adjust mask relative to the window size you need

( 2017-09-15 04:41:22 -0500 )edit

Got your point! Firstly I don't wan to find the maximum in the window. I want to find the peak whether the anchor point contains the maximum value or not. Secondly My window size is 5x5. For the image of 1280x720 how efficient is this thing?

( 2017-09-15 05:09:46 -0500 )edit

anchor ? filter2D is convolution so anchor is everywhere. I don't understand. A small example is welcome

( 2017-09-15 05:56:59 -0500 )edit

@LBerger: updated my question and I got my answer as well thanks for your help

( 2017-09-27 05:09:35 -0500 )edit

Sort by ยป oldest newest most voted

Hey I found my answer. I used the logic behind dilate operation to find the peak local maximum. It would be very simple than minmaxloc.

Quick background of Dilate operation

As the kernel is scanned over the image, we compute the maximal pixel value overlapped by the kernel and replace the image pixel under the anchor point with that maximal value.

I used this to my advantage, so the maximum value around the window remains the same even when dilation operation is happening, which is our peak value. I did dilation followed by equality comparison to extract the peak positions alone.

//Threshold operation
threshold_frame(input_frame, threshold_frame, threshold_value, max_value, THRESH_BINARY)

//Finding the peak locals
dilate(threshold_frame, dilate_frame, dilateKernel, Point(-1,-1), 1);
peak_local_frame = (dilate_frame == threshold_frame);

more

But as you can see, you are manually thresholding(threshold_value, max_value) the Image and you may need to find this value dynamically in many cases. Since your actual problem is to find the Local Maximum, I think this approach is not an optimum solution! What if the Maximum Value goes less than your threshold?

( 2017-09-27 04:44:48 -0500 )edit

I quoted threshold image as a sample. In fact, I feel minmaxloc wont work for my case at all. Let me update my question with an example

( 2017-09-27 04:57:15 -0500 )edit

Indeed this approach is a quite faster alternative! But this Threshold in your code is misleading! Anyway here is a similar question! http://answers.opencv.org/question/28...

( 2017-09-27 08:57:28 -0500 )edit