Matlab DNG image processing: CFA extraction yields black output

I’m trying to extract the Color Filter Array from a DNG file using Matlab. I followed a tutorial I found online, but I’m running into issues. Here’s the code I’m using:

imageInfo = getImageDetails('camera_raw.dng');
tiffObj = openTiffFile('camera_raw.dng', 'read');
subDirOffsets = tiffObj.getTagValue('SubDirectoryIFD');
tiffObj.setCurrentSubDir(subDirOffsets(1));
colorFilterArray = double(tiffObj.readCurrentImage());

When I run this, I end up with a colorFilterArray that’s all zeros. When plotted, it’s just a black image. I’m pretty sure I’m missing something, but I can’t figure out what. Has anyone successfully extracted the Bayer array from a DNG file in Matlab? Any tips or suggestions would be really helpful. I’m new to working with raw image formats, so I might be overlooking something obvious. Thanks!

I’ve worked with DNG files in MATLAB before, and extracting the CFA can be tricky. One common issue is that the raw data might be stored in a different IFD (Image File Directory) than you’re accessing. Try iterating through all SubDirectoryIFDs instead of just the first one.

Also, make sure you’re using the correct tag for the raw image data. Sometimes it’s ‘StripOffsets’ or ‘TileOffsets’ instead of directly reading the image. You might need to use tiffObj.getTag() to find the right data location.

Lastly, check if your DNG file is compressed. If it is, you’ll need to decompress it first using the appropriate algorithm (usually lossless JPEG). MATLAB’s imfinfo() function can give you details about compression.

If none of these solve your issue, you might want to try using a dedicated raw processing library like LibRaw, which has MATLAB bindings available.

hey Luke, i had similar issues. try using the ‘CFAPattern’ tag to get the bayer pattern first. then use that to interpret the raw data. also, check if ur DNG is compressed - u might need to decompress b4 extracting. good luck mate!

Hey there Luke87! I’ve dabbled with DNG files in MATLAB too, and it can be a bit of a headache sometimes. Have you checked if your DNG file is actually storing the raw data where you expect it to be?

Sometimes the raw image data is tucked away in a different part of the file structure. Maybe try poking around with imfinfo() to see what other tags and info you can find in your DNG?

Also, I’m curious - what camera are you working with? Some manufacturers do quirky things with their raw formats that might throw a wrench in the works.

Oh, and don’t forget to double-check your bit depth! If the raw data is stored in a higher bit depth than you’re expecting, it might look all black when you try to display it normally.

Keep us posted on what you find out! Raw processing can be a real rabbit hole, but it’s super interesting once you get the hang of it.