convert DNG to PNG with LibRaw and OpenCV

42 views Asked by At

I am do something about super-resolution with cpp now. I write a module to read a DNG file and convert it to PNG after some processing, I choose the LibRaw and OpenCV to achieve this.

The code runs successfully ,but the output file has different colors as the original.

my code is as follow:

LibRaw rawObj;
rawObj.open_file("C:/Users/yihee/Downloads/Samsung/Samsung/im_02.dng");
rawObj.unpack();
int ret = rawObj.dcraw_process();
libraw_processed_image_t* image = rawObj.dcraw_make_mem_image(&ret);
int width = rawObj.imgdata.sizes.iwidth;
int height = rawObj.imgdata.sizes.iheight;
at::Tensor ref_raw = torch::from_blob(image->data, {1, height,width,3}, torch::kByte).clone();
cv::Mat img = tensor2Image(ref_raw.to(torch::kByte));
cv::imshow("CR2 File", img);
cv::imwrite("out.png", img);

The tensor2Image function is:

cv::Mat tensor2Image(const torch::Tensor& t)
{
    torch::IntArrayRef s = t.sizes();
    if (s[0] == 1)
    {
        cv::Mat des(s[1], s[2], CV_8UC3, t.data_ptr<uchar>());
        return des;
    }
    else
    {
        uchar* r = (uchar*)t[0].data_ptr();
        uchar* g = (uchar*)t[1].data_ptr();
        uchar* b = (uchar*)t[2].data_ptr();
        int rows = s[1];
        int cols = s[2];
        cv::Mat des = cv::Mat::zeros(rows, cols, CV_8UC3);
        for (int row = 0; row < rows; row++)
        {
            for (int col = 0; col < cols; col++)
            {
                cv::Vec3b v;
                v[0] = r[row * cols + col];
                v[1] = g[row * cols + col];
                v[2] = b[row * cols + col];
                des.at<cv::Vec3b>(row, col) = v;
            }
        }
        return des;
    }
    return cv::Mat();
}

Here is the file (original is first)

enter image description here

enter image description here

I search for some article and try to change by correcting white balance:

rawObj.imgdata.color.cam_mul

and my white balance is

[2.0237,1.0,1.3385,0.0]

CFA pattern:

[1 0 2 1]

black levels

[0 0 0 0]

white level

1023

I have tried kinds of methods to correct,but colors still strange.

The issue reading-raw-image-with-libraw-and-converting-to-dng-with-libtiff is similar to me (but also no ans)

What can i do? can someone give me a direction?

0

There are 0 answers