Demosaicing Produces Grayscale Result

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.

#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);

  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).

VideoCapture always returns BGR data.

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

your assumptions may be wrong.

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.

1 Like

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?

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))


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

and with a little gamma:

1 Like

I’m, frankly, astounded. Thank you!