---
title: grabcuts
description: "#### Overview Image segmentation using GrabCut algorithm. #### Expected Outputs ![Picture3 | LIPS® Developer Documentation](https://dev.lips-hci.com/wp-content/uploads/2026/04/Picture3-1024x525.png) #### Prerequisite - [OpenCV Viewer](https://dev.lips-hci.com/lipsedge-sdk-c/opencv-viewer/) #### Tutorial C++ ``` const int MAX_FOV_DEPTH_MM = 700; const int DISPLAY_MIN_Z = 300; const int DISPLAY_MAX_Z = 3000; ```…"
url: "https://dev.lips-hci.com/lipsedge-sdk-c/grabcuts/"
updated: "2026-10-16"
category: "LIPSedge™ SDK 1.x / LIPSedge™ SDK Languages & Libraries / C++"
---

# grabcuts

#### Overview

Image segmentation using GrabCut algorithm.

#### Expected Outputs

![Picture3 | LIPS® Developer Documentation](https://dev.lips-hci.com/wp-content/uploads/2026/04/Picture3-1024x525.png)

#### Prerequisite

- [OpenCV Viewer](https://dev.lips-hci.com/lipsedge-sdk-c/opencv-viewer/)

#### Tutorial

 C++

```
const int MAX_FOV_DEPTH_MM = 700;
const int DISPLAY_MIN_Z = 300;
const int DISPLAY_MAX_Z = 3000;
```

- `MAX_FOV_DEPTH_MM` defines the maximum distance (70 cm) of the points we want to keep in the foreground.
- `DISPLAY_MIN_Z` and `DISPLAY_MAX_Z` are 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 -> COLORn";
        }
```

- 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_Z` and `DISPLAY_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.
