# kernels to compute second order derivative of digital image

For an image $I$, its first order derivatives can be computed using several oprators, such as $$K_{sobel} = \left[ \begin{array}{ccc} -1 &0 &1 \\ -2 &0 &2 \\ -1 &0 &1 \end{array}\right]$$ $$I_{x} = I * K_{sobel},\ I_{y} = I * K_{sobel}.T$$. And to compute the second order derivatives $(I_{xx}, I_{yy}, I_{xy})$, I can apply $K_{sobel}$ twice on $I$, however, I wonder are there any kernels which can be applied once to get the second order derivatives.

Here is what I tried, I made up 2 kernels, $$K_{xx} = \left[ \begin{array}{ccc} 0 &0 &0 \\ 2 &-4 &2 \\ 0 &0 &0 \end{array}\right]$$ $$K_{yy} = \left[ \begin{array}{ccc} 0 &2 &0 \\ 0 &-4 &0 \\ 0 &2 &0 \end{array}\right]$$ and $I_{xx} = I * K_{xx},\ I_{yy} = I * K_{yy}$, they look pretty much the same as twice-applied-sobel results, but I failed to find a good kernel for $I_{xy}$, I need your help, any idea is appreciated.

## 2 Answers

Convolution $$(*)$$ is associative, so

$$I_{xx} = K_{sobel} * (K_{sobel} * I) = (K_{sobel} * K_{sobel}) * I$$

meaning the convolution of a 1st order Sobel filter kernel with another 1st order Sobel filter gives the filter kernel for a second order filter. So e.g. a 2nd order horizontal Sobel filter kernel is:

$$\left( \begin{array}{ccc} -1 & 0 & 1 \\ -2 & 0 & 2 \\ -1 & 0 & 1 \\ \end{array} \right) * \left( \begin{array}{ccc} -1 & 0 & 1 \\ -2 & 0 & 2 \\ -1 & 0 & 1 \\ \end{array} \right)=\left( \begin{array}{ccccc} 1 & 0 & -2 & 0 & 1 \\ 4 & 0 & -8 & 0 & 4 \\ 6 & 0 & -12 & 0 & 6 \\ 4 & 0 & -8 & 0 & 4 \\ 1 & 0 & -2 & 0 & 1 \\ \end{array} \right)$$

And for $$I_{xy}$$:

$$\left( \begin{array}{ccc} -1 & 0 & 1 \\ -2 & 0 & 2 \\ -1 & 0 & 1 \\ \end{array} \right) * \left( \begin{array}{ccc} -1 & -2 & -1 \\ 0 & 0 & 0 \\ 1 & 2 & 1 \\ \end{array} \right)=\left( \begin{array}{ccccc} 1 & 2 & 0 & -2 & -1 \\ 2 & 4 & 0 & -4 & -2 \\ 0 & 0 & 0 & 0 & 0 \\ -2 & -4 & 0 & 4 & 2 \\ -1 & -2 & 0 & 2 & 1 \\ \end{array} \right)$$

If you want a 3x3 kernel, you can use the 1st order derivative filter $$\left( \begin{array}{cc} -1 & 1 \\ -1 & 1 \\ \end{array} \right)$$, which leads to the second order filters:

$$\left( \begin{array}{cc} -1 & 1 \\ -1 & 1 \\ \end{array} \right)*\left( \begin{array}{cc} -1 & 1 \\ -1 & 1 \\ \end{array} \right)=\left( \begin{array}{ccc} 1 & -2 & 1 \\ 2 & -4 & 2 \\ 1 & -2 & 1 \\ \end{array} \right)$$

and

$$\left( \begin{array}{cc} -1 & -1 \\ 1 & 1 \\ \end{array} \right)*\left( \begin{array}{cc} -1 & 1 \\ -1 & 1 \\ \end{array} \right)=\left( \begin{array}{ccc} 1 & 0 & -1 \\ 0 & 0 & 0 \\ -1 & 0 & 1 \\ \end{array} \right)$$

• Hi. Would you mind adding a couple of instruction steps for how to get from [K]*[K] to the second order kernel? I tried on paper but could not figure out how to derive the weights in the 5x5 matrix.. – Pa_ Mar 1 '15 at 11:48
• This might sound stupid, but this is not matrix multiplication, what is this ?? From the answer post – user16217 Jun 11 '15 at 23:56
• @SamChan: It's discrete convolution (en.wikipedia.org/wiki/Convolution#Discrete_convolution) – Niki Estner Jun 12 '15 at 6:22

What you are looking for is the so-called "Laplacian operator" (http://en.wikipedia.org/wiki/Laplace_operator and http://en.wikipedia.org/wiki/Discrete_Laplace_operator). In image processing, people often use the Laplacian of Gaussian, which is simply the difference between the two results of convolving one input image with two different Gaussian kernels. You may check details here (http://fourier.eng.hmc.edu/e161/lectures/gradient/node9.html).

• First: The Laplacian is Lxx + Lyy, not Lxy. Second: Laplacian of Gaussian (LoG) and Difference of Gaussian (DoG) are not the same. DoG can be used to approximate LoG, because the result is similar, but not the same. – Niki Estner Sep 4 '13 at 17:55
• +1 because it's exactly what I'm looking for! ;-) – uhoh Jul 26 '20 at 17:59