6

It seems that nikons own tool and photoshop has the option to open the image as they was taken.

but using libraws dcraw processor I cant figure this out.

here is my implementation.

CV_EXPORTS_W int load_image(const char * path, cv::Mat & output)
{


    LibRaw RawProcessor;

    int ret;

#define imgD RawProcessor.imgdata

    imgD.params.use_camera_wb = 1;
    imgD.params.use_auto_wb = 0;

    if ((ret = RawProcessor.open_file(path)) != LIBRAW_SUCCESS)
    {
        fprintf(stderr, path, libraw_strerror(ret));
        return -1;
    }
    if ((ret = RawProcessor.unpack()) != LIBRAW_SUCCESS)
    {

        return -1;
    }

    int check = RawProcessor.dcraw_process();
    libraw_processed_image_t *image_ptr = RawProcessor.dcraw_make_mem_image(&check);
                
    output = cv::Mat(cv::Size(image_ptr->width, image_ptr->height), CV_8UC3, image_ptr->data, cv::Mat::AUTO_STEP);
    cv::cvtColor(output, output, 4);
}

Updated with an image to show what I am talking about: The images are being normalized somehow. If the original image contains a large area of light matrial the overall image becomes more dark. I want to be able to just read the raw image data and normalize or handle it my own way in opencv.

enter image description here

update

Based on comments I got the brightness adjusted but a problem now arize with the pixel color values, best seen here:

enter image description here

The image on the left is the result of libraw and the right one is viewnx. There seem to be some noise colors in the libraw image.

Community
  • 1
  • 1
Poul K. Sørensen
  • 16,950
  • 21
  • 126
  • 283

1 Answers1

5

raw data is stored in LibRaw::rawdata.raw_image[] array. These values are 'as RAW as possible' without black subtraction and/on de-bayer (demosaic) applied.

To access this data you need to call LibRaw::open_file() and LibRaw::unpack.

Also, you may exclude some steps from data processing on LibRaw::dcraw_process():

 imgdata.params.no_interpolation=1 disables demosaic
 imgdata.params.no_auto_scale=1 disables scaling from camera maximum to 64k
 imgdata.params.no_auto_bright=1 disables auto brighten
  • Going to try that and mark as answer if it works. THanks – Poul K. Sørensen Mar 14 '14 at 11:38
  • The image is almost all black after using these 3. same code as in the question with your added parameter. Also the cvtColor is just doing BGR to RGB if thats not clear. – Poul K. Sørensen Mar 14 '14 at 11:41
  • that makes sense as the demosaic is disabled. The goal is to make the image look as they was taken (the thumbnail in the nef ect, also how nikons own tool extract jpgs) – Poul K. Sørensen Mar 14 '14 at 11:44
  • The 'exactly as taken' is RAW data. JPEG is postprocessed (white balance, demosaiced, tone curve applied, color converted from camera space to sRGB or whatever). LibRaw do not use any camera settings by default: WB is daylight and brightness is adjusted to have 1% saturated pixels. – Alex Tutubalin Mar 14 '14 at 16:57
  • I am at this point trying to extrac the RAW data and do a cv::COLOR_BayerBG2RGB conversion in openCV such I know no white balance, tone curve normalization have been done. We take images with 70% overlap from a plane and I want to do these changes based on the neighbour images also and not just the internal information of an image. Kinda want to do an image stiching and then apply all the tone curve and white balance changes. – Poul K. Sørensen Mar 14 '14 at 21:06
  • after unpack and accessing imgD.rawdata.raw_image for the raw data, i also need the size to to create my Cv::Mat, but none of the sizes in imgD.rawdata.sizes fits the size of the image. How do I get the real size? – Poul K. Sørensen Mar 14 '14 at 22:02
  • also tried use_camera_wb=1 and no_auto_bright=1 which is what gives the best result sofar. but that is to dark compared. – Poul K. Sørensen Mar 14 '14 at 22:36
  • 1) Today cameras are underexpose heavily (typical graypoint value is 7-10% instead of 18% graypoint). So, if use_camera_wb/auto_bright give you good direction, you may try to use positive exposure correction together with highlights compression. Try something like params.exp_correc=1; params.exp_shift=2.0; params.exp_preser=1.0; 2) After LibRaw::unpack() the imgdata.rawdata.sizes is exactly the same as imgdata.sizes. Use raw_pitch (in bytes!) as row pitch, raw_width/raw_height as image sizes. – Alex Tutubalin Mar 15 '14 at 06:25
  • 1) We are moving in the correct direction, image is now something like 5% darker compared to 25% darker (my impression). 2) I think Nikon Nef raw formats are storing data 2pixel/3bytes for E800 and opencv seems to only support CV_U16 for the Bayer to RGB. – Poul K. Sørensen Mar 15 '14 at 08:49
  • imgD.params.exp_shift = 4.0; is giving very very close to same result for my test image as viewNX do. Now I will do a large test on many images. If nikon know this stuff when exporting the image, and it is different for each camera, would we not be able to read that information somewhere what kind of exposure correction to use. – Poul K. Sørensen Mar 15 '14 at 08:56
  • exp_shift из +2EV correction. Yes, it is different for different cameras, very depends on exposure meter calibration – Alex Tutubalin Mar 16 '14 at 05:41
  • do you maybe have any sugestion to the updated questions. Seems that the colors are not as clear as my target output. – Poul K. Sørensen Mar 16 '14 at 14:52
  • created a new question : http://stackoverflow.com/questions/22439587/my-libraw-decoded-images-contains-noise – Poul K. Sørensen Mar 16 '14 at 16:34
  • I can't express how much headache your answer saved me. `no_interpolation` isn't in the docs as far as I can tell, and that flag was exactly what I wished I had. Thanks so much! – Syntactic Fructose Jun 15 '16 at 05:58