I want to find local maxima of each '3X3' sized Window. So, How do we find that local maxima of each 3X3 sized Window in an image in OpenCV ?
Asked
Active
Viewed 5,189 times
-2
-
1Duplicate of http://stackoverflow.com/questions/5550290/find-local-maxima-in-grayscale-image-using-opencv?rq=1 – old-ufo May 15 '14 at 13:44
2 Answers
1
You can use morphological operation dilate:
Mat img; // your input image that you should fill with values
Mat maxims(img.size(), img.type()); // container for all local maximums
dilate(img, maxims, Mat());
As a result each pixel of 'maxims' is maximum of appropriate 3x3 window in 'img'. Read more about morphological operation (dilatation, erosion, close, open, etc...) on Wikipedia or somewhere else.

Michael Burdinov
- 4,348
- 1
- 17
- 28
-
-
2@a-Jays, because calling it large amount of times (as the number of pixels) on 3x3 matrices is not efficient. The call itself will be more expensive than detection of the maximum, since it involves all kinds of initializations like definition of temporary matrix header and so on. It is ignorable when used on matrix of considerable size but not on those tiny matrices. On the other hand, dilate() is highly optimized for this specific task. And the detection of maximums is much cleaner - just one simple line of code, instead of loop that you will need for minMaxLoc. – Michael Burdinov May 17 '14 at 09:39
0
Please see my answer to Find local maxima in grayscale image using OpenCV
The idea is to dilate with a kernel that has a "hole" in the middle (i.e. replace each pixel with the maximum of all its neighbors, excluding the pixel itself), and then compare the result to the original image.