Overview #
Image segmentation using GrabCut algorithm.
Expected Outputs #

Prerequisite #
Tutorial #
C++
const int MAX_FOV_DEPTH_MM = 700;
const int DISPLAY_MIN_Z = 300;
const int DISPLAY_MAX_Z = 3000;MAX_FOV_DEPTH_MMdefines the maximum distance (70 cm) of the points we want to keep in the foreground.DISPLAY_MIN_ZandDISPLAY_MAX_Zare the depth range used to normalize and colorize the depth map.- These constants keep the depth filtering behavior consistent across the program.
C++
cv::Mat colorizeDepthMap(const cv::Mat& depth, const int display_min_z = DISPLAY_MIN_Z, const int display_max_z = DISPLAY_MAX_Z) {
const double normalize_alpha = 255.0 / static_cast<double>(display_max_z - display_min_z);
const double normalize_beta = static_cast<double>(-255.0 * display_min_z) /
static_cast<double>(display_max_z - display_min_z);
cv::Mat output, depth_normal;
depth.convertTo(depth_normal, CV_8UC1, normalize_alpha, normalize_beta);
cv::applyColorMap(depth_normal, output, cv::COLORMAP_JET);
cv::Mat displayMask = (depth >= display_min_z) & (depth <= display_max_z);
cv::Mat fovMask = (depth <= MAX_FOV_DEPTH_MM);
cv::Mat finalMask = displayMask & fovMask;
output.setTo(Scalar(0, 0, 0), ~finalMask);
return output;
}- Converts a 16-bit depth image into a colorized visualization using cv::applyColorMap.
- Normalizes depth values into 0–255 based on display_min_z and display_max_z.
- Creates a mask that keeps only pixels:
- Within the display range
- Within the FOV limit (MAX_FOV_DEPTH_MM, i.e. 70 cm).
- Pixels outside this combined mask are set to black – effectively “removing” background points beyond the desired depth.
C++
bool regEnabled = false;
if (device.isImageRegistrationModeSupported(IMAGE_REGISTRATION_DEPTH_TO_COLOR))
{
if (device.setImageRegistrationMode(IMAGE_REGISTRATION_DEPTH_TO_COLOR) == STATUS_OK)
{
regEnabled = true;
cout << "[INFO] Registration enabled: DEPTH -> COLOR\n";
}- Registration aligns depth pixels to the color camera viewpoint, which is critical when depth is used to mask RGB images.
C++
cv::Mat rgbDisplayMask = (depthRaw >= DISPLAY_MIN_Z) & (depthRaw <= DISPLAY_MAX_Z);
cv::Mat rgbFovMask = (depthRaw <= MAX_FOV_DEPTH_MM);
cv::Mat rgbFinalMask = rgbDisplayMask & rgbFovMask;- Builds a display range mask that keeps only depth values between
DISPLAY_MIN_ZandDISPLAY_MAX_Z. - Builds a FOV mask that keeps only points closer than or equal to
MAX_FOV_DEPTH_MM(70 cm). - Combines both masks to get the final foreground region.
- Applies the inverse of this mask to the RGB image to set background pixels to black.
- Effectively simulates a depth-based foreground extraction, similar in concept to GrabCut but using depth instead of interactive user input.
