# Why do we need to flip the kernel in 2D convolution?

Why do we need to flip the kernel in 2D convolution in the first place? What's the benefit of this? So, why can't we leave it unflipped? What kind of terrible thing can happen if you don't flip it?

SEE: "First, flip the kernel, which is the shaded box, in both horizontal and vertical direction"

http://www.songho.ca/dsp/convolution/convolution2d_example.html

• I'm tempted to say that this is a duplicate of this question, however that one doesn't address 2D convolution. Let's see what others say. – Peter K. May 1 '13 at 19:53
• If that would be a relevant answer, then how would 'scaled and time-delayed versions of the impulse response, not the "flipped"' make sense here? – user8005 May 1 '13 at 20:03
• Because both questions are the same. One just uses a two dimensional "impulse response" rather than a one dimensional impulse response. Both are trying to understand how convolution works. – Peter K. May 1 '13 at 20:09
• I agree with Peter K. "Flipping the kernel" is just part of the definition of convolution, no matter in how many dimensions. – Matt L. May 1 '13 at 20:29
• Duplicate of scicomp.stackexchange.com/questions/6962/… which I already answered. I gave 1D as an example but can update to nD if required. – geometrikal May 2 '13 at 2:43

## 1 Answer

At the risk of repeating what has been said elsewhere, this is how convolution in two dimensions is defined:

$$y(n_1,n_2) = \sum_{k}\sum_{l}x(k,l)h(n_1-k,n_2-l))$$

$x(n_1,n_2)$ is your data and $h(n_1,n_2)$ is the kernel. Note that in order to compute the double sum above, you need to flip $h(n_1,n_2)$ with respect to $x(n_1,n_2)$. Just take $n_1=0$ and $n_2=0$: in this case you need to multiply $x(k,l)$ with the flipped kernel $h(-k,-l)$. In order to compute the output for other values of $n_1$ and $n_2$ you need to move the flipped kernel across the input data.

• Can I choose x(k,l) to be the kernel as well? – user8005 May 1 '13 at 20:36
• Yes, but then your data needs to be flipped. – Matt L. May 1 '13 at 20:36
• I forgot the benefit of the minussigns of h(-k, -l). I thought it was something with the fourier transforms? – user8005 May 1 '13 at 20:37
• It's just how convolution/filtering works. Convolution in the time/spatial domain corresponds to multiplication in the frequency domain, but only if you define convolution properly. – Matt L. May 1 '13 at 20:39
• Are you the same user as in here: scicomp.stackexchange.com/questions/6962/… ? If not, please read the answer and comments in that question. – geometrikal May 2 '13 at 2:47