Is "Focus Stacking" possible with IM ?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Werty
Posts: 68
Joined: 2010-08-06T05:37:36-07:00
Authentication code: 8675308

Is "Focus Stacking" possible with IM ?

Post by Werty » 2014-05-19T04:08:46-07:00

I was wondering if Focus Stacking is possible with IM ?

I'm aware of all the free/commercial products like CombineZ, but I'm looking for something I can script myself, and to avoid including too much third party stuff in my application.

I'm on Win7 32bit so cant use Hadley's IM/CombineZ combo as it requires 64bit Windows, I've also been looking at Enfuse/EnBlend, but again, would be awesome if something similar could be done with IM.
Windows 7 user

snibgo
Posts: 12292
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Is "Focus Stacking" possible with IM ?

Post by snibgo » 2014-05-19T06:51:54-07:00

If the Hugin tools can do it, that's probably the best solution. Those tools are awesome interactively, and nearly awesome in batch.

IM has lower-level tools, so scripts would have to be built. If the images are aligned, I guess these would be fairly simple, using masks that identify which images have the best focus, along the lines of http://im.snibgo.com/zingph.htm#lbdm
snibgo's IM pages: im.snibgo.com

User avatar
whugemann
Posts: 294
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Is "Focus Stacking" possible with IM ?

Post by whugemann » 2014-06-18T23:56:30-07:00

I am using CombineZP under Win 7 32 bit without problems, so there still must be 32 bit versions around.

I cannot see how the trick could be performed with IM, even by use of a complex script. You would have to identify the (non-rectangular) regions in which some photo has the sharpest focus and then mount all these regions, found in various photos, into a new version of the entire photo.

Even in a dedicated program, this seems to be a tricky job, as most programs don't do a convincing job on that, with CombineZP being one of the exeptions!
Wolfgang Hugemann

snibgo
Posts: 12292
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Is "Focus Stacking" possible with IM ?

Post by snibgo » 2014-06-19T00:12:54-07:00

I don't think it would be difficult with IM. Yes, I know the proof is in the pudding and I haven't cooked it yet, but it's on my list.

My thoughts are: start with two images of the same size.[*] Can we decide, for each coordinate, which image is sharper? Yes. There are various measures, such as the standard deviation of an n*n square around the pixel. This easily gives us an image that is black where image #1 is best, or white where image #2 is best. Use that black/white image as a mask for "-compose Over" of image #1 and image #2.

The concept is easily extended to an arbitrary number of input images. In theory.

[*]Yes, we already have a complication. When a camera focuses, the lens moves in and out, changing the angle of view so images at different focuses need resizing. That's probably the harder problem to crack. A trial-and-error approach might work well.
snibgo's IM pages: im.snibgo.com

User avatar
whugemann
Posts: 294
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Is "Focus Stacking" possible with IM ?

Post by whugemann » 2014-06-19T00:25:41-07:00

I don't think that CombineZP scales the photos before mounting them together. They are just aligned in the first step.

Ideas for coding can possibly be taken from Fiji/ImageJ, which also provides a Java module for focus stacking and is Open Source.

Your basic idea – to calculate the sharpness in the neighbourhood of each pixel an then just take the pixel with the highest value - sounds convincing. Could possibly be performed with fx:, which however lacks a genuine max operator, working on an array (and not just two values).
Wolfgang Hugemann

snibgo
Posts: 12292
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Is "Focus Stacking" possible with IM ?

Post by snibgo » 2014-06-19T02:22:10-07:00

My theories often sound convincing until they fall apart. Nevertheless, the camera is charged up, and I'm just waiting for a bit of sun on a handy brick wall.

I avoid fx for 35 Mpix images. Life is too short. "-compose MinusSrc -composite -fill White +opaque Black" is a useful "less than" operation.
snibgo's IM pages: im.snibgo.com

Post Reply