This is a very short tutorial on how to use the Dilate Erode node in Blender’s Compositor to eliminate, or at least reduce, imperfections in Color Keying mattes, especially those found inside the areas we want to keep.
Video Transcript
Hello everyone! This is a very short tutorial on how to use the Dilate Erode node in Blender’s Compositor to eliminate, or at least reduce, imperfections in Color Keying mattes, especially those found inside the areas we want to keep. I talked about the Color Key node in my 10 part mini series on turntable photogrammetry, which you can find among the other videos on my channel.

When removing the background color from images using nodes from the Keying group, such as Color Key, it can happen that the node makes some areas inside the preserved regions transparent or semi-transparent. This is especially noticeable when viewing the mask output, known as the Matte.

There are many ways to fix these results, so the one I’ll show you here is just one of several techniques. Its advantage is that it’s easy to implement, though as we’ll see, it’s not suitable for all possible matte shapes.
If we connect a Dilate Erode node with a positive Distance value to the Matte output of a Keying node, the resulting mask will be expanded, in the sense that the white areas will grow, covering any black spots within them.

If we then connect another Dilate Erode node with the same Distance value but negative, the white areas will shrink. However, the parts previously expanded will not be restored. This brings the shape back to its original outline while keeping the internal areas white.

This behavior is similar to using the Select More and Select Less operators in Edit Mode: you can expand a selection with Select More to include an internal part, and then use Select Less multiple times to shrink the perimeter back to its original shape while keeping the included parts selected.
As I mentioned earlier, not all mattes are well suited for this kind of processing. In particular, there are usually no issues with convex areas that have smooth borders without too many irregularities or details. That’s because the Dilate Erode sequence will expand and then shrink those masks without losing important features.
On the other hand, in areas with concavities, either because of specific details or the overall shape of the object, the expansion caused by Dilate might significantly alter the silhouette of the matte, erasing the concavities. As a result, the following Erode will shrink the new mask but won’t be able to recover the lost details. So, be careful not to set the Distance values too high if you're dealing with shapes like these.
The Matte can also be refined in many other ways. For example, you can use Box Mask nodes in Add mode to make other black spots inside the shape turn white. Since it’s not advisable to over-expand the Dilate Erode mask, for the reasons we’ve discussed, this can be a good way to keep the Distance values low on both Dilate Erode nodes while still removing the most obvious imperfections.

Box Mask nodes in Subtract mode, on the other hand, can be used to remove white areas from the mask. I often use them to exclude regions around the object of interest. In this case, they’re not necessary, but multiple Subtract masks can hide a lot of tiny specks or entire background areas that weren’t removed by the Color Key node. The position and size values of the masks can be animated, so you can move and resize them as needed throughout the image sequence.

Anyway, to get the final color image with the modified mask, just use a Set Alpha node, connecting the original image to the Image input and the Matte output of the Keying node to the Alpha input.

Alright, that’s it for this short tutorial! Unlike most of my other video tutorials, a PDF version of this one is currently not available for download. However, you’ll find a link in the description to the page containing the PDF versions of many of my other tutorials. Either way, this is a great time to subscribe to the channel and enable notifications! See you soon!