Bayer filter, bayer demosaicing OpenCV C++

675 Views Asked by At

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);
}
2

There are 2 best solutions below

0
On

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
0
On

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.