Group Abstract Group Abstract

Message Boards Message Boards

0
|
15.9K 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 11 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

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

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

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

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 have added comments to my previous post.

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
Posted 11 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
POSTED BY: Szabolcs Horvát
POSTED BY: Marcelo De Cicco
Posted 11 years ago
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