Focus Stacking Module

Focus stacking (also known as focal plane merging and z-stacking or focus blending) 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 (DOF) than any of the individual source images (wikipedia).

Interactive demo

With (left) and without focus stacking (right)


The algorithm used is A Multi-focus Image Fusion Method Based on Laplacian Pyramid (Chang and Wang 2011).

It uses Laplacian pyramids and is the best focus stacking method I know. It’s also implemented in Helicon Focus and other professional focus stacking softwares.

It was kinda hard to implement, but fortunately OpenCV allows us to generate Laplacian pyramids easily.


Now we can use focus stacking directly from the software.

I added a simple photo mode command (Ctrl + Space) to create screenshots using focus stacking in 5-6 seconds.

Then I added a new focus module, and I linked it to the scanline capture system.

First, I generated a panorama of a TA8493 brushless DC motor driver:

  • die of 3.3 x 2.6 mm (8.6 mm²)
  • 30 steps separation
  • 45x35 tiles (1575 tiles)
  • 2h50 capture

The result is impressive, the details are sharp and everything is in perfect focus.

Only problem, 3h for a small die is quite a bit of time…

The point is, my tiles are heavily cropped because my lens are on focus only on a small area, so I have to use a small step between each tile. Except that now the complete image is on focus, so I can try to capture using a larger step.

Second test. Panorama of another brushless DC motor driver:

  • die of 4.6 x 3.6 mm (16.6 mm²)
  • 60 steps separation
  • 31x24 tiles (744 tiles)
  • 1h30 capture

Surprisingly efficient. Despite the 2x larger surface, it took 2x less time with the same quality. The capture time is comparable to capture using the chromatic aberration correction module.


The focus stacking module generate sharp images, allows us to use a larger capture surface and thus double the steps between each tile to get a capture time comparable to using the chromatic aberration correction module, but with a higher image quality.