Focus-stacked image composites

I’m exploring some of the applications of my new OpenFlexure microscope, and I’ve gotten focus-stacking images with different z-heights to work. Here’s what I have so far! These are very much starting points, and I’m probably missing some optimization steps along the way. But hey, not bad for 30 seconds of captures and 20 seconds of processing time on a modern budget laptop!

Results!

I took 40 z-stacked images of my sample, a cross section of a pine stem. Here are a couple of the unmodified images, one from the beginning, middle-ish, and end of the z range:

You can sort of see where different parts of the image are in focus for each of the above frames. One has the dark-green edge of the sample in focus, one has the finer cells on the top of the image, etc.

Here’s my composite, focus-stacked image:

Discussion

Way cool! I’ve got the whole thing in much better focus overall, and I can see lots of it at once! I notice that near the edges of the image there’s some blurring, and I suspect this is due to the spherical aberration of my objective lens. When I looked at all of my original images I noticed the edges of the image distorting a bit, and I don’t think my focus stacking software is compensating for this. As far as I know, the software is just doing some nifty Laplace transforms to figure out which parts of each frame are in-focus vs out of focus based on (assumed) Gaussian distribution blurring, and not compensating for non-ideal optics. I suppose one could fix this with fancier math, but I’m just going to get a planar 40x objective lens and try that instead. More to follow on that!

I should explicitly credit the open-source github repo focus-stack for having very easy-to-use software that made doing this much easier than I expected. Kudos!

Hardware

  • OFM microscope, bare-bones with the “low cost” optics variant (re-positioned raspi camera 2 lens)
  • Prepared slide of pine stem cross section

Microscope Settings

I used the “Stack and Scan” feature to capture 40 full-resolution images 50 z-steps apart. Your milage may vary with the z-step size, depending on your objective lens setup and how much Z range you want capture from your specimen.

The time to take all of my Z captures was about 30 seconds, but would be faster if I hadn’t turned up my settle time from the default.

Compute

  • $500 2023 laptop running Windows 11 (nothing special, I don’t think I even used GPU acceleration).
  • WSL2 running Ubuntu 24.04.1 LTS

Software

Speed bumps

The released binaries for focus-stack are compatible with Ubuntu 20.04, but not compatible with my 24.* version. I had to build the binaries from source to run on Linux. If you’re running Windows, I suspect the released Windows binaries will do what you want. Luckily this wasn’t too hard and didn’t require any weird WSL compatibility tricks.

5 Likes

Awesome! Thanks for the link to focus-stack it looks like a really useful tool, perhaps something that we could incorporate into the software as it is Python.

@JohemianKnapsody Have you played around with focus-stack or similar tools in your z-stacking adventures?

Focus-stack isn’t Python, it’s a compiled C++ executable. I’m not sure what the OpenFlexure image release process looks like, but focus-stack could be compiled appropriately and included on the raspi image as an executable and called by Python with os.exec or something, though this might leave some security concerns.

I’ve been poking around other microscope forums and discovered that there’s an awesome tool for image analysis, Fiji, which packages Imagej2. One of the available plugins is Micro-Manager, an open-source tool for microscope control. There might already be an OpenFlexure device implemented in Micro-Manager, but it’s unclear whether it works or not. Maybe I’ll try it and see. @Nico had a thread with some blood slide scans from late last year where he demonstrated effective use of some Fiji plugins, but it looks like he used the openflexure python library rather than Fiji or the web UI.

Yes it is… Who knows why I thought it was Python, I looked at the repo, closed it and then somehow remembered it incorrectly as Python. But you are correct we could compile it as an exe or compile it with python bindings, but this is a bit more effort.

As for ImageJ and Fiji, I believe these should be available using the ImJoy plugin in the software. I don’t know how stable this is.

This looks great! I’ve played with a focus stacking library before, in Python if memory serves. At the time I was thinking of using it for 100x malaria diagnosis, but we decided to not add another potential point of failure. It’s definitely something I can add back in as an option for the non-medical version of v3

1 Like

Hey, that’s cool, you serve up ImageJ in the browser window and everything!
Next step, image stitching with the imageJ plugin.

2 Likes

I haven’t been using the openflexure hardware, but have been stacking/stitching large datasets on-and-off. Happy to recommend enfuse for stacking, it’s pretty much automatic with the right parameters - and hugin or ptgui for stitching.

Some results here: https://setzkasten.relet.net/

I have also been experimenting with generating a live preview of the stacked image, but I’m not using it much: GitHub - relet/rolling-focus: live preview focus stacking for microscopy and android (see enfuse/auto.sh for some parameters to enfuse, cannot post too many links yet)

1 Like