Message Boards Message Boards

0
|
14360 Views
|
15 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How to implement Raw libraries in order to manipulate raw images as .NEF ?

Posted 10 years ago

Dear friends,

I have a lot of Raw images in .NEF format (NIKKON). So, I read the section : http://reference.wolfram.com/language/LibraryLink/tutorial/ImageProcessing.html

But I did not understand how implement these libraries in order to work with .NEF extension images, for astrophotography.

EDIT 1: a sample of image *.NEF: https://www.dropbox.com/s/2n5y5xjhhqeh7k0/DSC_5133.NEF?dl=0

POSTED BY: Marcelo De Cicco
15 Replies

But it seems correct

EDIT 1: OK , I saw the above path error, I changed it, and now the following error:

Out[185]= "C:\cygwin64\home\decicco\LibRaw-0.16.0\build"

During evaluation of In[180]:= CreateLibrary::cmperr: Compile error: LINK : fatal error LNK1181: cannot open input file 'libraw.lib' >>

Out[187]= $Failed LibraryFunction::notfound: Symbol C:\cygwin64\home\decicco\LibRaw-0.16.0\build\libraw not found. >>

It is not able to find the libraw.

EDIT 2.: There is no ´build directory´, and there is libraw directory.

POSTED BY: Marcelo De Cicco

Ok, I tried runnig but the following error came:

CreateLibrary::cmperr: Compile error: C:\Program Files\Wolfram Research\Mathematica\10.0\SystemFiles\Links\LibraryLink\LibraryResources\Source\image_external.c(4) : fatal error C1083: Cannot open include file: 'cv.h': No such file or directory >>

Although the file image_external is present in the source directory:

C:\Program Files\Wolfram Research\Mathematica\10.0\SystemFiles\Links\LibraryLink\LibraryResources\Source\image_external

What is "cv.h"? Is a header from image_external.c? Would be a bug?

POSTED BY: Marcelo De Cicco

I forgot that image_external.c requires both OpenCV and LibRaw. Please replace the content of image_external.c with the following code:

/* Include required header */
#include "WolframLibrary.h"
#include "WolframImageLibrary.h"
#include "libraw.h"

/* Return the version of Library Link */
DLLEXPORT mint WolframLibrary_getVersion( ) {
    return WolframLibraryVersion;
}

/* Initialize Library */
DLLEXPORT int WolframLibrary_initialize(WolframLibraryData libData) {
    return LIBRARY_NO_ERROR;
}

/* Uninitialize Library */
DLLEXPORT void WolframLibrary_uninitialize(WolframLibraryData libData) {
    return;
}

EXTERN_C DLLEXPORT int read_raw_image(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument res) {
    int err;
    int check;
    MImage out;
    char * file;
    libraw_data_t *iprc = libraw_init(0);
    libraw_processed_image_t * img;
    WolframImageLibrary_Functions imgFuns = libData->imageLibraryFunctions;

    err = LIBRARY_FUNCTION_ERROR;
    file = MArgument_getUTF8String(Args[0]);

    libraw_open_file(iprc, file);
    libraw_unpack(iprc);

    iprc->params.output_bps = 8;

    check = libraw_dcraw_process(iprc);
    if (check != LIBRAW_SUCCESS) goto cleanup;

    img = libraw_dcraw_make_mem_image(iprc, &check);
    if (img == NULL) goto cleanup;
    if (img->type != LIBRAW_IMAGE_BITMAP || img->colors != 3) goto cleanup;

    if (img->bits == 16) {
       raw_t_ubit16 * raw_data = (raw_t_ubit16*)img->data;
       imgFuns->MImage_new2D(img->width, img->height, 3, MImage_Type_Bit16, MImage_CS_RGB, 1, &out);
       memcpy(imgFuns->MImage_getBit16Data(out), raw_data, img->width * img->height * 3 * sizeof(raw_t_ubit16));
    } else if (img->bits == 8) {
       raw_t_ubit8 * raw_data = (raw_t_ubit8*)img->data;
       imgFuns->MImage_new2D(img->width, img->height, 3, MImage_Type_Bit8, MImage_CS_RGB, 1, &out);
       memcpy(imgFuns->MImage_getByteData(out), raw_data, img->width * img->height * 3 * sizeof(raw_t_ubit8));
    } else {
       goto cleanup;
    }

    MArgument_setMImage(res, out);
    err = LIBRARY_NO_ERROR;

cleanup:
    libData->UTF8String_disown(file);
    libraw_dcraw_clear_mem(img);
    return err;
}
POSTED BY: Piotr Wendykier

Hi, Piotr!

Well I did , but now the following error happened:

During evaluation of In[162]:= CreateLibrary::cmperr: Compile error: C:\Program Files\Wolfram Research\Mathematica\10.0\SystemFiles\Links\LibraryLink\LibraryResources\Source\image_external.c(5) : fatal error C1083: Cannot open include file: 'libraw.h': No such file or directory >>

Out[169]= $Failed

During evaluation of In[162]:= LibraryFunction::notfound: Symbol C:\cygwin64\home\decicco\LibRaw-0.16.0\libraw\build\libraw not found.

But ´libraw.h´ is there: C:\cygwin64\home\decicco\LibRaw-0.16.0\libraw\libraw.h

POSTED BY: Marcelo De Cicco

Marcelo, your include path and library path are incorrect. You need to set them correctly.

POSTED BY: Piotr Wendykier

I compiled and installed the Libraw files using cygwin in Windows environment using the instructions in: LibRaw install

You mentioned compiling also : ".... imageexternal.c, load the LibRaw library and load the readraw_image function...";

Will Mathematica performance this for (or I have call cygwin again)?

POSTED BY: Marcelo De Cicco

I have added comments to my previous post.

POSTED BY: Piotr Wendykier
Posted 10 years ago

ok!

Thanks!

POSTED BY: Updating Name

Dear Marcelo,

You need to download the source code of LibRaw library: http://www.libraw.org/download#stable and build it. Then, you need to compile image_external.c, load the LibRaw library and load the read_raw_image function. Here is an example for Windows:

Needs["LibraryLink`"]
Needs["CCompilerDriver`"]
sourcePath="c:\\Program Files\\Wolfram Research\\Mathematica\\10.0\SystemFiles\\Links\\LibraryLink\\LibraryResources\\Source";
librawDir="c:\\libraw";
librawIncludeDir=FileNameJoin[{librawDir,"libraw"}];
librawLibDir=FileNameJoin[{librawDir,"build"}];
librawLibraries={"libraw"};
(*Compile "image_external.c*) 
lib=CreateLibrary[{FileNameJoin[{sourcePath,"image_external.c"}]},"image_external","IncludeDirectories"->librawIncludeDir,"LibraryDirectories"->librawLibDir,"Libraries"->librawLibraries,"Defines"->"WIN32"];
(*Load LibRaw*)
LibraryLoad[FileNameJoin[{librawLibDir,"libraw"}]];
(*Load read_raw_image*)
readRawImage = LibraryFunctionLoad[lib, "read_raw_image", {{"UTF8String"}}, LibraryDataType[Image]]`

Finally, you can import raw images by calling:

readRawImage[pathToRawFile]
POSTED BY: Piotr Wendykier

So if we want use the raw image, first it is more useful do the reductions (darks, bias, flats, etc..) in a suitable program and then converting the results to a suited format for being manipulate inside mathematica.

POSTED BY: Marcelo De Cicco

I really don't know much about astrophotography, but don't modern Nikon cameras do in-camera dark-frame subtraction already? The camera calls it "long exposure noise reduction".

If you'd want to turn this off in camera and do it manually, then the dcraw based method I suggested does really give you data which is suitable for this.

dcraw -o 0 -D -T -6 infile.nef

will create a TIFF that has un-demosaiced, unprocessed data. The pixel values will be proportional to the charge accumulated at each photosite. The TIFF you get will look like this (no. 2) due to lack of demosaicing.

You can then process it in Mathematica.

But I imagine that eventually you would want to export this data from Mathematica and do proper demosaicing on it. I don't know what tool will be able to do this well. I don't know of any easy way to put the processed data back into a NEF file so you can use Lightroom, ViewNX or other RAW converters that know the camera's characteristics and can do a correct conversion.

This is why I stopped playing with RAW data in Mathematica. I could do some useful processing on it (I wanted to do de-banding), but I had no way to convert this data to a usable photograph afterwards.

POSTED BY: Szabolcs Horvát

Hi David,

In fact, I use raw images to do the following corrections: Dark frames and bias, mainly, so I can correct the termal noise from ccd,and other distorsions as vigneting etc...I do not ever try using tiff format. I ll try the Szlabocs suggestions.

Before using photoshop , or ligthroom I would like to clean the image using mathematica codes applied for these darks and bias reductions. So we need the crude informations from sensors to do that. My intend is explore the Mathematica programming in the same path that sherlock´s http://blog.wolfram.com/2014/08/14/fixing-bad-astrophotography-ii-imaging-mars-with-mathematica/ did (with. fits extension), but using dark and bias reductions first and then aplying other filtering codes..

POSTED BY: Marcelo De Cicco
Posted 10 years ago

Hi Marcelo, I do a lot of photography with a Nikon camera. The raw NEF images are exactly what is read from the imager. That means they lack even the color correction needed to compensate for the spectral response of the imager. I am doing photography -- not data analysis -- but I convert images by reading them into Adobe LightRoom, often to jpeg for printing, but sometimes to tiff. Adobe Camera Raw and Photoshop will also read raw images and convert them as needed. I think your best solution (as someone on StackExchage also recommended) is to read your images with such a tool and covert to tiff. This avoids the artifacts of jpeg compression, but can correct for color as needed. If you have in fact calibrated for the spectral response of the camera imager you could probably also create a tiff with no color correction, only the pixel values in tiff format so Mathematica can read them. If that is what you need, let me know and I'll look into it.

Kind regards, David

POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract