---
title: "remove-background"
description: "## [https://dev.lips-hci.com/lipsedge-tm-sdk-languages-and-libraries/overview/remove-background#overview](https://dev.lips-hci.com/lipsedge-tm-sdk-languages-and-libraries/overview/remove-background#overview)Overview This example shows you how to remove the background of an image using depth camera with range filter. ## [https://dev.lips-hci.com/lipsedge-tm-sdk-languages-and-libraries/overview/remove-background#expect-output](https://dev.lips-hci.com/lipsedge-tm-sdk-languages-and-libraries/overview/remove-background#expect-output)Expect Output ![rbg1 | LIPS® Developer Documentation](https://dev.lips-hci.com/wp-content/uploads/2025/02/rbg1.avif) ## [https://dev.lips-hci.com/lipsedge-tm-sdk-languages-and-libraries/overview/remove-background#prerequisite](https://dev.lips-hci.com/lipsedge-tm-sdk-languages-and-libraries/overview/remove-background#prerequisite)Prerequisite - [OpenCV Viewer](/lipsedge-sdk-c/opencv-viewer/) - [Align Depth & Color](/lipsedge-sdk-c/align-depth-color/) - [Range…"
url: "https://dev.lips-hci.com/lipsedge-sdk-c/remove-background/"
updated: "2025-06-26"
category: "LIPSedge™ SDK 1.x / LIPSedge™ SDK Languages & Libraries / C++"
tags:
  - LIPSedge SDK
---

# remove-background

## [https://dev.lips-hci.com/lipsedge-tm-sdk-languages-and-libraries/overview/remove-background#overview](https://dev.lips-hci.com/lipsedge-tm-sdk-languages-and-libraries/overview/remove-background#overview)Overview

This example shows you how to remove the background of an image using depth camera with range filter.

## [https://dev.lips-hci.com/lipsedge-tm-sdk-languages-and-libraries/overview/remove-background#expect-output](https://dev.lips-hci.com/lipsedge-tm-sdk-languages-and-libraries/overview/remove-background#expect-output)Expect Output

![rbg1 | LIPS® Developer Documentation](https://dev.lips-hci.com/wp-content/uploads/2025/02/rbg1.avif)

## [https://dev.lips-hci.com/lipsedge-tm-sdk-languages-and-libraries/overview/remove-background#prerequisite](https://dev.lips-hci.com/lipsedge-tm-sdk-languages-and-libraries/overview/remove-background#prerequisite)Prerequisite

- [OpenCV Viewer](/lipsedge-sdk-c/opencv-viewer/)
- [Align Depth & Color](/lipsedge-sdk-c/align-depth-color/)
- [Range Filter](/lipsedge-sdk-c/range-filter/)

## [https://dev.lips-hci.com/lipsedge-tm-sdk-languages-and-libraries/overview/remove-background#tutorial](https://dev.lips-hci.com/lipsedge-tm-sdk-languages-and-libraries/overview/remove-background#tutorial)Tutorial

Modify from [range-filter](/lipsedge-sdk-c/range-filter/), we add two variables to determinate the desired distance. Then add two keyboard shortcut to set these values.

```
double thresholdValueUp = 530;
double thresholdValueDown = 385;
char input;

input = cv::waitKey(1);
if (input == 'p')
{
    if (thresholdValueUp + 5 <= 2000)
        thresholdValueUp += 5;
    else
        thresholdValueUp = 2000;

    std::cout << "Threshold Value (Up):" << thresholdValueUp << " (Down):" << thresholdValueDown << std::endl;
}
else if (input == 'o')
{
    if (thresholdValueUp - 5 >= 0 && thresholdValueUp - 5 >= thresholdValueDown)
        thresholdValueUp -= 5;

    std::cout << "Threshold Value (Up):" << thresholdValueUp << " (Down):" << thresholdValueDown << std::endl;
}
else if (input == 'x')
{
    if (thresholdValueDown + 5 <= 2000 && thresholdValueDown + 5 <= thresholdValueUp)
        thresholdValueDown += 5;

    std::cout << "Threshold Value (Up):" << thresholdValueUp << " (Down):" << thresholdValueDown << std::endl;
}
else if (input == 'z')
{
    if (thresholdValueDown - 5 >= 0)
        thresholdValueDown -= 5;
    else
        thresholdValueDown = 0;

    std::cout << "Threshold Value (Up):" << thresholdValueUp << " (Down):" << thresholdValueDown << std::endl;
}
```

Using these two values to create range filter mask image like we did before.

```
cv::threshold(thres, thres, thresholdValueDown, 1024, cv::THRESH_TOZERO);
cv::threshold(thres, thres, thresholdValueUp, 1024, cv::THRESH_TOZERO_INV);
cv::threshold(thres, thres, 1, 1024, cv::THRESH_BINARY);
```

Then we use OpenCV `bitwise_and` operation on the color image and the mask. We also need to convert the mask to `CV_8UC1` because that the format needed by `bitwise_and`. Finally we will get the result image that remove the background.Copy

```
thres.convertTo(thres, CV_8UC1, 255.0 / 1024.0);
cv::Mat res;
cv::bitwise_and(colorMat, colorMat, res, thres);
```

## [https://dev.lips-hci.com/lipsedge-tm-sdk-languages-and-libraries/overview/remove-background#full-code](https://dev.lips-hci.com/lipsedge-tm-sdk-languages-and-libraries/overview/remove-background#full-code)Full code

[remove-background.cpp](https://github.com/lips-hci/dev-doc-code-samples/blob/master/c%2B%2B/opencv_viewer/remove-background.cpp)
