Sterncat / opticspy

python optics module
MIT License
448 stars 126 forks source link

Imaging simulations #14

Open meller92 opened 7 years ago

meller92 commented 7 years ago

Hi, I don't know Python a lot but i think that adding something like this to the zernike.py module could be useful:

import opticspy
import numpy as np
from PIL import Image
from scipy import signal
import matplotlib.pyplot as plt

Z = opticspy.zernike.Coefficient(Z1=0.2)
Z.zernikesurface()
psfmatrix = Z.psf(matrix=True)
Z.mtf()
abspsf=abs(psfmatrix/psfmatrix.max())

im = Image.open("img.bmp")
pic = np.array(im)
imgplot=plt.imshow(pic)
imgplot.set_cmap("gray")
plt.colorbar()
plt.show()

abspic=(pic/pic.max())
outpic=signal.fftconvolve(abspsf,abspic,mode="same")
outpic=np.trunc(255*abs(outpic/outpic.max()))
imgplot=plt.imshow(outpic)
imgplot.set_cmap("gray")
plt.colorbar()
plt.show()
Sterncat commented 7 years ago

Hi meller,

Thank you!

I will test it and add it in the module! Cool!

Marvin

dguerra commented 7 years ago

Hi Marvin, any improvement on this? I think it could be a nice feature. Thanks

brandondube commented 6 years ago

Hi @dguerra, you may be interested in prysm, an alternative python optics module. Among its features are image simulation, with respect for units and a lack of necessity for the PSF and image to have equal spacing. I do need to polish the code for that type of thing more, but you can find an example of using image simulation here.

goldengrape commented 6 years ago

failed in psfmatrix = Z.psf(matrix=True) return TypeError: psf() got an unexpected keyword argument 'matrix'

I dive into the code, for .psf

def psf(self,r=1,lambda_1=632*10**(-9),z=0.1):
        """
        ------------------------------------------------
        psf()
        Return the point spread function of a wavefront described by
        Zernike Polynomials
        ------------------------------------------------
        Input:
        r: exit pupil radius(mm)
        lambda_1: wavelength(m)
        z: exit pupil to image plane distance(m)
        """
        print(r,lambda_1,z)
        PSF = self.__psfcaculator__(r=r,lambda_1=lambda_1,z=z)
        fig = __plt__.figure(figsize=(9, 6), dpi=80)
        __plt__.imshow(abs(PSF),cmap=__cm__.RdYlGn)
        __plt__.colorbar()
        __plt__.show()
        return 0

Z.psf() always return 0, not PSF from psfcaculator

Sterncat commented 6 years ago

Maybe one int 0 get into the calculation.