Thursday, September 26, 2013

Blurry vision I

Ok, well, this is not really about blurry vision, more about blurry photographs, and the methods to get rid of that blur.

I've been experimenting a bit with it in DNGMonochrome.

There's several solutions available (in software) out there, to deblur photos. Be it motion blur (camera shake) or blur due to focus errors. But the problem with those solutions (some are actually quite good) is that they only work on the finished end product. You have to export your RAW or DNG first to JPG or PNG, then deblur that photo.

I find that rather tedious, because once you've done all that and you decide you still need to make some adjustments to the RAW photo in e.g. Lightroom, you have to go through the whole process of deblurring the final export again. I regularly go back to older DNGs or CR2s and redo them. Software progresses, your own vision of how a photo should look might change, and then you still have to have the deblurring software around and remember the settings for that particular photo. It's just not very convenient.

I think this deblurring should take place on the DNG or the RAW file, before you start working on it in your RAW developer. Problem is: so far I haven't found software that actually tackles the RAW file.

So, introducing the blur/deblur filter in DNGMonochrome, with plans to extend it to color DNGs. Deblur first, then develop, in stead of develop, deblur afterwards.

Yes, ok, but wait...

How does this work?

Well, it works.

Let me show you...

Move over the image with your mouse to see the difference...


Small crop from a slightly blurred Canon 40D, most likely a focus error, converted with DNGMonochrome... note that this is a real life shot, not some fake blurred image... the monochrome DNG was then developed in Lightroom, with no sharpening and no noise reduction. Move over the image with your mouse to see the blur corrected version produced by DNGMonochrome, which got exactly the same treatment in Lightroom (no sharpening nor noise reduction)...

As you can see - although this example is quite subtle, I don't have many blurry real life shots, because I usually throw them - it really works.

The deblurring filter uses a Fourier transform of the photo (essentially this means the photo is split up in two separate collection of numbers, both belonging to the frequency domain: one collection is called the spectrum and the other is called the phase). After the Fourier transform the transformation is filtered using a second Fourier transform of a so called 'kernel'. That actually causes the blur / deblur to take place (I'm still working on the user interface of that one, because currently it's simply a free block you can click in to create the kernel). Then the Fourier representation is transformed back to the original photo.

One of the biggest issues I had to solve was the memory demand of this transform. The Fourier transform requires a huge amount of memory for the bigger photos. For your average M9 DNG we're talking 16 bytes per number (a real number and an imaginary number, 8 bytes per piece), times the size of the closest power of 2 of the longest edge of the photo. In case of the M and M9 that's 8192 by 8192. Now multiply that (8192 times 8192 times 16) and you get a whopping 1073741824 bytes (roughly 1 gigabyte).

Then for the kernel we need the same amount. And then for some nice laplacian filtering or some other additional goodies, we need a third transform.

So in total we're talking at least 3 gigabytes for an average deblurring action.

Now, I was able to cut that down to 1.5GB by changing the doubles to floats (going from 16 bytes per number to 8 bytes per number), but that still wasn't enough.

My 64-bit Windows system with 4GB of memory simply couldn't handle it. Small photos (as shown in the example) were fine. The bigger ones crashed the software.

Well, to cut a long and boring story short, I solved it with memory mapped files. Essentially cache writing memory to disk. It does slow down a bit, but it works. All the huge DNGs now deblur without memory problems, at least on my system :-)

This whole thing isn't finished yet, there's more experimenting, but I will try to keep you posted and show you some more examples when things progress...

No comments:

Post a Comment