Group Abstract Group Abstract

Message Boards Message Boards

7
|
30.9K Views
|
18 Replies
|
30 Total Likes
View groups...
Share
Share this post:

Easy Focus Stacking with Mathematica: Live from M&M 2013

Posted 13 years ago
My colleagues from Wolfram Research and I are currently attending Microscopy and Microanalysis 2013 conference in Indianapolis. It is really fun to mingle and chat with microscopy crowd spread among huge microscopes and monitors flashing myriads of invisible worlds blown up 1000s times. There is a lot of new technology talk & show going around and our image processing functionality fits right in. I just wanted to give you one example of that.

A few times our Wolfram Booth was approached by folks wondering how easy would it be to do Focus Stacking in Mathematica. For a general reference:
Focus Stacking (aka focal plane merging or z-stacking) is a digital image processing technique, which combines multiple images taken at different focus distances to give a resulting image with a greater depth of field than any of the individual source images. Focus stacking can be used in any situation where individual images have a very shallow depth of field; macro photography and optical microscopy are two typical examples. ~ Wikipedia
Our manager of Image Processing Team, Shadi Ashnai, is right here with us at the conference and she worked out an example of focus stacking in a matter of minutes. She kindly agreed to explain it in detail below.
POSTED BY: Vitaliy Kaurov
18 Replies
POSTED BY: Marcelo De Cicco
POSTED BY: Tom Sherlock
POSTED BY: Marcelo De Cicco
POSTED BY: Tom Sherlock
Attachments:
POSTED BY: Marcelo De Cicco
Attachments:
POSTED BY: Tom Sherlock
POSTED BY: Marcelo De Cicco
POSTED BY: Vitaliy Kaurov
POSTED BY: Marcelo De Cicco
POSTED BY: Shadi Ashnai
POSTED BY: Marcelo De Cicco
Seth, I am lucky enough to understand your programing metaphors because I speak (code?) the same language ;-) Actually it is very easy to apply Shadi's code for a larger set of images. The procedure above treated images as independent. So if you apply it as it is to a larger set - it will just work. 
POSTED BY: Vitaliy Kaurov
Tom,

I will look forward to what you can do and wouid be very happy to be a tester for anything you create.   I have quite a few sets of images that I did not get the other software to process well and can test with.   I do both tracked and untracked imaging with camera and telescopes.   I have only been at it for nine months and have wanted to use Mathematica for the process.   My guess is that are a lot of astrophotography folks who would be interested.   I give talks at the local astronomy clubs periodically and I think they would be very interested in a presentation on this at some point.
Steven,

I'm working on a blog post to describe this process.  It works similarly to Deep Sky Stacker and Registax, but it does the sub-frame alignments automatically. Right now I'm using it to reduce noise in my images since I can't get my scope to track accurately without guiding for more than about 1 minute for each sub-frame, rather than using it to combine parts from images.  However, Shadi's technique might be useful to build images of galaxies because their dynamic range is so great.
POSTED BY: Tom Sherlock
Like!  This is focus stacking for two images.  Can anyone do it for n_/;And[n>2,IntegerQ]
POSTED BY: Seth Chandler
Steven, both astro stacking and post processing of stacked images in Mathematica are doable and easy. We once prototyped such a program in Mathematica with an astronomer colleague, Tom Sherlock, who is also the author of this blog post: "Fixing Bad Astrophotography Using Mathematica 8 and Advanced Image Deconvolution"

A quick list of steps to take for stacking (as far as I remember) were:
1. increase brightness of images (ImageAdd or ImageAdjust)
2. align images (ImageAlign)
3. reverse step 1 (ImageSubtract or ImageAdjust)
4. add images together (Fold of ImageAdd)

If needed, I can provide you the exact steps once I am back to the office from the M&M conference.

Also, for post processing of images, please look at some demonstrations or documentation examples like:
- Adjusting RGB Color of Image
- Image Deconvolution
POSTED BY: Shadi Ashnai
This is great.  Would it be possible to devise a stacking system for astrophotography in Mathematica similar to or better than the DeepSkyStacker or Registax programs?
Let’s import an image from the Wikipedia page about Focus Stacking. Note that the first and second image parts are out-of-focus: the head or tail of the fly. The goal is to get a result comparable to the 3rd image part below where all in-focus areas are merged in single crispy image.
i = Import["http://goo.gl/xMThiZ"]



Because it is one large image, we start by grabbing first 2 images to process further.
images = {i1, i22} = Most@Flatten@ImagePartition[i, {{Scaled[1/3]}, Scaled[1]}]



Next we need to align images perfectly so no artifacts will appear during merging
i2 = RemoveAlphaChannel@ImageAlign[i1, i22];
Now if we blur images and find difference with original we can find sharper domains
list = ImageAdjust@ImageDifference[Blur[#, 5], #] & /@ {i1, i2}


Having that we make masks covering those domains
masks = FillingTransform@Dilation[Binarize[#], 1] & /@ list



And use those masks to set an alpha (transparency) channel in the images
list2 = MapThread[SetAlphaChannel, {{i1, i2}, masks}]



And now we can compose these with original to get complete-in-focus image. And after slight sharpening, voila, as crispy as we can get it. Note how in this image both head and tail of the fly are sharp.
ImageCompose[i1, ImageCompose @@ list2] // Sharpen

POSTED BY: Shadi Ashnai
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard