# Range from image

**URL:** <https://forum.opencv.org/t/range-from-image/6108>\
**Category:** Uncategorized\
**Created:** [November 8, 2021, 7:32pm UTC](https://forum.opencv.org/t/range-from-image/6108 "2021-11-08T19:32:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bamboo324](https://avatars.discourse-cdn.com/v4/letter/b/a6a055/32.png) [@Bamboo324](https://forum.opencv.org/u/Bamboo324)\
**Post date:** [November 8, 2021, 7:32pm UTC](https://forum.opencv.org/t/range-from-image/6108/1 "2021-11-08T19:32:12Z")

</div>

I am trying to remove a specific range of colors, but whatever i try, i get a black mask…

cv::inRange(img, low, high, mask);

Even if i swap the colors, still all black is the output, which doens’t make any sence cause i took the colors from the same image.

Another problem is how to extract a range of colors from the image, i want to select a specific part of image, get the range of colors and get ride of them, now i did it manually, but it can’t be like that.

---

<div class="post-metadata">

**Author:** ![kbarni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/kbarni/32/26_2.png) [@kbarni](https://forum.opencv.org/u/kbarni)\
**Post date:** [November 8, 2021, 8:19pm UTC](https://forum.opencv.org/t/range-from-image/6108/2 "2021-11-08T20:19:45Z")

</div>

inRange returns a binary image (values of 0 or 1). If you display it, you will see black (as images are considered 0…255, so the value of 1 is very close to black).  
To check the result, do:

```
cv::imShow("Mask",mask*255);
```

---

<div class="post-metadata">

**Author:** ![Bamboo324](https://avatars.discourse-cdn.com/v4/letter/b/a6a055/32.png) [@Bamboo324](https://forum.opencv.org/u/Bamboo324)\
**Post date:** [November 8, 2021, 9:03pm UTC](https://forum.opencv.org/t/range-from-image/6108/3 "2021-11-08T21:03:57Z")

</div>

Nothing changed, still all black. Maybe you know the answer for the 2 question, how do i get the range, from a specific part of image i crop?

---

<div class="post-metadata">

**Author:** ![kbarni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/kbarni/32/26_2.png) [@kbarni](https://forum.opencv.org/u/kbarni)\
**Post date:** [November 9, 2021, 10:58am UTC](https://forum.opencv.org/t/range-from-image/6108/4 "2021-11-09T10:58:48Z")

</div>

Easiest solution is to take samples using an image editor like GIMP.

Otherwise you can do a segmentation program yourself. Untested code, so might need some modifications:

```auto
Mat image = imread("myimage.jpg");
namedWindow("Control", CV_WINDOW_NORMAL);
imshow("Control",image);

//inRange thresholds
int iLowR = 0,iHighR = 255;
int iLowG = 0,iHighG = 255;
int iLowB = 0,iHighB = 255;

//Create trackbars in "Control" window**
createTrackbar("LowR", "Control", &iLowR, 179);
createTrackbar("HighR", "Control", &iHighR, 179);
createTrackbar("LowG", "Control", &iLowG, 255);
createTrackbar("HighG", "Control", &iHighG, 255);
createTrackbar("LowB", "Control", &iLowB, 255);
createTrackbar("HighB", "Control", &iHighB, 255);
int ch=0;
while(ch!=27){ //break with ESC
    Mat segmented,result;
    inRange(image, Scalar(iLowB, iLowG, iLowR), Scalar(iHighB, iHighG, iHighR), segmented);
    image.copyTo(result,segmnted);
    imshow("Control",segmented);
    ch=waitKey(30);
}

```
