Wow, I often see microscope close-up images on my timeline with amazing sharpness. How is this possible? If I want to do that with my microscope, I only get a limited depth of field.
The trick is in the Extended Depth-Of-Focus or EDOF technology. You can achieve this with advanced lenses or software. In this article we will dive into this technology and try to write a software program for it ourselves.
Microscopes often have a small focus, so only part of the magnification is clearly visible, the rest is out of focus. With EDOF, advanced lenses or software with a special algorithm expand the focal area, giving you a full depth range.
Focus stacking is a technique used to achieve EDOF. With this method we create a short video with different focus distances. The algorithm analyzes each frame, identifies the sharpest areas and combines them into a composite image with greater depth of field.
We compare the contrast per pixel with the 24 surrounding pixels. A high contrast difference can be a sharp transition or edge. This information is essential when selecting which areas to include in the final composite image.
Looking in more detail at how EDOF can be achieved using C language code, we use the focus stacking method to merge video frames from a microscope slide taken at different focus distances to create an EDOF image. The code calculates the contrast of each pixel by analyzing the intensity variations between the central pixel and the twenty-four surrounding pixels. It then chooses the highest contrast image for each pixel and combines them to form the final composite image.
Our own GitHub contains sample code, which we went through in the previous steps. This is a plugin, AV-Filter-EDOF (1), for the AV-Viewer (2). In addition to the viewer, the AV-Filter-Player (3) is also required to open videos.
Although this software technique can produce beautiful photos, it is not suitable for every situation. For example, a slight glare can ruin the image. The software could also be further developed by applying interpolation for the intermediate pixels. If you want this, you are free to customize the software to your liking.
Extended depth of field technology expands microscope capabilities. Using focus stacking and contrast metering techniques we can create beautiful images with sharp focus and details with greater depth of field. So consider EDOF to take clear, detailed photos for your next research.