Search code examples
pythonmatlabimage-processing

Python equivalent of MATLAB otf2psf


Is there a library that does in Python what MATLAB's otf2psf function does?

Here is the documentation of the MATLAB function. I understand that this is done by a Fast Fourier Transform. I did find something that does psf2otf in here. But I could not find something that does it the other way around.


Solution

  • In Python, there's no direct equivalent of MATLAB's otf2psf function. You can achieve the same effect using the inverse Fourier transform (ifftn) and some additional processing.

    Here's a basic implementation in Python:

    import numpy as np
    from numpy.fft import ifftn, fftshift
    
    def otf2psf(otf, shape=None):
        if shape is None:
            shape = otf.shape
        
        # Inverse Fourier transform
        psf = ifftn(otf, shape)
        
        # Shift the zero-frequency component to the center
        psf = fftshift(psf)
        
        # Take the real part, as the PSF should be real-valued
        psf = np.real(psf)
        
        # Normalize the PSF so that its sum is 1
        psf /= np.sum(psf)
        
        return psf
    

    You can use this function to convert an OTF to a PSF in Python. The shape parameter allows you to specify the desired shape of the PSF.