Matze
Matze

Reputation: 11

Demosaicing RCCG color filter array to RGB with OpenCV

I’m trying to convert a raw image from a camera with RCCG (Red, Clear, Clear, Green) Color Filter Array into an RGB image.

My idea was to convert RCCG into YCrCg and then into YCrCb. To be able to use the OpenCV method to convert YCrCb to RGB.

This is my first non-working approach in C++ with OpenCV:

cv::Mat tmpMat = Mat::zeros(dst.rows, dst.cols, dst.type());

// Debayering from RCCG to RCG
cv::demosaicing(src, tmpMat, cv::ColorConversionCodes::COLOR_BayerBG2RGB); 

float R{};
float C{};
float G{};

float Y{};
float Cr{};
float Cg{};

for (int row = 0; row < tmpMat.rows; row++)
{
    for (int col = 0; col < tmpMat.cols; col++)
    {
        R = tmpMat.at<cv::Vec3w>(row, col)[0];
        C = tmpMat.at<cv::Vec3w>(row, col)[1];
        G = tmpMat.at<cv::Vec3w>(row, col)[2];

        // Convert RCCG to YCrCg
        Y = C;
        Cr = C - 0.299F * R;
        Cg = C - 0.587F * G;

        // Convert YCrCg to YCrCb by swapping Cr and Cg
        dst.at<cv::Vec3w>(row, col)[0] = static_cast<uint16_t>(Y);
        dst.at<cv::Vec3w>(row, col)[1] = static_cast<uint16_t>(Cg);
        dst.at<cv::Vec3w>(row, col)[2] = static_cast<uint16_t>(Cr);
    }
}

cv::cvtColor(dst, dst, cv::ColorConversionCodes::COLOR_YCrCb2BGR);

Could anyone help me with that?

Upvotes: 1

Views: 780

Answers (0)

Related Questions