# Image Gradient - Flipping Effect of the Convolution Matrix (Kernel) for Edge Detection

Prewitt operator is a common operator to compute image gradient in edge detection, and I knew that to apply this Prewitt operator to an image is just to convolve them together, say, the image is represent as $$A$$, and as the wikipedia page lays out, the Prewitt operator for *x-gradient is $$G_x=\pmatrix{ -1 & 0 & 1\\\ -1 & 0 & 1\\\ -1 & 0 & 1}$$, for y-gradient is $$G_y=\pmatrix{1 & 1 & 1\\\ 0 & 0 & 0\\\ -1 & -1 & -1}$$

So if I want to compute the x-gradient of image $$A$$, convolve them like this,$$A_x=A*G_x$$, but here comes my problem. $$G_x$$ is the kernel, when convolve it with $$A$$, $$G_x$$ should be flipped which turn it into this, $$G_x=\pmatrix{1 &0 &-1\\\ 1 &0 &-1\\\ 1 &0 &-1}$$ right? If so, the x-gradient will be something like this, $$\nabla A_x=A(x-1,y)-A(x+1,y)$$ , BUT I think it should be $$\nabla A_x=A(x+1,y) - A(x-1,y)$$.

So I presume that either $$G_x$$ and $$G_y$$ should not be flipped when convolve them with $$A$$, or $$G_x$$ doesn't look like the above, and it should be this,$$G_x=\pmatrix{1& 0& -1\\\ 1& 0& -1\\\ 1& 0& -1}$$

Anyone can help me with this?

## 1 Answer

This is a common question and there are a few other answers on this site which may be helpful. What you have noticed is correct. To be clear, convolution is defined by this flipping, it gives the convolution operation some nice maths properties. Non-flipping convolution is called cross-correlation.

For these kernels, the only thing flipping changes is the sign of the result. You can safely use the inverse (flip) of the kernels to get the form you desire.

• But for my case, if I don't flip the matrix, then I'll get the right image-gradient, but if I do flip, the image-gradient will be the inverse, right? Is that ok? Jun 14, 2013 at 8:31
• Yea, I think both should be inverted. Maybe the wikipedia page needs changing. Jun 14, 2013 at 11:12
• This is the reason I always prefer using Central Differences to approximate the gradient. Jun 25, 2019 at 20:36