MTF.py
Modulation Transfer Function Calculation for Slanted Edge Images
About
The optical transfer function (OTF) of an optical system specifies how different spatial frequencies are handled by the system. It is used by optical engineers to describe how the optics project light from the object or scene onto the detector or simply the next item in the optical transmission chain. A variant, the modulation transfer function (MTF), neglects phase effects, but is equivalent to the OTF in many situations.
In normal operation, MTF is the Fourier transform of the Point Spread Function (PSF). But for slanted edge-targeted MTF calculations, we use Line Spread Function (LSF) instead of PSF. This is because we assume LSF as a cross-section of PSF along the slant. LSF can be obtained using the derivative of Edge Spread Function (ESF).
Since LSF is the cross-section of PSF along the slant edge, a better approach will be calculating MTF for both horizontally and vertically tilted images.
For line-scan operations, it is better to calculate along the track and across the track MTF's.
Also for non-symmetrical pixel pitch/sizes sensors, it will also be convenient to calculate horizontal and vertical slants.
Note that the preferred slant tilt is between 2 degrees and 10 degrees.
Usage (normalized frequency)
import mtf as mtf
imgArr = mtf.Helper.LoadImageAsArray('slant.png')
res = mtf.MTF.CalculateMtf(imgArr, verbose=mtf.Verbosity.DETAIL)
Usage (spatial frequency in lp/mm)
Pass the pixel pitch (pixel size) in millimeters using pixel_size_mm:
import mtf as mtf
imgArr = mtf.Helper.LoadImageAsArray('slant.png')
# Example: a 3.45 µm pixel -> 0.00345 mm
res = mtf.MTF.CalculateMtf(imgArr, pixel_size_mm=0.00345, verbose=mtf.Verbosity.DETAIL)
When pixel_size_mm is provided and > 0, the MTF plot x-axis is converted to spatial frequency (line pairs per millimeter), and the plot title also shows the Nyquist frequency in lp/mm.
Dependencies
This module requires the following external modules Pillow, numpy, scipy, matplotlib, opencv-python
Installation
On Windows (cmd.exe):
- Create and activate a virtual environment (optional but recommended)
python -m venv .venv
.venv\Scripts\activate
- Install dependencies
pip install -r requirements.txt
- Run
python main.py
If you encounter backend issues for GUI windows or plotting, ensure your Python installation includes Tkinter.
License
Distributed under the MIT License. See LICENSE.txt for more information.
Contact
Ufuk Onder - ufuk.onder@gmail.com
Project Link: (https://github.com/u-onder/mtf.py)