Let's assume that a user's face, or some other object of interest, occupies most of the depth camera's field of view. However, the image also contains some other content that is not of interest. By analyzing the disparity map, we can tell that some pixels within the rectangle are outliers—too near or too far to really be a part of the face or another object of interest. We can make a mask to exclude these outliers. However, we should only apply this test where the data is valid, as indicated by the valid depth mask.
Let's write a function to generate a mask whose values are 0 for the rejected regions of the image and 255 for the accepted regions. This function should take a disparity map, valid depth mask, and optionally a rectangle as arguments. If a rectangle is specified, we will make a mask that is just the size...