Depth from Focus or Shape from Focus is a method of reconstructing a 3D Object Depth Map and it's corresponding All-Focused image using focus as a que for depth. This is efficient and isn't as costly as a stereo camera.
According to the physics of lenses, blur circles are formed when an object is not located at the working distance (plane of focus). If we see an image that is perfectly in focus throughout, then it is saying that the object is in a frontoparallel planar world. If only some parts are in focus then it is hinting that the object has a 3D structure!
To make use of this effectively, we keep the Depth of Field as small as possible and take a stack of images by changing the focus each time. This is then processesed pixel-by-pixel by a focus operator to find the depth at which it is most likely in focus.
First Image in Focus Stack | Last Image in Focus Stack | Reconstructed All-Focus Image |
---|---|---|
2D Depth Plot | 3D Depth Plot |
---|---|
First Image in Focus Stack | Last Image in Focus Stack | Reconstructed All-Focus Image |
---|---|---|
2D Depth Plot | 3D Depth Plot |
---|---|
- This project assumes that the images do not undergo axial parallax (or is corrected) when changing focus. This is always true if we use a telecentric lens else it has to be preprocessed for alignment.
- Apply Focus Operator, Sum Modified Laplacian (SML) with kernel size 3.
- Gaussian Interpolation to find the best depth, to counter discretization.
- Use the generated depth map to reconstruct an All-Focused image.
- Apply Median Filter to cleanup some noise.
- Generate a 2D Grayscaled Depth Plot.
- Generate a 3D Depth Surface Plot.
- Use
python3 dff.py
to run the script.
The dataset folder contains a compilation of various aligned focus stack I could find online and on GitHub. I hold no copyright for any of the images in the dataset. This is for learning and education purposes only.
- Use an Optical Flow / SIFT algorithms to correct axial parallax.
- Use Graph Cut and segmentation approaches to fine-tune the depth map.