# Creating a convolution kernel from filter description

I am not quite sure whether the question I am stating is right, or if I am trying to combine two not related topics but here it comes..

I am trying implement and apply a contra harmonic mean filter, but do not know how the kernel should look like, and why it should like that, or even if should do it with a convolutional kernel....

The contra harmonic mean filter is described here.

http://www.blackice.com/Help/Tools/Document%20Imaging%20SDK%20webhelp/WebHelp/Contra-Harmonic_Mean_Filter.htm

Sorry for being a noob within this area, but seems like this information isn't stated anywhere, and I kinda get the feeling that it is some form of knowledge everybody should have, but not me...

I am using filter2D to create my filter..

The key point in the description on the linked-to page is:

This function will filter the image by the nonlinear contra-harmonic mean method.

that the method in question is nonlinear.

That means convolution kernels are not really applicable, because they are generally only useful for linear, time-invariant systems.

Having said that, you may be able to use a simple linear convolution kernel in the process of calculating this mean.

You need to calculate: $$\sum_{(i,j) \in M} A(x+i,y+j)^Q$$

where $Q$ is either $P$ or $P+1$ and $M$ is an $N\times N$ square mask ($\square$).

One way to do this would be to:

• Calculate the image to the power $P+1 = I_1 = A(x,y)^{P+1}$.
• Calculate the image to the power $P = I_2 = A(x,y)^{P}$.
• Form $$I_\mbox{NUM} = \square * I_1\\ I_\mbox{DEN} = \square * I_2\\ \mbox{Contra-harmonic mean} = \frac{I_\mbox{NUM}}{I_\mbox{DEN}}$$

Here, the convolution kernel, $\square$ is just: $$\square(x,y) = \left\{ \begin{array}{cl} 1 & |x|,|y| < N/2\\ 0 & \mbox{otherwise} \end{array} \right .$$ i.e. it's just a square $N \times N$ kernel of all ones.

• I am not sure i get why that this is linear, rather than the other methods. So you convolute the whole image with an matrix consisting of ones. but how?? Are you transforming the while image into I_1, I_2 and then convoluting that with an matrix consisting of ones.. If so, i don't get the difference of doing that Or just dividing I_1/I_2.. How does convoluting it with an matrix consisting of ones alter I_1 or I_2. – Bob Burt Nov 2 '15 at 17:18
• If all you do is divide $I_1$ by $I_2$ then you'll just get $A$. So you need to do some averaging (the summation over $M$ in the equation on the page you linked to). Not sure what you mean by "the other methods"? This is NOT linear. The only linear part is filtering $I_1$ to get $I_\mbox{NUM}$ and $I_2$ to get $I_\mbox{DEN}$. – Peter K. Nov 2 '15 at 17:24
• I am not sure if I am getting it, what the purpose of the square matrix.. I don't see what the difference between I_NUM and I_1... – Bob Burt Nov 2 '15 at 17:34
• Suppose $I_1$ = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]; and $N=2$ then $I_\mbox{NUM}$ is [22 26 30 ...] where 22 = 1 + 2 + 9 _+ 10. – Peter K. Nov 2 '15 at 17:37
• I tried implementing it in OpenCV.. And doesn't seem like i am doing it right.. My output is a black image.. – Bob Burt Nov 2 '15 at 20:01