2

I have some troubles with bayer-demosaicing (OpenCV C++). I need to convert the image from the Bayer mosaic to the normal version.

I tried to use the cv::cvtColor with cv::COLOR_BayerGB2BGR, but the output image looks so "green" , and I don't understand why. I also tryed to somehow use cv::demosaicing for bilinear interpolation, but that didn't help either. I know that the output image should be two times smaller than the input one, because we have to get 1 out of 4 pixels, but I don't understand how to implement it correctly. Upd:enter image description here

#include <opencv2/opencv.hpp>

int main()
{
    cv::Mat bayer_img = cv::imread("bayer_image2.png", cv::IMREAD_GRAYSCALE);
    cv::Mat rgb_img;

    cv::cvtColor(bayer_img, rgb_img, cv::COLOR_BayerGBRG2BGR);

    cv::Mat demosaiced_img;
    cv::cvtColor(rgb_img, demosaiced_img, cv::COLOR_BGR2RGB);

    cv::Mat resized_img;
    cv::resize(demosaiced_img, resized_img, cv::Size(), 0.5, 0.5);
    
    cv::imwrite("bayer_image_output.png", resized_img);
}
Mikhail K
  • 21
  • 2
  • 2
    The output image should be the same resolution as the Bayer input, but with 3 color channels. Please add `bayer_image2.png` to your question. Make sure SO site keeps the image in PNG format (large images are automatically converted to JPEG). In case the image is too large, crop a relevant area or share the image using Google Drive (for example). – Rotem Jun 04 '23 at 20:25
  • 2
    The image you have posted is not a ture Bayer input (image in Bayer format looks like a grayscale image). It looks like a simulation that tries to illustrate the concept of Bayer format. – Rotem Jun 05 '23 at 20:47

2 Answers2

1

In your code, you are using cv::COLOR_BayerGBRG2BGR for the conversion, which may not match the actual Bayer pattern of your input image. The greenish output you are seeing could be a result of using the incorrect conversion. To solve this problem, you need to determine the correct Bayer pattern of your input image and use the appropriate conversion code. The most common Bayer patterns are BayerBG, BayerGB, BayerRG, and BayerGR. You should choose the correct pattern based on your camera sensor or image source.

0

Debayered pictures still need to be white-balanced. The raw sensor values aren't balanced.

There are various methods. You can

  • determine the balance manually (e.g. take the mean of each color plane) and then scale each color channel accordingly
  • apply histogram equalization to each color plane
  • anything more complex, to be found via research
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36