Morphology

Morphology

Erode, Dilate and Median: kernels that don't multiply

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

A convolution matrix multiplies the pixels under the kernel and adds them up. Morphology uses the same sliding window, but instead of a weighted sum it takes the maximum, the minimum or the median. It sounds like a small change, and it's the maths behind every Erode, every edge matte and every despeckle you do on a matte.



Useful Links



Index



01. Max and min instead of sum

Put a kernel on a pixel, look at all the pixels under it, and keep:

- the maximum: that's a dilate. A white pixel anywhere under the kernel wins, so white areas grow and small black holes disappear.
- the minimum: that's an erode. A black pixel anywhere under the kernel wins, so white areas shrink and small white specks disappear.
- the value in the middle of the sorted list: that's a median. Isolated pixels, too few to be in the middle, simply vanish.

A matte with white specks and black holes
A dirty matte
The matte dilated: white grows, holes close
Dilate (max), radius 4
The matte eroded: white shrinks, specks vanish
Erode (min), radius 4

Unlike a convolution, these operations are not linear: there is no Fourier trick, no way to undo them, and dilate then erode does not give you back the original. That's exactly what makes them useful.



02. The shape of the kernel

In morphology the kernel is called structuring element, and its shape ends up printed on the corners of your matte. Here is a small square dilated by a big kernel of three shapes:

Dilate with a square kernel keeps sharp corners
Square: sharp corners
Dilate with a disc kernel rounds the corners
Disc: round corners
Dilate with a diamond kernel cuts the corners at 45 degrees
Diamond: 45° corners

A square is the fastest (it can be done in two passes, rows then columns) but grows mattes in a boxy way. A disc keeps the look of the original shape. That's the difference you see between the Erode nodes of Nuke.



03. In Nuke: three Erodes and a Median

Nuke has three nodes called Erode, and careful: they don't agree on the sign of size, and two of them start with channels: none (if your Erode does nothing, that's why).

menu nameclasssize > 0channelsnotes
Erode (fast)Dilategrows whiteallinteger, square, the fastest
Erode (filter)FilterErodeshrinks whitenonesub-pixel, filter box to gaussian
Erode (blur)Erodeshrinks whitenonesoft edges, extra blur and quality
MedianMedianbigger windowallremoves single pixel noise

In short: Erode (fast) for quick matte growing on integer pixels, Erode (filter) when you need fractional sizes that animate smoothly, Erode (blur) when you want the edge soft at the same time. Erode (blur) treats every grey value as part of an edge, so solid areas that aren't 0 or 1 change value: use it on mattes, not on images.



04. Open and close

Two steps, same size, opposite directions. The magic is that the big shapes come back to their size, while the small details don't.

- Open = erode, then dilate: the specks are destroyed by the erode and there's nothing left to grow back. The shapes return to their size.
- Close = dilate, then erode: the holes are filled by the dilate and never reopen. Again the shapes return to their size.

Open: the white specks are gone
Open: no more specks
Close: the black holes are filled
Close: no more holes
Open then close: a clean matte
Open + close: clean

This is the classic clean-up of a key: two Erode (fast) nodes with size −2 and +2 remove the noise in the background, two with +2 and −2 fill the holes in the foreground. Too big and you start rounding the corners of the subject: keep the size just bigger than the dirt.



05. Edge mattes

Subtract an erode from a dilate and only the border survives. Three variations, all useful:

- dilate − erode: a band centred on the edge (the morphological gradient).
- matte − erode: the inner edge, for edge blur, light wrap or a little despill on hair.
- dilate − matte: the outer edge, for edge extend or a soft halo.

The clean matte
Clean matte
Edge matte: dilate minus erode
Dilate − erode: the edge


06. Median

The median sorts the pixels under the kernel and keeps the one in the middle. A single bright pixel is never in the middle of the list, so it disappears, but a real edge is (half of the pixels are on each side), so it stays sharp. That's the big difference with a blur, which spreads the noise around and softens the edges.

The dirty matte
Dirty matte
Median 5x5: specks and holes gone, edges still sharp
Median 5x5: one node, both problems

Use it for salt-and-pepper noise in keys, dead or hot pixels, fireflies in CG renders (on a copy, then keyed back only where needed) and to calm chroma noise. Big medians make images look painted: keep the size small.



Morphology Lab

Pick an operation, a shape and a size. Drag on the image to compare with the original.

Original Dilate
Operation
Kernel shape

The nodes use Erode (fast), which works on a square: in the Lab choose Square to match it exactly.



Nodes to paste in Nuke

Click a box, copy it (Ctrl+C / Cmd+C) and paste it in Nuke's Node Graph (Ctrl+V / Cmd+V). They work on the alpha.

Clean key (open + close). Four Erode (fast) nodes: the first two remove the specks outside, the last two fill the holes inside. Change all the sizes together.

Edge matte. Dilate and Erode (filter) on the alpha, and a Merge in difference: the band around the edge, ready for edge blur or light wrap.

Despeckle. A Median 3x3 on the alpha only.

Grow and soften. Erode (blur) with negative size to grow the matte, with a soft edge in one node.



The matrix series

If you are in love with Matrices :) have a look at the other pages:
Convolution Matrix Transform Matrix ColorMatrix Convolve & Deconvolution 3D Matrices CornerPin & Homography BlinkScript Matrices

Leave a comment