Loading in raw image data using the Tiff class: Images return as all zeros, multiple warnings

14 views (last 30 days)
I have image data in the Nikon .NEF raw format that I would like to open in MATLAB for some image processing. To accomplish this, I have followed Steve Eddins' blog post on the subject. Specifically, I am converting my .NEF files to digital negative (.dng) files using the Adobe DNG Converter, then using the Tiff class to read in the data.
I should also note that I am able to view my converted .dng files externally to MATLAB, and there are no apparent problems with the conversion.
My images are 4288x2848 px, and simply calling the imread() function on them returns a smaller thumbnail, which seems consistent with the blog post. Since I would like to view them in full resolution, I run the following lines, copied from the blog:
warning off MATLAB:tifflib:TIFFReadDirectory:libraryWarning
t = Tiff(fullpath,'r'); %fullpath = path of .dng file
offsets = getTag(t,'SubIFD');
setSubDirectory(t,offsets(1));
cfa = read(t);
close(t);
However, the data read into cfa returns as a 2868x4310 uint16 of zeros. I suspect this has something to do with my offsets, but I don't know enough about .tiff and SubIFDs in particular to really know where to go from here.
A few relevant warnings that appear in the code are copy+pasted below:
The following appears after calling Tiff() and getTag():
Warning: TIFF library warning: TIFFReadDirectory: [full path of .dng redacted]: unknown field with tag 37393 (0x9211) encountered..
This warning appears numerous times with numerous tags listed.
The following appears after attempting to read in the data into cfa:
Warning: TIFF library warning: JPEGPreDecode: Improper JPEG strip/tile size, expected 256x240, got 128x240..
Warning: TIFF library warning: JPEGLib: Must downscale data from 16 bits to 8..
Apologies if it sounds like I am just dumping this on the forum and saying "fix this," as that is not my intention. As I said, I know next to nothing about .tiff files, so if my problem is one that could be solved simply by reading up on them, relevant sources in lieu of a line-by-line solution would be appreciated.
  6 Comments
TaylorB
TaylorB on 17 May 2020
Thanks for that useful list--it's a good resource and I'll check it against the other warnings I received.
I'm not sure what I can do about the difficulty of accessing the SubIFD information, which these NEF files seem to have stubbornly hidden. One thought which occurred to me is that the camera might be set to store "compressed raw" images, an option on the D800E. If so, I suppose this might override the custom setting of checking "uncompressed" in the Adobe DNG Converter settings. Unfortunately, I can't check the camera setting now because it's in the lab and there's no access during the virus shutdown.
I downloaded a tool called "Tiff Tag Reader" by Dirk-Jan Kroon (https://uk.mathworks.com/matlabcentral/fileexchange/36958-tiff-tag-reader) that allowed me to see that there are five SubIFD structures in the converted DNG file, but inspection of these structures doesn't yield any linearization table. I'm very new to this, so I'm still exploring what the best way is to handle NEF files in Matlab. I might just use dcraw outside of matlab to process the images, but I was hoping I could implement a workflow entirely within Matlab.

Sign in to comment.

Accepted Answer

Evan
Evan on 8 Jul 2013
It looks as if my problems stemmed not from the Matlab code, but from Adobe's DNG converter. The below settings in the DNG converter solved my problem:
Preferences > Compatability > Custom > Backward Version > DNG 1.3
Preferences > Compatability > Custom > Uncompressed
  1 Comment
KAE
KAE on 15 Mar 2023
These settings worked for me except the backward version I used was "DNG 1.6". After that Matlab had no problems reading in the DNG as non-zero values.

Sign in to comment.

More Answers (3)

Reem
Reem on 16 Sep 2014
I have tried the same Adobe DNG Converter settings .. and whenever I tired
imtool(cfa)
to display my image I got a black image. Any clue?!

Image Analyst
Image Analyst on 7 Jun 2016
One option that worked for me is to use a very inexpensive image management program, ACDSee, http://www.acdsee.com/ to convert the file to a standard .PNG format. You can select one of more image files in ACDSee by clicking on their thumbnails and then simply right click on the .NEF file and select Batch->ConvertFileFormat and select .PNG, .TIFF, or any of a bunch of other image formats. Then you just read in a standard image format file, like PNG, with MATLAB.

Image Analyst
Image Analyst on 8 Jul 2013
Try responding to Steve by posting a link to this question on his blog. Don't post your whole question, just say you have a question on http://www.mathworks.com/matlabcentral/answers/81448-loading-in-raw-image-data-using-the-tiff-class-images-return-as-all-zeros-multiple-warnings and ask if he can help.
  4 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!