# Demosaicing Produces Grayscale Result

**URL:** <https://forum.opencv.org/t/demosaicing-produces-grayscale-result/14137>\
**Category:** C++\
**Tags:** imgproc, videoio\
**Created:** [August 4, 2023, 8:18pm UTC](https://forum.opencv.org/t/demosaicing-produces-grayscale-result/14137 "2023-08-04T20:18:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kringg](https://avatars.discourse-cdn.com/v4/letter/k/a8b319/32.png) [@kringg](https://forum.opencv.org/u/kringg)\
**Post date:** [August 4, 2023, 8:18pm UTC](https://forum.opencv.org/t/demosaicing-produces-grayscale-result/14137/1 "2023-08-04T20:18:50Z")

</div>

I’m attempting to produce a demosaiced RGB image from a RAW8 UVC stream. The RAW8 data is stored on the G channel of the incoming VideoCapture. I’ve tried many different permutations, but the result of OpenCV’s demosaicing algorithm always generates a grayscale image.

```auto
#include <iostream>
#include <opencv2\opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
  namedWindow("Debayer Test");

  Mat image;
  Mat channels[3];
  VideoCapture cap(0);

  if (!cap.isOpened())
  {
    cout << "cannot open camera";
  }

  while (true)
  {
    cap >> image;
    split(image, channels);

    Mat output(image.size(), image.type());
    demosaicing(channels[1], output, COLOR_BayerBG2BGR);

    imshow("Demosaic Result", output);

    waitKey(25);
  }
  return 0;
}

```

Any suggestions? Even if I’ve selected the wrong Bayer pattern, I’d expect the result of ‘demosicing’ to be full-color (albeit incorrectly mapped).

---

<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:** [August 4, 2023, 9:31pm UTC](https://forum.opencv.org/t/demosaicing-produces-grayscale-result/14137/2 "2023-08-04T21:31:37Z")

</div>

VideoCapture always returns BGR data.

dump a whole frame as PNG, present it, so people can investigate.

your assumptions may be wrong.

---

<div class="post-metadata">

**Author:** ![kringg](https://avatars.discourse-cdn.com/v4/letter/k/a8b319/32.png) [@kringg](https://forum.opencv.org/u/kringg)\
**Post date:** [August 4, 2023, 9:49pm UTC](https://forum.opencv.org/t/demosaicing-produces-grayscale-result/14137/3 "2023-08-04T21:49:34Z")

</div>

You’ve made me second-guess whether my input frame is a valid Bayer-filtered image. Thank you, I’m checking with the guys who wrote the firmware.

 ![IMX-586](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/6/606add67aa3a056c09182f773c2556334c444602.png)

---

<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:** [August 5, 2023, 1:55pm UTC](https://forum.opencv.org/t/demosaicing-produces-grayscale-result/14137/5 "2023-08-05T13:55:55Z")

</div>

Dima\_Fantasy was an AI spam bot. do not trust anything the AI spam bot posted.

* * *

the values seem to be inverted. what did your firmware guys do, flip the bits?

```python
debayered = 1 - np.float32(1/255) * cv.cvtColor(green, cv.COLOR_BayerBG2BGR)

# this also works: debayered = np.float32(1/255) * cv.cvtColor(~green, cv.COLOR_BayerBG2BGR)
# ~ flips the bits

high = np.max(debayered, axis=(0,1))
low = np.min(debayered, axis=(0,1))

print(high)
print(low)

debayered = (debayered - low) / (high - low)

```

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/4/4bf86d83f60c2c0a4b5c3788057f71034918d730.jpeg)

and with a little gamma:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/d/d65de1540658faa3ca217f1dcaff11739bcc7146.jpeg)

---

<div class="post-metadata">

**Author:** ![kringg](https://avatars.discourse-cdn.com/v4/letter/k/a8b319/32.png) [@kringg](https://forum.opencv.org/u/kringg)\
**Post date:** [August 8, 2023, 1:54am UTC](https://forum.opencv.org/t/demosaicing-produces-grayscale-result/14137/6 "2023-08-08T01:54:34Z")

</div>

I’m, frankly, astounded. Thank you!
