Select in-focus image from z-stack

Hi All,

I’m working on some time lapse microscopy, and unfortunately my microscope suffers from drift that I can’t fix. I’m collecting z-stack images so that I have at least one image in the correct focal plane.

As my cells are moving on a bed of micropillars, it’s simple to choose the correct image as the StDev is high, as is the max. value.

Can someone point me in the right direction of how I might go about analysing a stack to select out the image with the highest StDev? Ultimately, I’d like to be able to create a 4D stack, and work through it automatically to choose the in-focus image for each time point?

Image 10 in the montage above is in focus for my application (I’m tracking the pillar movements)

Cheers,
Paul

1 Like

Dear Paul,

I guess you need a macro that loops through the stack to determine the best focus.

I just analyzed your sample stack using two approaches (none of them using STD) and both result in slice #15. (The analyses come from a centered square of side length 32 pel.)

Some of the slices show heavy artefacts. Depending on the method, they may strongly influence the analyses.

Best

Herbie

Hi Herbie,

Thanks for the reply. I’m not a regular writer of code, so it’s really useful when someone can give me a gentle push in the right direction.

I’ve now cobbled together something which selects the image with the highest STD from a stack, and I’ve tried it out on ~10 of my z-stacks and I’m happy that it’s accurate enough for me at the moment. I suppose that as I progress with this project I can consider using more detailed ways of analysing focus.

https://dl.dropboxusercontent.com/u/3404091/FindMaxSTD_.ijm

Now, I’ll try and make it work for a 4D stack

Thanks again
Paul

1 Like

Dear Paul,

nice to hear that your project advances.

But what about the artifacts. I’m sure they influence the results if you use the STD.

Best

Herbie

Hi Herbie,

Yes the artefacts are a problem. The larger black dots are collapsed micropillars, and there is a chance that the STD will be a poor metric as the z-stack passes through the cell body and there are some high contrast organelles.

Also, within a single image there are changes in focus. The fact that this appears circular suggests to me that my focal plane is not flat. I’m not using the best microscope or objective for this - but it’s the only timelapse capable one I have access to (it’s a 100x objective, the pillars are 500nm in diameter, 1000nm pitch).

This is the full resolution z-stack:
https://dl.dropboxusercontent.com/u/3404091/Z-Stack-Scene1Interval1.tif

My macro also works if I select an ROI, so my plan is to position this over the approximate area of interest in frame 1, then work through the hyperstack. Indeed, if I don’t do this, there are some frames where the wrong image is selected.

Cheers,
Paul

1 Like

Dear Paul,

great!
With the provided high-resolution stack I get consistenly frame #10 as the in focus frame (no STD analyses). I used a 512pel square-sized ROI around the cell. But I’m not sure whether the result is mainly due to the regular fine structure (raster) that is of highest contrast for the cell region in frame #10.

I don’t really understand the fine-strcuture (raster) of your images that has z-varying contrast that also varies over the image plane.

Best

Herbie

Hi Herbie,

The dot pattern you see in the images is actually an array of micropillars on which I’m growing the cells. Much like this paper:


(http://www.pnas.org/content/109/18/6933.abstract)

I don’t know why it varies over the image either. That’s a problem I need to fix! It may be an optical issue, or the surface may not be flat.

I now have a macro which allows me to select a folder with my z-stacks in subfolders, and an ROI location, then it will focus and register the full movie. I love it when a macro comes together!

Cheers,
Paul

2 Likes

Dear Paul,

thank you for clarifying the “raster” issue, for the images, and for the link.
Quite interesting!

In fact these micropillars will have a considerable impact on focus analyses.
I’m somehow clueless of how to obtain the focus layer of the cell alone.
(Is the cell itself thin or do you have to focus a certain layer of the cell?)

Be careful with what you get from the STD-evaluation if the micropillars show high contrast.

Best

Herbie

Hi Herbie,

I’m quite lucky in that I want to focus on the micropillars! I don’t really care about the cell, I’m more interested in the pillars and how they move underneath the cell :wink:

So at this point the STD is fine, but if I ever have an application where the cell is more interesting to me then I’ll have to figure out a better solution

Cheers,
PAul

1 Like

Hi Paul,

Sounds like you solved the problem for now, but in case…

There is an Autofocus Hyperstack macro – and a related Tiled Autofocus Hyperstack by Richard Mort – no experience with these, but they might be helpful. They use the “normalized variance” method.

For manual selection of slices, the “Hyper With Stacks” macro toolset by William Ashby has worked for me.

Best,
Theresa

3 Likes

Dear Theresa,

as you conjectured, the problem appears to be less in the focus detection per se, but in the question what determines the focus slice in the very case.

Paul used the STD and I used a custom approach not based on common measures. The latter is perhaps the best regarding the quality of the results and the computational speed. (The method is not generally available!)
Both report the same slice as being in focus.

So in fact it appears that the case is settled but of course I can’t speak for Paul.

Best

Herbie

Wonderful, thank you Teresa and Rich Mort!