Mask Design

This module provides utilities to create different types of masks (CodedAperture, PhaseContour, FresnelZoneAperture) and simulate the corresponding PSF.

Abstract Mask Class

class lensless.hardware.mask.Mask(resolution, distance_sensor=None, size=None, feature_size=None, psf_wavelength=[4.6e-07, 5.5e-07, 6.4e-07], use_torch=False, torch_device='cpu', centered=True, **kwargs)[source]

Parent Mask class. Attributes common to each type of mask.

__init__(resolution, distance_sensor=None, size=None, feature_size=None, psf_wavelength=[4.6e-07, 5.5e-07, 6.4e-07], use_torch=False, torch_device='cpu', centered=True, **kwargs)[source]

Constructor from parameters of the user’s choice.

Parameters
  • resolution (array_like) – Resolution of the mask (px).

  • distance_sensor (float) – Distance between the mask and the sensor (m). Needed to simulate PSF.

  • size (array_like) – Size of the mask (m). Only one of size or feature_size needs to be specified.

  • feature_size (float or array_like) – Size of the feature (m). Only one of size or feature_size needs to be specified.

  • psf_wavelength (list, optional) – List of wavelengths to simulate PSF (m). Default is [460e-9, 550e-9, 640e-9] nm (blue, green, red).

  • use_torch (bool, optional) – If True, the mask is created as a torch tensor. Default is False.

  • torch_device (str, optional) – Device to use for torch tensor. Default is ‘cpu’.

  • centered (bool, optional) – If True, the mask is centered. Default is True.

compute_psf(distance_sensor=None, wavelength=None, intensity=True)[source]

Compute the intensity PSF with bandlimited angular spectrum (BLAS) for each wavelength. Common to all types of masks.

Parameters
  • distance_sensor (float, optional) – Distance between mask and sensor (m). Default is the distance specified at initialization.

  • wavelength (float or array_like, optional) – Wavelength(s) to compute the PSF (m). Default is the list of wavelengths specified at initialization.

abstract create_mask()[source]

Abstract mask creation method that creates mask with subclass-specific function.

classmethod from_sensor(sensor_name, downsample=None, **kwargs)[source]

Constructor from an existing virtual sensor that copies over the sensor parameters (sensor resolution, sensor size, feature size).

Parameters
  • sensor_name (str) – Name of the sensor. See SensorOptions.

  • downsample (float, optional) – Downsampling factor.

  • **kwargs – Additional arguments for the mask constructor. See the abstract class Mask and the corresponding subclass for more details.

Example

mask = CodedAperture.from_sensor(sensor_name=SensorOptions.RPI_HQ, downsample=8, ...)
height_map_to_field(wavelength, return_phase=False)[source]

Compute phase from height map.

Parameters
  • height_map (ndarray) – Height map.

  • wavelength (float) – Wavelength of the light (m).

  • return_phase (bool, optional) – If True, return the phase instead of the field. Default is False.

plot(ax=None, **kwargs)[source]

Plot the mask.

Parameters
  • ax (Axes, optional) – Axes to plot the mask on. Default is None.

  • **kwargs – Additional arguments for the plot function.

Coded Aperture (FlatCam)

class lensless.hardware.mask.CodedAperture(method='MLS', n_bits=8, **kwargs)[source]

Coded aperture mask as in FlatCam.

__init__(method='MLS', n_bits=8, **kwargs)[source]

Coded aperture mask contructor (FlatCam).

Parameters
  • method (str) – Pattern generation method (MURA or MLS). Default is MLS.

  • n_bits (int, optional) – Number of bits for pattern generation, must be prime for MURA. Results in 2^n - 1``x``2^n - 1 for MLS. Default is 8 (for a 255x255 MLS mask).

  • **kwargs – The keyword arguments are passed to the parent class Mask.

create_mask(row=None, col=None, mask=None)[source]

Creating coded aperture mask.

generate_mura(p)[source]

Generate MURA square pattern.

Parameters

p (int) – Number of bits.

get_conv_matrices(img_shape)[source]

Get theoretical left and right convolution matrices for the separable mask.

Such that measurement model is given P @ img @ Q.T.

Parameters

img_shape (tuple) – Shape of the image to being convolved.

Returns

  • P (ndarray) – Left convolution matrix.

  • Q (ndarray) – Right convolution matrix.

is_prime(n)[source]

Assess whether a number is prime or not.

Parameters

n (int) – The number we want to check.

simulate(obj, snr_db=20)[source]

Simulate the mask measurement of an image. Apply left and right convolution matrices, add noise and return the measurement.

Parameters
  • obj (ndarray) – Image to simulate.

  • snr_db (float, optional) – Signal-to-noise ratio (dB) of the simulated measurement. Default is 20 dB.

Phase Contour (PhlatCam)

class lensless.hardware.mask.PhaseContour(noise_period=(16, 16), refractive_index=1.2, n_iter=10, design_wv=5.32e-07, **kwargs)[source]

Phase contour mask as in PhlatCam.

__init__(noise_period=(16, 16), refractive_index=1.2, n_iter=10, design_wv=5.32e-07, **kwargs)[source]

Phase contour mask contructor (PhlatCam).

Parameters
  • noise_period (array_like, optional) – Noise period of the Perlin noise (px). Default is (8, 8).

  • refractive_index (float, optional) – Refractive index of the mask substrate. Default is 1.2.

  • n_iter (int, optional) – Number of iterations for the phase retrieval algorithm. Default is 10.

  • design_wv (float, optional) – Wavelength used to design the mask (m). Default is 532e-9, as in the PhlatCam paper.

  • **kwargs – The keyword arguments are passed to the parent class Mask.

create_mask()[source]

Creating phase contour from edges of Perlin noise.

Fresnel Zone Aperture

class lensless.hardware.mask.FresnelZoneAperture(radius=0.00056, **kwargs)[source]

Fresnel Zone Aperture (FZA) mask as in this work, namely binarized cosine function.

__init__(radius=0.00056, **kwargs)[source]

Fresnel Zone Aperture mask contructor.

Parameters
  • radius (float) – Radius of the FZA (m). Default value is 0.56e-3 (largest in the paper, others are 0.32e-3 and 0.25e-3).

  • **kwargs – The keyword arguments are passed to the parent class Mask.

create_mask()[source]

Creating binary Fresnel Zone Aperture mask.

Trainable Mask

class lensless.hardware.trainable_mask.TrainableMask(optimizer='Adam', lr=0.001, **kwargs)[source]

Abstract class for defining trainable masks.

The following abstract methods need to be defined:

  • get_psf: returning the PSF of the mask.

  • project: projecting the mask parameters to a valid space (should be a subspace of [0,1]).

__init__(optimizer='Adam', lr=0.001, **kwargs)[source]

Base constructor. Derived constructor may define new state variables

Parameters
  • optimizer (str, optional) – Optimizer to use for updating the mask parameters, by default “Adam”

  • lr (float, optional) – Learning rate for the mask parameters, by default 1e-3

abstract get_psf()[source]

Abstract method for getting the PSF of the mask. Should be fully compatible with pytorch autograd.

Returns

The PSF of the mask.

Return type

Tensor

abstract project()[source]

Abstract method for projecting the mask parameters to a valid space (should be a subspace of [0,1]).

update_mask()[source]

Update the mask parameters. According to externaly updated gradiants.

class lensless.hardware.trainable_mask.TrainablePSF(initial_psf, grayscale=False, **kwargs)[source]

Class for defining an object that directly optimizes the PSF, without any constraints on what can be realized physically.

Parameters

grayscale (bool, optional) – Whether mask should be returned as grayscale when calling get_psf. Otherwise PSF will be returned as RGB. By default False.

__init__(initial_psf, grayscale=False, **kwargs)[source]

Base constructor. Derived constructor may define new state variables

Parameters
  • optimizer (str, optional) – Optimizer to use for updating the mask parameters, by default “Adam”

  • lr (float, optional) – Learning rate for the mask parameters, by default 1e-3

get_psf()[source]

Abstract method for getting the PSF of the mask. Should be fully compatible with pytorch autograd.

Returns

The PSF of the mask.

Return type

Tensor

project()[source]

Abstract method for projecting the mask parameters to a valid space (should be a subspace of [0,1]).