plenpy.cameras package
Subpackages
Submodules
plenpy.cameras.abstract_camera module
Module defining the AbstractCamera
base class.
This is the base class that all implemented cameras are derived from. The derived camera classes are defined in their respective modules.
- class plenpy.cameras.abstract_camera.AbstractCamera(path)[source]
Bases:
abc.ABC
AbstractCamera base class.
This is the base class that all actual camera classes are derived from. This class is abstract and objects cannot be initialized directly from it. Implements an API for all the basic functions needed by a camera model.
The structure of a camera folder has to be:
<camera-folder>
├── Images/
├── Calibration/
└── Reference/ [optional]
- Variables
~AbstractCamera.path (Path) – Absolute system path the main camera folder.
~AbstractCamera.imageFolderPath (Path) – Relative path to image folder.
~AbstractCamera.calibrationFolderPath (Path) – Relative path to calibration folder.
~AbstractCamera.referenceFolderPath (Path) – Relative path to reference folder.
~AbstractCamera._sensorImages (Dict[int, SensorImage]) – Dict of raw sensor images.
~AbstractCamera._decodedImages (Dict[int, Union[ndarray, LightField]]) – Dict of decoded sensor images.
~AbstractCamera._isCalibrated (bool) – Flag indicating camera’s calibration status.
AbstractCamera
base class initialization.- Parameters
path (
Any
) – System path to the main camera folder.
- _add_decoded_image(img, num)[source]
Add decoded image to dictionary of decoded images.
The dictionary of decoded images collects all decoded images. The function adds an image to that dictionary using the key which is specified by
num
.- Parameters
img (
Union
[ndarray
,LightField
]) – Decoded image to add.num (
int
) – Number of the sensor image that the image is decoded from.
- abstract calibrate()[source]
Calibrate the camera.
Calibrates the camera using the calibration metadata and/or the files provided in the Calibration/ subfolder. The implementation depends on the camera in use.
- abstract decode_sensor_image(num)[source]
Decode a sensor image.
The implementation depends on the camera in use.
- Parameters
num (
int
) – Number of the sensor image that is to be decoded.
- get_decoded_image(num)[source]
Get decoded image.
- Parameters
num (
int
) – Number of the sensor image that the image is decoded from.- Return type
- Returns
Decoded image decoded from sensor image of number
num
.
- list_sensor_images()[source]
Print a list of sensor images.
Prints all sensor images found in the camera’s image subfolder. Denotes which images have been loaded.
- load_sensor_image(num, format=None)[source]
Load a sensor image.
- Parameters
num (
Union
[int
,List
[int
],str
]) – Specifies the number of the image to load. If a list is passed, multiple images are loaded. If “all” is specified, all images in theImages
folder are loaded.format (
Optional
[str
]) – The image format to be used to read the file. By default, imageio selects an appropriate one based on the filename and its contents. Useplenpy.utilities.misc.get_avail_extensions()
to get a list of available formats.
- load_sensor_image_from_path(path, format=None)[source]
Load a sensor image from a system path image.
- Parameters
path (
Any
) – System path to the image file.format (
Optional
[str
]) – The format to be used to read the file. By default, imageio selects an appropriate one based on the filename and its contents. Useplenpy.utilities.misc.get_avail_extensions()
to get a list of available formats.
- abstract show_decoded_image(num)[source]
Show a decoded sensor image.
Shows the image decoded from sensor image
num
usingmatplotlib
This can be a regular RGB image as well as a hyperspectral image or aplenpy.lightfields.LightField
object.- Parameters
num (
int
) – Number of the sensor image that the image is decoded from.
- show_image_metadata(num)[source]
Print the metadata of a raw sensor image.
Prints the metadata of the sensor image specified by
num
on the standard output.- Parameters
num (
int
) – Number of the sensor image.
plenpy.cameras.abstract_lightfield_camera module
Module defining the AbstractLightFieldCamera
base class.
This bass class, derived from the AbstractCamera
base class,
implements methods that all microlens based light field cameras share.
In particular, this class implements the estimation of an ideal grid from
(a collection of) whiteimages.
- class plenpy.cameras.abstract_lightfield_camera.AbstractLightFieldCamera(path, microlens_size, grid_type, ml_focal_length=None)[source]
Bases:
plenpy.cameras.abstract_camera.AbstractCamera
AbstractLightFieldCamera base class.
This is the base class that all micro lens based light field camera classes are derived from. This class is abstract and objects cannot be initialized directly from it. Implements the basic functions needed by a light field camera model.
The abstract methods
AbstractCamera.calibrate()
andAbstractCamera.decode_sensor_image()
are left undefined and have to be implemented by derived light field camera classes.The camera inherits all attributes from the
AbstractCamera
class. Additional attributes are:- Variables
~AbstractLightFieldCamera._microlensSize (float) – The microlens diameter in pixels.
~AbstractLightFieldCamera._microlensRadius (float) – The microlens radius in pixels, derived from the microlens diameter.
~AbstractLightFieldCamera._microlensFocalLength (float) – The (ideal) microlens focal length in meters (optional).
~AbstractLightFieldCamera._calDataFilename (str) – The name of the calibration data file placed in the
Calibration
subfolder of the camera. Default is: cal_data.npz
AbstractLightFieldCamera
base class initialization.- Parameters
path (
Any
) – System path to the main camera folder.microlens_size (
float
) –Estimated size of the microlens diameter in pixels. For example:
Lytro Illum: 14
Lytro F01: 11
grid_type (
str
) – Grid type of the underlying microlens array. Either ‘hex’ or ‘rect’.ml_focal_length (
Optional
[float
]) – Focal length of the micro lenses in meters (optional).
- _align_image(img, wi_idx)[source]
Align to sensor image with the microlens array.
Image is rotated, scaled and translated, so that all microlens centers fall onto pixel centers and rotation is compensated. Bicubic itnerpolation is performed by default.
- static _calc_grid_params_helper(x, im, f_mat, hires)[source]
Helper function to calculate spacing and rotation from a white image using different hyperparameters.
- Parameters
x – Hyperparameter vector. x = [x_q, x_gamma, x_exponent], where - q is pixel value lower bound used for contrast stretching - gamma is gamma correction factor used for value stretching - exponent is the exponent used in the center of mass calculation Note that x is normalized in [0, 1]^3
im – Input white image.
f_mat – Pre-calculated basis frequency vectors.
hires – Whether to use hires FFT (zero padding) and return full grid values.
show – Whether to show intermediate results for debugging.
- Return type
- Returns
spacing_std [if hires == False] or Tuple (spacing, spacing_std, rot, rot_std) [if hires == True]
- static _crop_ml_centers(ml_centers, size, crop)[source]
Crop ml center coordinates by crop amount to get rid of edge defects
- _est_grid(method='own')[source]
Estimate regular grid(s) that best fits the white image(s). For every white image in the Calibration folder, a regular grid is estimated.
- Parameters
method (
str
) – Method for grid estimation. Choices are: - ‘own’: Proposed method [ref] - ‘dans’: Dansereau et al.
- static _est_grid_offset_dans(wi, ml_centers, spacing_est, rotation_est, grid_type, show=False)[source]
Estimate overall grid offset.
- static _est_grid_offset_prop(wi, lam_est, spacing_est, rotation_est, grid_type)[source]
Estimate overall grid offset.
- static _est_grid_params_prop_hex(wi)[source]
Estimate the grid parameters of a hex grid in the Fourier domain.
This algorithm follows the presentation in [PAPER]. The white image is fourier transformed and peaks are detected corresponding to the frequencies of the spatial basis vectors. From these, spacing and rotation are estimtaed.
- static _est_offset(grid_type, ml_centers, size, spacing_est, rotation_est, offset_init=None, window=None)[source]
Estimate a grid offset from estimated ml_centers of a white image.
- Parameters
Returns:
- static _get_align_transform(ideal_grid_params)[source]
Calculate transformation that is used to align the images.
- Return type
Tuple
[GridParameters
,AffineTransform
]
- _get_calibration_db_entry(path, method, ideal_grid_params, align_grid_params, align_transform)[source]
Get a single entry for the calibration database.
- Parameters
path (
Path
) – Path to the corresponding white image.method (
str
) – Method used for calibration.ideal_grid_params (
GridParameters
) – The ideal grid parameters.align_grid_params (
GridParameters
) – The aligned grid parameters.align_transform (
AffineTransform
) – The transformation needed for alignment.
- Return type
- Returns
A structured numpy array with the needed data.
- static _get_fourier_peaks(wi_fft, constrained, exponent=2.0)[source]
Find peaks in absolute value of a Fourier transform of an image.
- Parameters
- Return type
- Returns
Peak coordinates (p_x, p_y) as Numpy array of shape (N, 2) in units 1/px.
- static _get_grid_params_dans_hex(ml_centers, spacing_guess, rot_guess, wi, show=False)[source]
Grid parameter estimation as implemented by Dansereau in the MATLAB Light Field Toolbox v0.4.
The grid is traversed horizontally and vertically, performing line fits to estimate the grid rotation. For this a prior spacing and rotation estimate is necessary.
Spacing is calculated as the mean distance of grid neighbors in the corresponding grid direction.
- Parameters
ml_centers (
ndarray
) – Estimated micro lens centers to be used for grid estimate.spacing_guess (
float
) – A prior estimation of the grid spacing in y-direction.rot_guess (
float
) – A prior estimation of the grid rotation in radians.show (
bool
) – Boolean flag indicating whether to plot results for debugging.white_img – White image. Only used for plotting.
- Return type
- Returns
Estimated spacing (2D array) and rotation (in radians) of the grid. If not estimate is possible, returns nans.
- _get_ideal_grid_params(method, wi, args)[source]
Estimate a regular grid from a white image using the specified method.
- Parameters
- Return type
- Returns
Ideal grid parameters estimated from the white image.
- static _get_ml_centers_dans(wi)[source]
Calculat ML centers from WI, method proposed by Dansereau et al. in Matlab LightField Toolbox.
- static _get_ml_centers_prop(wi, lam_est)[source]
Estimate ml centers in only central part of the white image
- abstract _get_wi(path, process=True)[source]
Read the white image from path and perform preprocessing such as contrast stretching, normalization, etc., if desired.
- abstract _get_wi_db_entry(path)[source]
Get a single entry for the white image database.
The type of entry depends on the actual camera. Must contain a ‘focal_length’ entry. This could for example be focus and zoom settings of the white image or gamma settings etc.
- _resample_lf(lf, method)[source]
Resample from a hexagonally sampled to rectangular sampled image.
- Parameters
lf (
LightField
) – Light field to resample.method (
str
) – Resampling method. Available: - ‘guided’: Perform gradient guided interpolation (recommended) - ‘bilinear’ : Perform bilinear interpolation. - ‘horizontal’: Only use horizontal 1D-interpolation - ‘vertical’: Only use vertical 1D-interpolation
Returns:
- _save_cal_data(filename=None)[source]
Save the calibration data.
The calibration data is saved as a compressed numpy array file in the camera’s
Calibration
folder.
- _slice_image(img, wi_idx)[source]
Slice image into a light field.
- Parameters
- Return type
- Returns
Decoded light field.
- show_decoded_image(num)[source]
Show the decoded light field using
plenpy.lightfields.lightfield.LightField.show_interactive()
.- Parameters
num (
int
) – Number of the sensor image that the image is decoded from.
- show_microlens_centers(wi_idx, grid=False, plt_show=True)[source]
Show the calculated microlens centers on top of the whiteimage.
- Parameters
wi_idx (
int
) – Index of the corresponding white image.grid (
bool
) – Whether to also show the estimated ideal grid on topplt_show (
bool
) – Whethermatplotlib.pyplot.show()
is called at the end of the function
plenpy.cameras.generic_lightfield_camera module
plenpy.cameras.lytro_illum module
Module defining the LytroIllum
camera class.
This camera class, derived from the AbstractLightFieldCamera
base
class, implements the Lytro Illum camera.
- class plenpy.cameras.lytro_illum.LytroIllum(path, format='LYTRO-ILLUM-RAW')[source]
Bases:
plenpy.cameras.abstract_lightfield_camera.AbstractLightFieldCamera
Lytro Illum light field camera.
The class does not add any attributes to the
AbstractLightFieldCamera
base class.- Parameters
path (
Any
) – Folder path of camera.format – The
imageio
format of the white images. Default: LYTRO-ILLUM-RAW.
- static _get_focal_length(zoom_step)[source]
Calculate focal length in meter from zoom_step setting.
Uses a cubic function obtained via least squares fit of data measured with a Lytro Illum Camera.
- _get_wi(path, process=False, meta_only=False)[source]
Read the white image from path and perform preprocessing such as contrast stretching, normalization, etc.
- _get_wi_db_entry(path)[source]
Get the database entry for a white image. Entries include the zoomStep and focusStep setting of the white image to be used for decoding.
- calibrate(filename=None, method='own', force=False)[source]
Calibrate the Lytro Illum camera.
The calibration estimates the microlens centers from the provided white images. Based on the chosen method, this will yield ml centers with (
det_method='own'
) or without (det_method='dans'
) subpixel precision. Using the estimated ML centers, an ideal grid is estimated that best approximates the ML centers. This grid is used in the decoding of every light field (depending on the zoom and focus settings).- Parameters
filename (
Optional
[str
]) – System path to calibration filename withoout extension. IfNone
, the standard path is used.method (
str
) – Method used for grid estimation. - ‘own’: Own method, proposed in [ref] - ‘dans’: Method by Dansereau et al.force (
bool
) – IfTrue
forces the recalibration, even if a calibration file is found. This can be useful when recalibrating with different parameters.
- decode_sensor_image(num, demosaic_method='malvar2004', resample_method='guided')[source]
Decode the specified sensor image.
The decoding yields a
plenpy.lightfields.lightfield.LightField
object that is added the objects dictionary of decoded images.- Parameters
num (
int
) – Number of the sensor image that is to be decoded.demosaic_method (
str
) – Method used to calculate the demosaiced image. IfNone
is specified, no demosaicing is performed. For available methods and default value, seeplenpy.utilities.demosaic.get_demosaiced()
.resample_method (
str
) – Method used to resample the light field from a hex to rect grid. Available: - ‘guided’: Perform gradient guided interpolation (recommended) - ‘bilinear’ : Perform bilinear interpolation. - ‘horizontal’: Only use horizontal 1D-interpolation - ‘vertical’: Only use vertical 1D-interpolation
- static unique_exposure_gen(db, paths_processed)[source]
Generator to yield unique exposure and iso values from a raw database together with the correpsoning current database and paths.
- Parameters
db – Raw image database to process.
paths_processed – Paths of raw images that have already been processed
- Yields
tmp_db, paths_curr, exposure, iso
- static unique_geometry_exposure_gen(db, paths_processed)[source]
Generator to yield unique zoom, focus, exposure and iso values from a raw database together with the correpsoning current database and paths.
- Parameters
db – Raw image database to process.
paths_processed – Paths of raw images that have already been processed
- Yields
tmp_db, paths_curr, exposure, iso, focus_step, zoom_step
plenpy.cameras.ms_lytro module
Module defining the MsLytro
camera class.
This camera class, derived from the AbstractLightFieldCamera
base
class, implements a prototype multispectral Lytro Illum camera.
- class plenpy.cameras.ms_lytro.MsLytro(path, num_channels=13, format='LYTRO-ILLUM-RAW')[source]
Bases:
plenpy.cameras.lytro_illum.LytroIllum
Multispectral Lytro Illum light field camera.
- Parameters
- _get_ms_wi(folder, dtype=<class 'numpy.float64'>, process=False, multi_exposure=False, return_meta=False, meta_only=False)[source]
Read a multispectral white image from a folder and perform preprocessing such as contrast stretching, normalization, etc.
- Parameters
- Returns
Multispectral white image as ndarray and metadata.
- _get_ms_wi_db_entry(folder)[source]
Get the database entry for a white image. Entries include the zoomStep and focusStep setting of the white image to be used for decoding.
- _read_ms_raw(folder, dtype=<class 'numpy.float64'>, multi_exposure=False, meta_only=False)[source]
Reads a single multispectral raw light field. All raw data must be contained in a single folder with alphanumerically sorted filenames according to the corresponding spectral channel index order.
Checks data for consistency regarding the camera serial number, the ISO setting and the exposure time.
- Parameters
folder (
Path
) – Folder containing the raw measurements per channel. The folder is searched recursively.dtype – Dtype used for loading.
multi_exposure – Whether to allow multi-exposure series.
meta_only – Whether to only read the metadata.
- Return type
- Returns
Spectral raw data of shape (num_channels, x, y)
- _save_cal_data(filename=None)[source]
Save the calibration data.
The calibration data is saved as a compressed numpy array file in the camera’s
Calibration
folder.
- decode_image(folder, resample_method='guided')[source]
Decode a multispectral light field
The decoding returns a
plenpy.lightfields.lightfield.LightField
object.- Parameters
folder (
str
) – Path to multispectral raw data folder.resample_method (
str
) – Method used to resample the light field from a hex to rect grid. Available: - ‘guided’: Perform gradient guided interpolation (recommended) - ‘bilinear’ : Perform bilinear interpolation. - ‘horizontal’: Only use horizontal 1D-interpolation - ‘vertical’: Only use vertical 1D-interpolation
- static fit_linear_full(data, exposures, dark_current, dark_offset, epochs=2000, learning_rate=0.01, **kwargs)[source]
- Parameters
- Return type
- Returns
responsivity, offset
- static fit_linear_pixel_wise(data, exposures, dark_current, dark_offset, **kwargs)[source]
- Parameters
- Return type
- Returns
responsivity, offset
- static unique_ms_wi_gen(db, paths_processed)[source]
Generator to yield unique zoom, focus, and iso values from the multispectral white image database together with the corresponding current database and paths.
- Parameters
db – Raw image database to process.
paths_processed – Paths of raw images that have already been processed
- Yields
tmp_db, paths_curr, exposure, iso, focus_step, zoom_step
plenpy.cameras.raytracer_lightfield_camera module
Module defining the RayTracerLF
camera class.
This camera class, derived from the AbstractLightFieldCamera
base
class, implements a MLA based light field camera synthesized by the IIIT-RayTracer.
- class plenpy.cameras.raytracer_lightfield_camera.RayTracerLF(path, grid_type='hex', ml_size=14, ml_focal_length=4e-05, format='PNG-FI')[source]
Bases:
plenpy.cameras.abstract_lightfield_camera.AbstractLightFieldCamera
Raytracer implementation of MLA based LF camera.
The class does not add any attributes to the
AbstractLightFieldCamera
base class.AbstractLightFieldCamera
base class initialization.- Parameters
path (
Any
) – System path to the main camera folder.microlens_size –
Estimated size of the microlens diameter in pixels. For example:
Lytro Illum: 14
Lytro F01: 11
grid_type (
str
) – Grid type of the underlying microlens array. Either ‘hex’ or ‘rect’.ml_focal_length (
float
) – Focal length of the micro lenses in meters (optional).
- _get_metadata(path)[source]
Get the metadata from a raytracer image using its JSON file.
- Parameters
path (
Path
) – Path to the white image, relative to Calibration folder.
- _get_wi(path, process=True)[source]
Read the white image from path and perform preprocessing such as contrast stretching, normalization, etc.
- _get_wi_db_entry(path)[source]
Get the database entry for a white image. Entries include the focal_length and focus_distance setting of the white image to be used for decoding.
- calibrate(filename=None, method='own', force=False)[source]
Calibrate the RayTracer camera.
See Also: :func:LytroIllum.calibrate()
- Parameters
filename (
Optional
[Any
]) – System path to calibration filename withoout extension. IfNone
, the standard path is used.method (
str
) – Method used for grid estimation. - ‘own’: Own method, proposed in [ref] - ‘dans’: Method by Dansereau et al.force (
bool
) – IfTrue
forces the recalibration, even if a calibration file is found. This can be useful when recalibrating with different parameters.
- decode_sensor_image(num, resample_method='guided')[source]
Decode the specified sensor image.
The decoding yields a
plenpy.lightfields.lightfield.LightField
object that is added the objects dictionary of decoded images.- Parameters
num (
int
) – Number of the sensor image that is to be decoded.resample_method (
str
) – Method used to resample the light field from a hex to rect grid. Available: - ‘guided’: Perform gradient guided interpolation (recommended) - ‘bilinear’ : Perform bilinear interpolation. - ‘horizontal’: Only use horizontal 1D-interpolation - ‘vertical’: Only use vertical 1D-interpolation
plenpy.cameras.rgb_camera module
Module defining the RgbCamera
class.
- class plenpy.cameras.rgb_camera.RgbCamera(path, bayer_pattern)[source]
Bases:
plenpy.cameras.abstract_camera.AbstractCamera
Basic RGB camera.
This class is a very basic camera class illustrating the implementation of the necessary class methods such as calibration and decoding. The RGB camera decoding process is in this case just a simple demosaicing of the raw sensor image.
Todo: Implement the calibration of a camera model, e.g. to do rectification of the images.
- Variables
~RgbCamera._bayerPattern (str) – The camera’s mosaicing Bayer pattern.
RgbCamera
class initialization.- Parameters
path (
Any
) – System path to the main camera folder.bayer_pattern (
str
) – The camera’s mosaicing Bayer pattern. See alsoplenpy.utilities.demosaic.get_demosaiced()
.
- calibrate()[source]
Calibrate the camera.
Calibrates the camera using the calibration metadata and/or the files provided in the Calibration/ subfolder. The implementation depends on the camera in use.
- decode_sensor_image(num, method='malvar2004')[source]
Decode the sensor image of the RGB Camera.
This is just a demosaicing of the monochromatic sensor image to obtain a RGB color image.
- Parameters
num (
int
) – Number of the sensor image to decode.method (
str
) – Method used to calculate the demosaiced image. See Alsoplenpy.utilities.demosaic.get_demosaiced()
- show_decoded_image(num, plt_show=True)[source]
Show the decoded image using matplotlib.
- Parameters
num (
int
) – Number of the sensor image that the image is decoded from.plt_show (
bool
) – Flag indicating whether to callmatplotlib.pyplot.show()
at the end.
Module contents
The camera package of plenpy.
The package provides a framework to implement camera models as well as
pre developed cameras such as a basic
plenpy.cameras.rgb_camera.RgbCamera
or light field cameras such as
the plenpy.cameras.lytro_illum.LytroIllum
camera.
The framework provides an API to implement own camera modules (or classes).