Microscopy focus stacking

I have a simple setup using a nikon d5200 to take photomicrographs. The microscope is an old (1942) Spencer microscope that I absolutely love. I can take good quality photos using a T-adapter.

With Darktable I can get a good live view and optimise shots with both reflected and transmitted light, also dark ground.

I want to try my hand at focus stacking. I have no problem using ESP32/Raspbery PI, etc., and programming them. Adding a stepper motor control to either coarse or fine focus controls is not a problem. My preferred programming language is C++.

What Iā€™d like to do is use live view to define the upper and lower limits of focus, choose the number of steps and automate the increments, taking a shot at each step.

Hereā€™s where I could use some help, first to see whatā€™s possible/not possible. I see that Darktable has a LUA interface. Although I am unfamiliar with LUA, learning it wonā€™t be impossible. I can build an interface from, say, a microcontroller to the PC (mine is Linux/Fedora) with the former controlling a stepper motor to move the focus. Images could be stored either on the SD or on the PC but either way, the next shot cannot be taken until the image is stored and the buffer released. The best (quickest) way is some sort of feeedback of the image processing/saving cycle to prompt the focus move and next shot; the alternative is a delay which is guaranteed to be long enough to allow the save comfortably to have occurred.

Some initial questions are therefore (relating specifically to the implementation in Darktable):

  1. can LUA detect when a shot has been taken and saved
  2. can LUA interface with computer ports to trigger the next event

Itā€™s an interesting project. Maybe a lot of experimentation and a lot of dead ends. But all hardware/software design is like that and thatā€™s all part of the challenge.

Thanks,
Ric

PS: the ability of change focusing in the lens does not apply - the lens is removed and the microscope does the imaging.

1 Like

Afaik, tethering is not the strongest point of darktable, and youā€™d need it to have darktable control the image collection. LUA interacts with darktable, which then interacts with gphoto2 for tethering.

So the easiest way might be to have the acquisition completely separate from the (darktable) treatment. That would also remove the need for an interface between the controller and a PC (I donā€™t have the microscope next to the PCā€¦)

Lua may help in the stacking treatment, which will be done by an external program. But I find it just as easy to export the images and then use the external program directly.

If, for the stepper motor, you are talking about the coarse focusing on the microscope, not sure that oneā€™s going to be useful (depending on the magnification of course). Weā€™re talking sub-Āµm level here, good preparations are usually less than 10 Āµm (sometimes a lot less).

Iā€™d expect the distance between two ā€˜slicesā€™ to be fairly constant for a given magnification. So the number of slices would depend only on start and end positions.

Also (at least with ā€œmodernā€ lighting) light intensity isnā€™t too much of an issue (expositions of 1/20 -1/100 at 100ISO are possible. So a fixed delay of about 0.5s would be my choice (and of course a delay between focus change and shooting to let any vibrations dissipate). (thatā€™s for standard light field microscopy, if you do darkfield or polarisation, you have less light availableā€¦)

But, have you already tried a stack ā€œby handā€? If your microscope looks like this one, you may run into issues with the connection between camera and optics when changing the focus ā€¦

(In most modern microscopes, the table is moved to focus, not the optical system, so there are no issues with the optics moving relative to the camera)

Yes

Lua can interface with the operating system and run other programs, so I think this is probably a yes.

1 Like

I can offer no advice to your project, but I offer encouragement for you or anyone else to bring focus stacking capabilities to DT. That would be amazing if it could be done.

1 Like

Hello and thank you for your various replies.

ā€œyou may run into issues with the connection between camera and optics when changing the focusā€

Yes, this is a real problem. I have refurbished a number of microscopes and one of the most common issues is the hardening of the grease used in rack-and-pinion gears. I have used a ā€˜helicoidā€™ type grease (similar greases are used in the moving parts of inkjet printers) but choose the wrong one and the coarse focus simply slips and gravity takes over dragging the optics down. Getting the right viscosity grease was a lot of trial and error. The issue is that the whole weight of the camera is taken by those same gears - a burden for which they were never designed. But, if it does prove intractable, swapping for a moving stage focusing microscope is always possible. For single shots focus slippage simply does not occur - the juryā€™s out on whether that might be still the case over the course of dozens of shots.

My instinct was always with adjusting the fine focus. The scale is calibrated in Ī¼m. A stepper motor may have 200 steps per revolution, further gearing can theoretically give sub-micron movement - but at what point will lack of stiffness, and meshing issues affect reproducibility? That remains to be seen. I suppose hysteresis could be reduced by always starting at the lowest position and moving upwards, i.e., against gravity, thus providing a constant opposing force against the natural torque of the stepper motor.

If anything Iā€™m more worried about the hardware interfacing issues. I suppose thereā€™s always DigicamControl - which i have used though that would transfer a lot of processing to a Windows PC. The latest version dates back to 2023.

But focus stacking using a microscope is a very different animal from using a controller to adjust focusing in-camera (i.e., with an attached macro lens): thereā€™s still that interface to control. The hard decision is where to invest time: hardware/software projects always consume more time than anticipated and Iā€™d really like to direct my energies more towards photomicrography.

Ric