Convolution Matrix

Convolution Matrix

Matrix 3x3 in Nuke

I created this tutorial and this gizmo because I could never remember the various matrices. It collects the most popular convolution kernels for image processing, explained for compositors and ready to paste into Nuke.

You can download the Gizmo for Nuke here:
Download from Nukepedia

I have also created a tool that uses a matrix to find the edges of an object, useful for rim lights:
Quick 2D Rim Light

Matrices in Nuke, the series: Convolution Matrix · Transform Matrix · ColorMatrix · Convolve & Deconvolution · 3D Matrices · Morphology · CornerPin & Homography · BlinkScript Matrices



Useful Links



Index



Overview: how a kernel works

A kernel (or convolution matrix, or mask) is a small grid of numbers. To filter an image, you put the kernel on top of a pixel, multiply every number of the kernel by the pixel under it, add everything together and write the result in the new pixel. Then you move to the next pixel and do it again. That's a convolution. Change the numbers and the same operation blurs, sharpens, finds edges or embosses.

Kernels can be 3x3, 5x5, 7x7... Here we use 3x3: it's the most common size and it's enough for most effects. The formula for the output pixel O at position (y, x), with the image I and the kernel F, is:

Convolution formula: the sum of image pixels times kernel weights

If the weights don't sum to 1 the image gets brighter or darker, so we divide by the divisor D, the sum of all the weights (or 1 when the sum is 0, as in edge kernels). Some tools also add an offset at the end, useful when the result can be negative. The complete formula:

Convolution formula with divisor and offset

I = the image, F = the filter (kernel), D = the divisor, L = the size of the kernel (3 for a 3x3).

One detail that confuses everybody: a real convolution flips the kernel horizontally and vertically before using it. So the top-left value of the kernel multiplies the bottom-right pixel:

The kernel is flipped horizontally and vertically before the convolution
The kernel is flipped…
The flipped kernel over the image pixels
…then multiplied and summed

OUTPUT = i·1 + h·2 + g·3 + f·4 + e·5 + d·6 + c·7 + b·8 + a·9

Most kernels are symmetric (blur, sharpen, Laplacian), so the flip changes nothing. It matters only for directional kernels like emboss and Sobel: a flipped Sobel finds the same edges with the opposite sign.



A pixel, step by step

Here is an edge-detection kernel applied to one pixel (in red). The blue square is the area the kernel reads. You can find a great interactive version of this at songho.ca.

A 3x3 kernel applied to one pixel of an image matrix

Multiply and add: (47×0) + (22×1) + (25×0) + (52×1) + (51×4) + (50×1) + (35×0) + (47×1) + (49×0) = 375

Divisor: 0 + 1 + 0 + 1 + 4 + 1 + 0 + 1 + 0 = 8

Offset: 0

Result: 375 ÷ 8 = 46.875 ≈ 47

The filter always reads the original image, never the pixels it has already changed.



In Nuke

Create a Matrix node from Filter → Matrix... and choose the size, 3x3 for this page. The key knob is normalize: when it's off, the pixel of the example above becomes 375; when it's on, Nuke divides by the sum of the weights and you get 46.875. Turn it on for blurs, off for kernels that sum to 0 (edges, Sobel), where there's nothing to divide by.

The normalize checkbox of the Matrix node in Nuke

Two ways to use the kernels of this page:

- the row of numbers: copy it, then in the Matrix node right-click the matrix knob and choose Paste → Paste Absolute.
- the dark box: click it, copy it and paste it directly in the Node Graph. You get a Matrix node already set, with normalize on or off as it should be.

010
141
010

normalize: on

Paste Absolute in the Matrix node of Nuke

Remember that Nuke works in linear float: edge and Sobel kernels give negative values (black in the Viewer) and nothing is clamped. Add a Clamp, or look at the absolute value with an Expression, when you need a clean edge matte.



Kernel Lab

Type any 3x3 kernel or pick one of the presets: the image updates live, in linear light like Nuke. Drag on the image to move the wipe, then copy the node for Nuke.

Original Matrix


Matrices

  • Original image
Original image: the Mole Antonelliana in Turin

Blur

  • 01. Identity
Convolution matrix identity: the image does not change

A single 1 in the centre: every pixel takes only itself. Nothing changes. It's the neutral kernel, the starting point to build all the others, like f(x) = x.

000
010
000

normalize: on

  • 02. Box blur
Box blur 3x3 convolution kernel

All weights equal: every pixel becomes the average of its 3x3 neighbourhood. With normalize on, the nine 1s are divided by 9. It's fast and simple, but its square shape can leave a slightly blocky look on strong details.

111
111
111

normalize: on

  • 03. Light blur (cross)
Light blur with a cross shaped kernel

The centre counts 4, the four direct neighbours 1, the corners 0. Half of the result comes from the pixel itself, so it's a very gentle softening. It's a rough approximation of a Gaussian, cheaper and lighter than example 04.

010
141
010

normalize: on

  • 04. Gaussian blur 3x3
Gaussian blur 3x3 convolution kernel

The classic 3x3 Gaussian: 4 in the centre, 2 on the sides, 1 on the corners, divided by 16. The weights fall off smoothly from the centre, so the blur is round and soft, without the blocky look of the box blur. It's also separable: it's the same as a [1 2 1] blur horizontally followed by one vertically, which is why Gaussian blurs are fast.

121
242
121

normalize: on

  • 05. Diagonal motion blur
Diagonal motion blur convolution kernel

The weights lie on a line, so every pixel is smeared along that line: a motion blur. On the diagonal it looks like an identity matrix, but as a kernel it's a 45° streak. Put the ones in the middle row for a horizontal blur, in the middle column for a vertical one. On a 3x3 it's subtle; bigger kernels give longer streaks.

100
010
001

normalize: on

  • 06. Mean filter
Mean filter 3x3 convolution kernel

The same as the box blur, already divided: nine times 1/9 = 0.111111. It gives the same result with or without normalize, because the weights already sum to 1. Used to reduce noise; running it twice is close to a bigger, softer blur.

0.1111110.1111110.111111
0.1111110.1111110.111111
0.1111110.1111110.111111

normalize: off

  • 07. Antialiasing filter (1)
Antialiasing filter convolution kernel

A cross with the centre at 2: a very small blur that softens jagged, aliased edges (stair-stepping on thin lines and CG renders) without losing much detail. There's a gizmo on Nukepedia based on these kernels (link at the top).

010
121
010

normalize: on

  • 08. Antialiasing filter (2)
Antialiasing filter 2 convolution kernel

A slightly stronger antialiasing. If it looks familiar, it is: it's exactly the Gaussian 3x3 of example 04. Many "different" filters are the same kernel with a different name.

121
242
121

normalize: on

Sharpen

  • 09. Sharpen
Sharpen convolution kernel

The centre is boosted (5) and the four neighbours are subtracted (-1). The weights sum to 1, so flat areas don't change, but wherever a pixel differs from its neighbours the difference gets bigger: edges look crisper. It's the original image plus an edge detection (example 11 with the sign flipped). Sharpening can't rescue a really blurry image: for that see the deconvolution.

0-10
-15-1
0-10

normalize: on

  • 10. Strong sharpen
Strong sharpen convolution kernel

The same idea with all eight neighbours subtracted and the centre at 9. Much stronger, it also amplifies grain and compression artefacts. Use the mix knob to dose it.

-1-1-1
-19-1
-1-1-1

normalize: on

Edges and emboss

  • 11. Edge detect (Laplacian)
Laplacian edge detection convolution kernel

The four neighbours minus four times the centre. The weights sum to 0: flat areas become black and only the changes survive, in every direction. It's the Laplacian, the "second derivative" of the image. Turn normalize off.

010
1-41
010

normalize: off

  • 12. Horizontal difference
Horizontal difference convolution kernel

Each pixel minus its neighbour on the side: the simplest possible derivative. It shows the vertical edges (where the image changes from left to right) and ignores the horizontal ones. Often called "edge enhance", it's the basic block of Sobel and Roberts below.

000
-110
000

normalize: off

  • 13. Emboss
Emboss convolution kernel

Negative weights on one side, positive on the other, along the diagonal: edges facing one way get lighter, the opposite ones darker, as if the image was pressed in metal and lit from a corner. The weights sum to 1, so the image keeps its colours. Rotate the numbers to move the "light".

-2-10
-111
012

normalize: on

  • 14. Outline
Outline convolution kernel

The centre times 8 minus all eight neighbours. Like the Laplacian, but it also looks at the diagonals, so it gives thicker, stronger outlines. Flat areas go to black, normalize off.

-1-1-1
-18-1
-1-1-1

normalize: off

Sobel

The Sobel kernels measure how fast the image changes in one direction, giving more weight to the middle row or column. Each one finds edges in one direction; combine two of them (horizontal and vertical) to get all the edges. Normalize off for all four.

  • 15. Top Sobel
Top Sobel convolution kernel

Top row against bottom row: it finds the horizontal edges (where the image changes from top to bottom).

121
000
-1-2-1

normalize: off

  • 16. Bottom Sobel

The same kernel upside down (all signs flipped): the same horizontal edges, with the opposite sign. Edges that were negative (black) in the top Sobel become positive here.

-1-2-1
000
121

normalize: off

  • 17. Right Sobel
Right Sobel convolution kernel

Left column against right column: it finds the vertical edges (where the image changes from left to right).

10-1
20-2
10-1

normalize: off

  • 18. Left Sobel

The mirror of the right Sobel: the same vertical edges with the opposite sign.

-101
-202
-101

normalize: off

All the edges at once. Run a horizontal and a vertical Sobel on the same image and combine them with an Expression node: sqrt(Gx² + Gy²) gives the strength of the edge in any direction. That's how a lot of edge-detection and rim-light tools work.

Roberts cross

  • 19. Roberts cross

One of the oldest edge detectors: tiny 2x2 kernels that compare each pixel with its diagonal neighbour. Very fast and very sensitive to noise. Each kernel is the previous one rotated by 90°. In Nuke's 3x3 Matrix node, put the 2x2 in the bottom-right corner and leave zeros elsewhere (normalize off):

Roberts cross kernel 1
0 1 / -1 0
Roberts cross kernel 2
0 -1 / 1 0
Roberts cross kernel 3
-1 0 / 0 1
Roberts cross kernel 4
1 0 / 0 -1
000
001
0-10

normalize: off

000
00-1
010

normalize: off

000
0-10
001

normalize: off

000
010
00-1

normalize: off



Bigger kernels

A 3x3 kernel only sees one pixel around. For bigger effects you can choose 5x5 or 7x7 in the Matrix node, or go all the way and use a whole image as the kernel with the Convolve node: that's how you get a real lens bokeh. And if you're wondering whether a blur can be undone… yes, sometimes. It's all in the next page of the series:

Convolve & Deconvolution Transform Matrix ColorMatrix 3D Matrices

Questions about this tutorial?

Write me directly: I read every message. You can also find me on LinkedIn.