OpenCV implements BGR2BayerGB/BG format conversion

1. Description

OpenCV only provides an interface to generate BGR format from Bayer conversion

 However, the opposite is not true, ie:

OpenCV does not provide an interface for converting from BGR to Bayer format, so you need to write it yourself:
OpenCV defines 4 formats, namely:
BGGR arrangement -> RG format (referred to as two letters, two letters are taken from the end to the head)
RGGB arrangement -> BG format
GRBG arrangement -> GB format
GBRG arrangement -> GR format

2. Conversion

void CUtils::BGR2BayerGB(const cv::Mat &matSrc, cv::Mat &matDst) //BGR格式转换为BayerGB格式
{
    //GRBG排列 -> GB格式
    matDst = cv::Mat(matSrc.size(), CV_8UC1, cv::Scalar::all(0));

    for (int y = 0; y < matSrc.rows; ++y)
    {
        for (int x = 0; x < matSrc.cols; ++x)
        {
            cv::Vec3b colorSrc = matSrc.at<cv::Vec3b>(y, x);
            uchar colorDst = 0;

            if (y % 2 == 0)
            {
                colorDst = (x % 2 == 0) ? colorSrc[1] : colorSrc[2]; //G:R
            }
            else
            {
                colorDst = (x % 2 == 0) ? colorSrc[0] : colorSrc[1]; //B:G
            }

            matDst.at<uchar>(y, x) = colorDst;
        }
    }
}

void CUtils::BGR2BayerBG(const cv::Mat &matSrc, cv::Mat &matDst) //BGR格式转换为BayerBG格式
{
    //RGGB排列 -> BG格式
    matDst = cv::Mat(matSrc.size(), CV_8UC1, cv::Scalar::all(0));

    for (int y = 0; y < matSrc.rows; ++y)
    {
        for (int x = 0; x < matSrc.cols; ++x)
        {
            cv::Vec3b colorSrc = matSrc.at<cv::Vec3b>(y, x);
            uchar colorDst = 0;

            if (y % 2 == 0)
            {
                colorDst = (x % 2 == 0) ? colorSrc[2] : colorSrc[1]; //R:G
            }
            else
            {
                colorDst = (x % 2 == 0) ? colorSrc[1] : colorSrc[0]; //G:B
            }

            matDst.at<uchar>(y, x) = colorDst;
        }
    }
}

3. Test

void Widget::on_pushButton_BGR2BayerBG_clicked()
{
    QFileDialog *fileDlg = new QFileDialog(this);
    fileDlg->setWindowTitle(tr("打开图片"));

    QStringList qstrFilters;
    qstrFilters << "picture(*.bmp;*.jpg;*.png)";
    fileDlg->setNameFilters(qstrFilters);            //设置文件过滤器
    fileDlg->setFileMode(QFileDialog::ExistingFile); //设置选择单文件,如果是多个文件就写成ExistingFiles
    fileDlg->setAcceptMode(QFileDialog::AcceptOpen);

    if (fileDlg->exec() == QDialog::Accepted)
    {
        QStringList strPathList = fileDlg->selectedFiles();
        if (strPathList.count() > 0)
        {
            //文件路径
            QString file_path = strPathList.at(0);

            //打开图片
            cv::Mat m1;
            m1 = UTILS->readMat(file_path, cv::IMREAD_COLOR);

            //转换
            cv::Mat m2;
            UTILS->BGR2BayerBG(m1, m2);
            UTILS->writeMat("d:\\test_BGR2BayerBG.bmp", m2);

            //验证
            cv::Mat m3;
            cv::cvtColor(m2, m3, cv::COLOR_BayerBG2BGR);
            UTILS->writeMat("d:\\test_BayerBG2BGR.bmp", m3);
        }
    }

    fileDlg->close();
    delete fileDlg;
    fileDlg = nullptr;
}

Guess you like

Origin blog.csdn.net/libaineu2004/article/details/132381748