Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Travail seif #102

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion lensless/hardware/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def phase_retrieval(target_psf, wv, d1, dz, n=1.2, n_iter=10, height_map=False):
Target PSF to optimize the phase mask for.
wv: float
Wavelength (m).
d1: float
d1: float=
Sample period on the sensor i.e. pixel size (m).
dz: float
Propagation distance between the mask and the sensor.
Expand Down Expand Up @@ -470,3 +470,70 @@ def create_mask(self):
radius_px = self.radius / self.feature_size[0]
mask = 0.5 * (1 + np.cos(np.pi * (x**2 + y**2) / radius_px**2))
self.mask = np.round(mask)


class HeightVarying(Mask):
"""
A class representing a height-varying mask for lensless imaging.

Parameters
----------
refractive_index : float, optional
The refractive index of the material. Default is 1.2.
wavelength : float, optional
The wavelength of the light. Default is 532e-9.
height_map : ndarray or None, optional
An array representing the height map of the mask. If None, a random height map is generated.
height_range : tuple, optional
A tuple (min, max) specifying the range of heights when generating a random height map.
Default is (min, max), where min and max are placeholders for the actual values.
seed : int, optional
Seed for the random number generator when generating a random height map. Default is 0.

Example
-------
Creating an instance with a custom height map:

>>> custom_height_map = np.array([0.1, 0.2, 0.3])
>>> height_varying_instance = HeightVarying(
... refractive_index=1.2,
... wavelength=532e-9,
... height_map=custom_height_map,
... height_range=(0.0, 1.0),
... seed=42
... )
"""
def __init__(
self,
refractive_index = 1.2,
wavelength = 532e-9,
height_map = None,
height_range = (1e-3, 1e-2),
seed = 0,
**kwargs):


self.refractive_index = refractive_index
self.wavelength = wavelength
self.height_range = height_range
self.seed = seed

if height_map is not None:
self.height_map = height_map
else:
self.height_map = None
np.random.seed(self.seed)

super().__init__(**kwargs)

def get_phi(self):
phi = self.height_map * (2*np.pi*(self.refractive_index-1) / self.wavelength)
phi = phi % (2*np.pi)
return phi

def create_mask(self):
if self.height_map is None:
self.height_map = np.random.uniform(self.height_range[0], self.height_range[1], self.resolution)
assert self.height_map.shape == tuple(self.resolution)
phase_mask = self.get_phi()
self.mask = np.exp(1j * phase_mask)
14 changes: 13 additions & 1 deletion test/test_masks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import numpy as np
from lensless.hardware.mask import CodedAperture, PhaseContour, FresnelZoneAperture
from lensless.hardware.mask import CodedAperture, PhaseContour, FresnelZoneAperture, HeightVarying
from lensless.eval.metric import mse, psnr, ssim
from waveprop.fresnel import fresnel_conv
from matplotlib import pyplot as plt


resolution = np.array([380, 507])
Expand Down Expand Up @@ -90,6 +91,17 @@ def test_classmethod():
desired_psf_shape = np.array(tuple(resolution) + (len(mask3.psf_wavelength),))
assert np.all(mask3.psf.shape == desired_psf_shape)

mask5 = HeightVarying.from_sensor(
sensor_name="rpi_hq", downsample=downsample, distance_sensor=dz
)
assert np.all(mask5.mask.shape == resolution)
desired_psf_shape = np.array(tuple(resolution) + (len(mask5.psf_wavelength),))
assert np.all(mask5.psf.shape == desired_psf_shape)
fig, ax = plt.subplots()
im = ax.imshow(np.angle(mask5.mask), cmap="gray")
fig.colorbar(im, ax=ax, shrink=0.5, aspect=5)
plt.show()


if __name__ == "__main__":
test_flatcam()
Expand Down
Loading