Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openCV filter image - replace kernel with local maximum

Some details about my problem:

I'm trying to realize corner detector in openCV (another algorithm, that are built-in: Canny, Harris, etc).

I've got a matrix filled with the response values. The biggest response value is - the biggest probability of corner detected is.

I have a problem, that in neighborhood of a point there are few corners detected (but there is only one). I need to reduce number of false-detected corners.

Exact problem:

I need to walk through the matrix with a kernel, calculate maximum value of every kernel, leave max value, but others values in kernel make equal zero.

Are there build-in openCV functions to do this?

like image 302
Larry Cinnabar Avatar asked Nov 29 '22 02:11

Larry Cinnabar


2 Answers

I would suggest an original 2-step procedure (there may exist more efficient approaches), that uses opencv built-in functions :

  1. Step 1 : morphological dilation with a square kernel (corresponding to your neighborhood). This step gives you another image, after replacing each pixel value by the maximum value inside the kernel.

  2. Step 2 : test if the cornerness value of each pixel of the original response image is equal to the max value given by the dilation step. If not, then obviously there exists a better corner in the neighborhood.

like image 25
sansuiso Avatar answered Dec 04 '22 03:12

sansuiso


This is how I would do it:

  1. Create a kernel, it defines a pixels neighbourhood.
  2. Create a new image by dilating your image using this kernel. This dilated image contains the maximum neighbourhood value for every point.
  3. Do an equality comparison between these two arrays. Wherever they are equal is a valid neighbourhood maximum, and is set to 255 in the comparison array.
  4. Multiply the comparison array, and the original array together (scaling appropriately).
  5. This is your final array, containing only neighbourhood maxima.

This is illustrated by these zoomed in images:

9 pixel by 9 pixel original image:

enter image description here

After processing with a 5 by 5 pixel kernel, only the local neighbourhood maxima remain (ie. maxima seperated by more than 2 pixels from a pixel with a greater value):

enter image description here

There is one caveat. If two nearby maxima have the same value then they will both be present in the final image.

Here is some Python code that does it, it should be very easy to convert to c++:

import cv

im = cv.LoadImage('fish2.png',cv.CV_LOAD_IMAGE_GRAYSCALE)
maxed = cv.CreateImage((im.width, im.height), cv.IPL_DEPTH_8U, 1)
comp = cv.CreateImage((im.width, im.height), cv.IPL_DEPTH_8U, 1)
#Create a 5*5 kernel anchored at 2,2
kernel = cv.CreateStructuringElementEx(5, 5, 2, 2, cv.CV_SHAPE_RECT)

cv.Dilate(im, maxed, element=kernel, iterations=1)
cv.Cmp(im, maxed, comp, cv.CV_CMP_EQ)
cv.Mul(im, comp, im, 1/255.0)

cv.ShowImage("local max only", im)
cv.WaitKey(0)

I didn't realise until now, but this is what @sansuiso suggested in his/her answer.

This is possibly better illustrated with this image, before:

enter image description here

after processing with a 5 by 5 kernel:

enter image description here

solid regions are due to the shared local maxima values.

like image 147
fraxel Avatar answered Dec 04 '22 02:12

fraxel