Masking in Blender compositor

Posted in Action / Study / Video editing on Wednesday, January 4, 2017 by Joel Höglund

As a followup to my previous Blender tutorial (found here), here is some additional information on how to combine two, or potentially more, masks into one. I'll go through how I added in Bernie's face onto the fallen gladiator, as seen in the video below. As you can see, the head of the axe covers up his face for part of the scene, but also the helmet itself. Since they move independent of each other, we're really going to have to use two separate masks. Here I only use a still image, but masking works exactly the same with video.

While you could use Blender masks even for still images, it is easier to get a good result using a photo editor, such as Photoshop or GIMP. In GIMP I prefer to use the Foreground Select Tool. Make a rough outline of the object you want to extract and finish the selection by clicking the starting point. You can then start "drawing" on the colors that you want selected (mark foreground) and the tool will automatically select all other pixels of the same color inside your outline. If you make a mistake you can switch to mark background instead, or simply hold down Ctrl and draw on the background layer. But it's not that big of a deal if you get some of the background image selected also. It's quite easy to just erase it with the eraser afterwards. When finished, hit Enter and then Ctrl+C to copy and Ctrl+V to paste the selection. In the layer menu, you'll see a new layer called Floating Selection. Right click on it and hit To New Layer. You can now delete or simply hide the layer beneath it. Do some touch up if needed and then save it as PNG, to keep the alpha channel (for transparency).

Right click and select View Image, to view the images in full resolution!

Adding the image in Blender is as simple as using the Image Input node, linked to an Alpha Over node to keep the transparency data.

Now, as we want this image to be masked by both the helmet and the axe blade this will prove slightly more complicated, though not that much. Although it probably would be possible to make one single mask that achieves what we want, it would force as to do a lot of manual labor and we want to avoid that as much as possible. Instead I created two separate masks, one for the face of the gladiator, where we want Bernie to show up instead, and one for the axe that covers his face partially. And remember that you can animate individual vertices to fit the motion of the video. You can also Parent your vertices to a tracking point by pressing Ctrl+P, which can be extremely helpful. Sometimes it is easier to do it by hand though.

Now these two masks are quite different in nature, the first one marks that which we want Bernie's face to cover, while the second one marks that which should cover his face. This is no problem though, as long as we understand how these masks work. Luckily Blender allows you to see how the masks function graphically! By holding Ctrl+Shift and left-clicking on any node, you can link a Viewer node to it. This will show you the image information of that single node. Below are the two masks with the viewer node connected to them.

Face maskAxe mask. Don't put any energy into things that won't be seen anyway. Keep it simple!

As you can see, the selected area in each mask is represented as white pixels, and you can think of these pixels as having a value of 1.0, while the black pixels have a value of 0.0. So how do we mix these two masks together? Well in this case we want everything in the Face-mask to show up except that which is covered by the Axe-mask. So we simply use a Subtract node node, found under Converter>Math. Using this node, we will subtract the value of the second mask from the first one, for each individual pixel. White (1.0) - White (1.0) = Black (0.0). Then I put the combined mask through a blur filter to cover up some of the rough edges. It actually makes quite a difference. Lastly, connect the blurred mask to the Factor input of an Alpha Over node. This node will "paste" the top image connected to it, over the bottom image, except for where the mask has a value of 1.0 (White).

Node setup

For your interest, you can see the resulting mask below.

Result from subtraction node

And that is that! If you have any questions, feel free to ask them in the comments below.