# How do I convert HSV mask to BGR?

**URL:** <https://forum.opencv.org/t/how-do-i-convert-hsv-mask-to-bgr/4239>\
**Category:** C++\
**Tags:** imgproc\
**Created:** [July 8, 2021, 10:34am UTC](https://forum.opencv.org/t/how-do-i-convert-hsv-mask-to-bgr/4239 "2021-07-08T10:34:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Salih\_MSA](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/salih_msa/32/2608_2.png) [@Salih\_MSA](https://forum.opencv.org/u/Salih_MSA)\
**Post date:** [July 8, 2021, 10:34am UTC](https://forum.opencv.org/t/how-do-i-convert-hsv-mask-to-bgr/4239/1 "2021-07-08T10:34:04Z")

</div>

Hi,

For this colour detection program I’m writing, I essentially convert an image to HSV and mask it to detect yellow (so white where yellow is, black otherwise) & then simply see if a given pixel is white or not.

```auto
    const cv::Mat roiImage = // read in image ... ;

	cv::Mat tmpMask ;
	cv::cvtColor(roiImage, tmpMask, cv::COLOR_BGR2HSV) ;
	cv::inRange(tmpMask, cv::Scalar(20, 100, 100), cv::Scalar(30, 255, 255), tmpMask) ;

```

Problem is, to see if a given pixel is white in value, I try to convert it back to BGR, but opencv keeps throwing an error regarding the fact HSV has one channel, BGR has 3.

```auto
	cv::Mat yellowMask ; cv::cvtColor(tmpMask, yellowMask, cv::COLOR_HSV2BGR) ;
                                               // ^ error here

```

```auto
terminate called after throwing an instance of 'cv::Exception'
  what(): OpenCV(4.2.0) ../modules/imgproc/src/color.simd_helpers.hpp:92: error: (-2:Unspecified error) in function 'cv::impl::{anonymous}::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::impl::{anonymous}::Set<3>; VDcn = cv::impl::{anonymous}::Set<3, 4>; VDepth = cv::impl::{anonymous}::Set<0, 5>; cv::impl::{anonymous}::SizePolicy sizePolicy = cv::impl::<unnamed>::NONE; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&]'
> Invalid number of channels in input image:
> 'VScn::contains(scn)'
> where
> 'scn' is 1

```

The only work around I’ve found is to write the HSV mask externally (ie save it as an image) and then read it in, and presto that works, but is horribly inefficient.

How can I find a workaround for this issue? Thanks for your time

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [July 8, 2021, 10:42am UTC](https://forum.opencv.org/t/how-do-i-convert-hsv-mask-to-bgr/4239/2 "2021-07-08T10:42:19Z")

</div>

> [@Salih\_MSA](#):
>
> `cv::inRange`

if you apply this in-place, it will destroy your hsv image, and replace it with a single channel, binary mask, which explains:

> [@Salih\_MSA](#):
>
> ```auto
> > Invalid number of channels in input image:
> > 'VScn::contains(scn)'
> > where
> > 'scn' is 1
> 
> ```

> [@Salih\_MSA](#):
>
> I try to convert it back to BGR

you don’t need to convert anything,  
remember, you still got the original BGR image (roiImage) around ?

(it also does not make any sense to try to convert the mask, it’s binary, and does no more contain any colour information)

---

<div class="post-metadata">

**Author:** ![Salih\_MSA](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/salih_msa/32/2608_2.png) [@Salih\_MSA](https://forum.opencv.org/u/Salih_MSA)\
**Post date:** [July 8, 2021, 10:57am UTC](https://forum.opencv.org/t/how-do-i-convert-hsv-mask-to-bgr/4239/3 "2021-07-08T10:57:35Z")

</div>

Long story short, yellow is a horrible colour to detect. I use distancing to see if a pixel is closest to BGR orange, black, blue, white or grey. If the pixel is orange, I then check if the corresponding pixel in the mask was detected as yellow. If the pixel is white, I say it’s yellow, else its actually orange.  
So I can’t use the original BGR image in that if statement.  
How can I do what I described by either using the mask or doing something else ?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [July 8, 2021, 4:39pm UTC](https://forum.opencv.org/t/how-do-i-convert-hsv-mask-to-bgr/4239/4 "2021-07-08T16:39:14Z")

</div>

related:

> <https://stackoverflow.com/questions/68301184/how-do-i-convert-hsv-mask-to-bgr>
