Generating object mask during rendering for data collection I am creating a synthetic hand dataset for machine learning. I am rendering images of characters with different hand gestures in Blender. At the same time, I want to generate a hand mask for each rendered image to indicate the position of the hand in the RGB image. For example, the hand mask would be an image in which every pixel that is part of the hand at corresponding position in the rendered RGB image has value 1, all other pixels have value 0. 

How do I generate the hand mask? My intuition tells me that I need to select all vertices belonging to the hand, then calculate the projection of these pixels onto the image plane. However, I am new to Blender so do not know how to do this or if this is possible.
If you are looking to create a black and white image of a portion of a mesh, make the portion you wish to capture a distinct material (top image). 

Assign the material a material pass (see yellow arrow), and collect and output that pass in your render (See render stack):



Here's a step-by-step, if this is your desired output: How to put 2 parts of 1 object on different layers?

Hand by SuperDasil on BlendSwap: https://www.blendswap.com/blends/view/81285