# How do convolution matrices work?

How do those matrices work? Do I need to multiple every single pixel? How about the upperleft, upperright, bottomleft and bottomleft pixels where there's no surrounding pixel? And does the matrix work from left to right and from up to bottom or from up to bottom first and then left to right?

Why does this kernel (Edge enhance):

turn into this image:

• The source image of the transformation is missing. (Also note that I put the images inline.) Commented Mar 12, 2013 at 9:21
• Please don't crosspost without giving each community time to answer! (The crosspost has an accepted answer.) Commented Mar 13, 2013 at 8:38

Convolution matrix are mean to change your image so you have to treat all the pixels.

For pixels without surrounding pixels there are different way to treat than dependently of the result you expect. You can ignore them and get a smaller image. Put an arbitrary value for their surrounding. Or consider the surrounding being symmetrical to the image.

Since the matrix apply on the image it doesn't mater if you apply it up to bottom, left to right or whatever. Each pixel of the result is computed as a local result of the convolution on the image.

For example for an image $$\begin{pmatrix} a &b&c&d\\ e&f&g&h\\ i&j&k&l\\ m&n&o&p \end{pmatrix}$$ If we discard the surrounding pixels the result of the convolution with you matrix will be :

$$\begin{pmatrix} f-e&g-f\\ j-i&k-j \end{pmatrix}$$

So for your convolution, a pixel $(i,j)$ is defined as $I_{(i,j)}-I_{(i,j-1)}$. So this convolution stress the differences between pixels hence show the edges.

I hope its clear.

• What does 'f-e' mean? Take the value (from 0 to 255) from e and substract it from f and store it in the 'new f'? and 'g-f' means take the value of the old 'f' and substract it from g? And What does I(i,j)-I(i,j-1) mean? You substract j with 'j-1=i', so it becomes j-i? Commented Mar 12, 2013 at 11:51
• Convolution doesn't change the image but create a new one. $f-e$ mean that the result of the convolution for this pixel is equal to the value of this pixel in the image f minus the value of its left neighbour e. $I_{(i,j)}$ represent the value (from 0 to 255) of the pixel in the $i^{th}$ row, $j^{th}$ column. So if we denote $R$ the result of the convolution for an image $I$ we have $\forall i,j\in[2,n-1], R_{(i,j)}=I_{(i,j)}-I_{(i,j-1)}$.
– wece
Commented Mar 12, 2013 at 12:57
• you may have a problem with the fact that the result is not necessarily in [0,255], is that the problem? if it is, there are different solutions: either you normalize to bring back the value in [0,255] (here for example you can add 255 and divide by 2) or you truncate the results.
– wece
Commented Mar 12, 2013 at 13:01
• Question 1: What does [2,n−1] stand for? I don't understand where the 2 refers to and what 'n' is in your formula? Question 2: Which books on the math in image processing would you recommend? Question 3: Could you make those other matrices feel obvious for me as well? (Sharpen, Edge detect, Emboss) docs.gimp.org/en/plug-in-convmatrix.html Commented Mar 12, 2013 at 16:37
• 1)Sorry, I assumed a matrix of size $n\times n$. $[2,n-1]$ are the integer between $2$ and $n-1$. That is I kept only the pixel where the convolution is defined (without the image's border). 2) I'm not expert in the domain, and the website linked seem good enough to me. 3) Blur make an average of the neighbour pixels hence the images is blurred. Sharpen do the reverse of Blur, it tries to sharpen the differences of all pixels to remove their dependencies, hence in increase the contrast. Edge detect works as edge enhance, not only up-down edges for the edges but also right-left edges.
– wece
Commented Mar 12, 2013 at 17:16