A script to merge several DNG files into one (digital ND filter, HDR tool, noise reducer)

Anton Wolf

Member
Messages
12
Reaction score
4
Hi all,

this is my first post in this forum, I hope some may find this topic interesting.

I created a little batch script that takes several DNG raw files and creates a new DNG that is the average if the input. This can be like a digital ND filter. Also, this improves quality a lot, noise can go to almost zero. This can be quite useful for HDR post processing.

I published it here, along with instructions how to use it and more: https://github.com/antonwolf/dng_stacker

Can we have a discussion on:
  • Is this useful to some of you?
  • Do you think this is useful to a broader audience?
  • Is there alerady another tool out there that does this? I could not find one.
  • Why do high-end cameras not do this out of the box? Is this not a very straightformward way to improve the capabilities of a camera?
Samples:

98 images combined. Camera: iPhone 7
98 images combined. Camera: iPhone 7



38 frames combined. Camera: XT2 w/ 23mm f/1.4
38 frames combined. Camera: XT2 w/ 23mm f/1.4
 
Hi Anton, neat, anybody who put in the time to master the DNG SDK deserves high praise.

As for utility, my opinion is that it could be useful in very specific applications, though to know what those might be it would be good to understand the practical advantages of using a stacked DNG image vs stacking after demosaicing, with the same rendering. What have you measured/seen and can you share some samples?

Jack
 
Impressive!

I tried it out with 21 raw files and underexposed at ISO 400. Got som vertical lines/pattern in in the underexposed area but in the other area completely noiseless.

Will try it more.
 
Thanks for testing this, I appreciate this.

In one of my tests, I underexposed by three stops and I got some strange looking gradients when I recovered the shadows.

If you get a completely symmetric pattern, then it could be im a not subtracting the blacklevels correctly.

Can you share some of the raw files with me, I am curious if I can fix this.
 
Thank you :-)

It never really occurred to me that you might as well stack the JPGs. The results should be very similar, I guess.

I believe there are some good advantages of stacked DNGs:
  • It's easy - put the DNGs in a folder, start the batch file, wait, done.
  • The stacked DNG is only as big as one raw file. If you want to do a non-destructive edit, you have to keep much less data than if you want to stack the JPEGs later.
  • While editing the DNG, you can really assess the final look better because it is much less noisy.
  • I don't have a user-friendly tool to stack a hundred rendered JPEGs (best I got is imagemagick which is command-line)
  • Rendering many raw files takes pretty long on my Surface Pro 7 and rendering the stacked DNG is much faster.
I would not really say I mastered the DNG SDK. I did not manage to build the SDK from source code, but I found out that the SDK ships with pre-compiled dng_validate.exe which does everything I needed.

I am sure this tool could run 20 times faster if I knew how to code it in C++ using the DNG SDK. I really hope someone will create a faster DNG stacker, all I can do is promote the idea of stacking DNGs and write this cheap batch script.

Maybe in a few months thousands of landscape photographers will start stacking DNGs and Fuji, Sony, Canon, Nikon, Phase One and Adobe will make raw stacking a feature of their cameras or their raw converters. Who knows :-)
 
I am sure this tool could run 20 times faster if I knew how to code it in C++ using the DNG SDK. I really hope someone will create a faster DNG stacker, all I can do is promote the idea of stacking DNGs and write this cheap batch script.
I've considered writing one but just haven't been motivated enough. In the meantime you could speed up your solution by running multiple, parallel invocations of dng_validate (via Windows's "start" cmd invocation) if that is one of the bottlenecks.
Maybe in a few months thousands of landscape photographers will start stacking DNGs and Fuji, Sony, Canon, Nikon, Phase One and Adobe will make raw stacking a feature of their cameras or their raw converters. Who knows :-)
When ACR/LR added HDR/pano stacking support they implemented the ability to combine multiple raws into a single DNG. Unfortunately they didn't include a straight median/mean stacking feature as part of this even though it would be a trivial addition on top of the logic they added for HDR/panos.
 
Hello,

I had thought about such a tool using imagemagick provided it does align the images, which imagemagick can do I guess.

Otherwise, this would have very iittle benefit for me, i can stack easily with good results !

So this would have great benefit for handheld shots.

But there is more.... much more (imho) !!!

Only stack automatically the images which are the sharpest. Again this is usefull for handheld shots.

This would mean:

1: align the pictures

2: divide the image in areas and compare for instance rhe contrast between the different images, you will easily detect the images which are blurry (due to camera shake).

3: stack only the sharp images

Your tool would be much more usefull, just my opinion.

Precisions for point 2: why dividing in areas ? Because for instance an object may enter in the scene , changing the global contrast. So if you see over 10 areas, 8 ar sharper and 2 are not, you can consider that the image is sharper than the other image. This is just to give the main idea.
 
Thanks for testing this, I appreciate this.

In one of my tests, I underexposed by three stops and I got some strange looking gradients when I recovered the shadows.

If you get a completely symmetric pattern, then it could be im a not subtracting the blacklevels correctly.

Can you share some of the raw files with me, I am curious if I can fix this.
Tried a little bit more now. darktable has the same feature (HDR tool), but the raw files are more like 70-80MB than 13-25MB. The result seems to be the same. I have uploaded 2,3GB raw files and darktable DNG files vs your DNG files. I will remove the link after a week or more. https://drive.google.com/open?id=1RaE1oEsVNdAsn7cV42pFFUrqzmbFMEB5
 
Thank you :-)

It never really occurred to me that you might as well stack the JPGs. The results should be very similar, I guess.

I believe there are some good advantages of stacked DNGs:
  • It's easy - put the DNGs in a folder, start the batch file, wait, done.
  • The stacked DNG is only as big as one raw file. If you want to do a non-destructive edit, you have to keep much less data than if you want to stack the JPEGs later.
  • While editing the DNG, you can really assess the final look better because it is much less noisy.
  • I don't have a user-friendly tool to stack a hundred rendered JPEGs (best I got is imagemagick which is command-line)
  • Rendering many raw files takes pretty long on my Surface Pro 7 and rendering the stacked DNG is much faster.
My current workflow for stacking (whether the exposures are identical or bracketed) is along the lines of:
  1. use darktable to export the RAW files to linear TIFF after applying lens correction only (and sometimes the “defringe” module)
  2. import the TIFF files into Hugin/PanoTools to align them and merge them into an EXR file
  3. import the exported EXR file back into darktable and edit it like I would a RAW file, just with plenty of dynamic range.
What’s nice about it is that it generalizes well to stitching (since Hugin/PanoTools are made for that). In such cases, likewise, I use darktable to edit the assembled panorama as if it were one big RAW file.

It also allows me to use masks to remove undesirable movement in the scene. (Although last time, I forgot to also check someone’s shadow…)
 
Last edited:
Thanks for the script. Looks interesting.

BTW, back in 2009 I used PhotoAcute & Adobe DNG Converter to stack 14x 14.6MP Pentax K20D PEF (raw) files. The 14 shots were done in a handheld burst at ISO3200 to test the NR and auto-aligning capabilities of PhotoAcute. You see the results here:

https://www.pentaxforums.com/forums...74707-photoacute-studio-nr-stacking-test.html

I had to convert the PEFs to DNGs first. (PhotoAcute would invoke the DNG converter automatically if it was given a raw format it couldn't handle.) After stacking, I had one DNG file of standard size.

What I didn't realise at the time was that the raw files in the K20D were "cooked" (NR was applied to them) when you went above ISO400. Preferably, you'd like to stack raw files without any NR filtering already applied.

In 2009 it took a long,long time to auto-align the images. The program was also prone to running into memory issues handing the relatively large amount of data. I was on WinXP at the time. Now with a 64-bit OS and a 64-bit version of the program things should be better.

Dan.
 
Last edited:
My current workflow for stacking (whether the exposures are identical or bracketed) is along the lines of:
  1. use darktable to export the RAW files to linear TIFF after applying lens correction only (and sometimes the “defringe” module)
  2. import the TIFF files into Hugin/PanoTools to align them and merge them into an EXR file
  3. import the exported EXR file back into darktable and edit it like I would a RAW file, just with plenty of dynamic range.
What’s nice about it is that it generalizes well to stitching (since Hugin/PanoTools are made for that). In such cases, likewise, I use darktable to edit the assembled panorama as if it were one big RAW file.

It also allows me to use masks to remove undesirable movement in the scene. (Although last time, I forgot to also check someone’s shadow…)
I am pretty sure process could be automated as well with a batch file or similar. However, it is very different from what I made.

My process is much less intelligent than this.

dng_stacker does not even demosaic the raw files. This means there is no processing applied except median stacking. It also means it only works if the pictures are taken on a tripod.

The advantage of my very "dumb" workflow is that you get rather small files and there is not a bit of processing applied, so you can still do whatever you want with it.
 
I've considered writing one but just haven't been motivated enough.
That's why I chose the batch script when I found out there are command line tools for everything. Learning C++ just to speed this up is not really worth it. My computer does not lock up while processing, so I just do something else in that time.
In the meantime you could speed up your solution by running multiple, parallel invocations of dng_validate (via Windows's "start" cmd invocation) if that is one of the bottlenecks.
This is a great idea! Thanks
When ACR/LR added HDR/pano stacking support they implemented the ability to combine multiple raws into a single DNG. Unfortunately they didn't include a straight median/mean stacking feature as part of this even though it would be a trivial addition on top of the logic they added for HDR/panos.
It's a pity they did not think of this.

I think it is really funny how they implemented a lot of logic to deghost HDRs. Mean stacking and median stacking is faster, easier, has less artifacts and it can generate much smaller files.

But honestly, I wish this was just a feature of my camera.
 
Tried a little bit more now. darktable has the same feature (HDR tool), but the raw files are more like 70-80MB than 13-25MB. The result seems to be the same. I have uploaded 2,3GB raw files and darktable DNG files vs your DNG files. I will remove the link after a week or more. https://drive.google.com/open?id=1RaE1oEsVNdAsn7cV42pFFUrqzmbFMEB5
Thanks for sharing these samples Peter.

The pattern you are getting on the Canon 7D is the same I saw on my Canon 5D Mk II. Is some very strange colored pattern in the shadows. It's not really noise. Noise disappears when you average it out, but is part of the signal. I think Canon did not have a great sensor design 11 years ago.

The tool I wrote is very simple and it cannot fix that. You might be more successful with exposure bracketing and an HDR tool. Or of you don't need to raise the shadows so much. Or with a newer camera :-)

I could not open the DNGs you created with darktable with Lightroom CC. Maybe they don't stick to the DNG standard. Maybe it is better if you don't delete the original RAWs if you ever want to use another raw converter.

The darktable HDRs are uncompressed 32bit float DNGs. My tool gives you compressed 16bit integer DNGs which is why they are much smaller.

I chose integer of float because the raw files are always integer as well.

I chose 16 bits because most cameras capture 14bits, so more than 16bits would probably just be noise.

8e1da7c487344123a8a81ccbcce9199e.jpg
 
Tried a little bit more now. darktable has the same feature (HDR tool), but the raw files are more like 70-80MB than 13-25MB. The result seems to be the same. I have uploaded 2,3GB raw files and darktable DNG files vs your DNG files. I will remove the link after a week or more. https://drive.google.com/open?id=1RaE1oEsVNdAsn7cV42pFFUrqzmbFMEB5
Thanks for sharing these samples Peter.

The pattern you are getting on the Canon 7D is the same I saw on my Canon 5D Mk II. Is some very strange colored pattern in the shadows. It's not really noise. Noise disappears when you average it out, but is part of the signal. I think Canon did not have a great sensor design 11 years ago.

The tool I wrote is very simple and it cannot fix that. You might be more successful with exposure bracketing and an HDR tool. Or of you don't need to raise the shadows so much. Or with a newer camera :-)

I could not open the DNGs you created with darktable with Lightroom CC. Maybe they don't stick to the DNG standard. Maybe it is better if you don't delete the original RAWs if you ever want to use another raw converter.

The darktable HDRs are uncompressed 32bit float DNGs. My tool gives you compressed 16bit integer DNGs which is why they are much smaller.

I chose integer of float because the raw files are always integer as well.

I chose 16 bits because most cameras capture 14bits, so more than 16bits would probably just be noise.

8e1da7c487344123a8a81ccbcce9199e.jpg
Actually, the first set I tried was with 6D (lens case pictures). With 7D I get the maze pattern 7D and 5D II have this) and need to use VNG4 as demosaicing method.

I couldn't open the DNG files from darktable either in RawTherapee until I realised I didn't use RawTherapee 5.8.
 
  • Why do high-end cameras not do this out of the box? Is this not a very straightformward way to improve the capabilities of a camera?
IIRC Canons starting from 5DM3 can do multiexposure stacking, with average, median, min and max filtering, with one raw file as result so it can be used in PP-less competitions.

Why? There is negligible user demand for this feature. If it exists already and you don't know about it, then even less tech savvy people won't.
 
  • Why do high-end cameras not do this out of the box? Is this not a very straightformward way to improve the capabilities of a camera?
IIRC Canons starting from 5DM3 can do multiexposure stacking, with average, median, min and max filtering, with one raw file as result so it can be used in PP-less competitions.
Why? There is negligible user demand for this feature. If it exists already and you don't know about it, then even less tech savvy people won't.
You are right.

Canon seems to offer this feature on their high-end cameras. Not sure how easy it would be to make them stack 20 pictures in a row. It does not seem like they offer this easy feature on cheaper cameras.

https://support.usa.canon.com/kb/index?page=content&id=ART170176&cat=8035B&actp=LIST

I believe Olympus has this as well on high-end cameras.

With mean stacking, you can get awesome quality even out of very bad and small sensors (I tested the iPhone 7), so it is very useful to have this on lower-end cameras as well.

I use a Fuji X-T2, I only have JPEG multi exposure stacking. For me this feature is very useful.
 
  • Why do high-end cameras not do this out of the box? Is this not a very straightformward way to improve the capabilities of a camera?
IIRC Canons starting from 5DM3 can do multiexposure stacking, with average, median, min and max filtering, with one raw file as result so it can be used in PP-less competitions.
Why? There is negligible user demand for this feature. If it exists already and you don't know about it, then even less tech savvy people won't.
You are right.

Canon seems to offer this feature on their high-end cameras. Not sure how easy it would be to make them stack 20 pictures in a row. It does not seem like they offer this easy feature on cheaper cameras.

https://support.usa.canon.com/kb/index?page=content&id=ART170176&cat=8035B&actp=LIST

I believe Olympus has this as well on high-end cameras.

With mean stacking, you can get awesome quality even out of very bad and small sensors (I tested the iPhone 7), so it is very useful to have this on lower-end cameras as well.

I use a Fuji X-T2, I only have JPEG multi exposure stacking. For me this feature is very useful.
Sony implemented this as well in their PlayMemories Smooth Reflection App, which supports an arbitrarily-large number of frames and generates a single raw output. Unfortunately Sony removed support for their app ecosystem in the most recent generations of their cameras.
 
Thanks for the script. Looks interesting.

BTW, back in 2009 I used PhotoAcute & Adobe DNG Converter to stack 14x 14.6MP Pentax K20D PEF (raw) files. The 14 shots were done in a handheld burst at ISO3200 to test the NR and auto-aligning capabilities of PhotoAcute. You see the results here:

https://www.pentaxforums.com/forums...74707-photoacute-studio-nr-stacking-test.html

I had to convert the PEFs to DNGs first. (PhotoAcute would invoke the DNG converter automatically if it was given a raw format it couldn't handle.) After stacking, I had one DNG file of standard size.

What I didn't realise at the time was that the raw files in the K20D were "cooked" (NR was applied to them) when you went above ISO400. Preferably, you'd like to stack raw files without any NR filtering already applied.

In 2009 it took a long,long time to auto-align the images. The program was also prone to running into memory issues handing the relatively large amount of data. I was on WinXP at the time. Now with a 64-bit OS and a 64-bit version of the program things should be better.

Dan.
Thanks for sharing - I just added some code for my tool to invoke the DNG converter as well.

My tool does not correct the alignment of the images, you need to use a tripod. I am really amazed by the quality you can get from stacking.

On my Fuji (which has a Sony sensor), I found it is best to shoot in the lowest ISO possible (200), stacking 16 ISO 800 is worse than 4 ISO 200.

Cheers

Anton
 
  • Why do high-end cameras not do this out of the box? Is this not a very straightformward way to improve the capabilities of a camera?
IIRC Canons starting from 5DM3 can do multiexposure stacking, with average, median, min and max filtering, with one raw file as result so it can be used in PP-less competitions.
Why? There is negligible user demand for this feature. If it exists already and you don't know about it, then even less tech savvy people won't.
Is it a good stack, though?

I don't remember which Canon it was, but IIRC the first one I had with RAW multiple-exposure files was clearly giving much higher standard deviation than it should, as if every time it added another exposure, it added it to a smoothed stack of the existing exposures.
 

Keyboard shortcuts

Back
Top