Focus Stacking in Matlab
3-color fluorescence images of pollen grains, before and after focus stacking

Focus Stacking in Matlab

In this article we demonstrate using Matlab to do focus stacking (in 3 colors) using this script, fstack(), which produces a single image from a z-stack consisting of the sharpest pixels using a simple algorithm. 

acquireFocusStack() takes a sequence of pictures and keeps a subset that differ by a simple focus metric that it computes.  The user has to start out of focus, and slowly turn through the focal planes, once for each color.  

The fstack function was obtained from the Mathworks website. It is user contributed code, to which, after some simplification, we added a separate returned matrix called index.  Index tells from which image in the stack each pixel of the final image comes from. One could use index to generate a 3-d representation. 

Although the function is not well documented, it shows the extremely expressive power of this language -- it focus stacks in ~25 lines of code.

How?  (warning, extreme nerd content)

focusFilter is by default a 9x9 square matrix of value 1/81. gfocus convolves the images with this matrix, replacing each pixel with the average of the pixels in its 9x9 pixel neighborhood -- in other words it linearly blurs the image. It then subtracts the image from the blurred image, squares the difference (so that should just leave the high spatial frequencies), and then blurs by averaging that over a 9x9 neighborhood. That's done for each of the P images in the image stack. 

"Fusing images" begins by finding for each pixel which image in the stack has the highest value of the blurred high frequencies solved earlier. Now we build a synthetic image using these maximum pixels pulled from the original images.  We loop over each image.  Instead of indexing each pixel by an (x,y) pair, we reshape the image into a long column vector, and synthesize an image using the j'th pixels from the k'th image. Finally we reshape this vector into the final synthetic focus stacked image. 

 This work was done by Dr. Brian Rasnow with a manually focused Etaluma LS600 using a 20x Olympus objective looking at pine pollen and moss samples.

Rohan Gupta

M.D./M.Eng. Candidate at EnMed

3 年

Thanks for the simple, well-explained article. Would you say this method compromises or at least changes the RAW data/image in any sort of way? I want to incorporate focus stacking into a lot of image analysis that my lab does, but we are worried that incredibly small structures (maybe 10-20 pixels wide) may be altered due to convolution. However, my knowledge in this area is very very limited, do you think that this is a legitimate concern? The images are gray scale and we are looking at cells. What about with bigger and even smaller structures (~5 pixels wide cells)?

回复

Hi Bradley, I don't understand what you mean by difference in focal area. The focus stacking algorithm that I used came from an old fstack.m file from fileexchange, but I radically simplified it to do the following. For each pixel, compute the magnitude of the gradient as a measure of local sharpness. Then do winner-take-all, replacing each pixel with the sharpest one of that pixel in the stack. The code is at: https://weaklyelectricfishcsuci.pbworks.com/w/file/140627229/LSGUIdefaultCamera%203jul20.zip (click on the download tab), example from a deliberately tilted planar slide is here: https://weaklyelectricfishcsuci.pbworks.com/w/page/139703301/lsgui%20Applications#Focusstacking. Hope that helps! -- Brian

回复

I must say very interesting, I have been looking at Matlab Script (assume what your document describes) https://www.mathworks.com/matlabcentral/fileexchange/58662-creating-extended-depth-of-field-image-through-focus-stacking)... At moment script does not appear to take into account that difference if focal area, as we have a series of images that each focus has a slight change in focal area. Assume slightly stretching each image to have same focal area could be a slight improvement ..

回复

要查看或添加评论,请登录

Chris Shumate的更多文章

  • New AI Tool for Image Analysis

    New AI Tool for Image Analysis

    When Silicon Valley VC firm Andreesen Horowitz funded a couple of Stanford students in the cell analysis space I took…

    6 条评论
  • The Popular Science of Stem Cells

    The Popular Science of Stem Cells

    Niles Interview Part 2 Earlier this year we published the first half of an interview with my longtime friend…

    3 条评论
  • The most compact 3-color fluorescence microscope in the world!

    The most compact 3-color fluorescence microscope in the world!

    Etaluma's patented microscope module features: Solid-state channel selection, no filter wheels, no pixel shift Triple…

    2 条评论
  • Top 3 Customer Misconceptions

    Top 3 Customer Misconceptions

    #1 CCDs are better than CMOS camera sensors. Well, Sony doesn’t think so… “Sony Corporation is going to stop its CCD…

    1 条评论
  • Choose Etaluma’s OEM Microscopy Modules for Your Development Project

    Choose Etaluma’s OEM Microscopy Modules for Your Development Project

    When designing products that include microscopy or cell imaging functions, often one must decide what to buy and what…

    2 条评论
  • Stitching Images from Your Manual Microscope

    Stitching Images from Your Manual Microscope

    Introduction Tiling microscope images allows one to stitch together a larger montage image with the resolution of the…

    3 条评论
  • Disinfecting Etaluma Microscopes before Placement into an Incubator

    Disinfecting Etaluma Microscopes before Placement into an Incubator

    Etaluma’s microscopes are revolutionary in their ability to be operated within cell culture incubators, providing the…

    1 条评论
  • Live Cell Imaging

    Live Cell Imaging

    Live cell imaging began with the invention of the inverted microscope, which allowed cells growing under media to be…

社区洞察

其他会员也浏览了