darsia.CurvatureCorrection#

class CurvatureCorrection(config=None, **kwargs)[source]#

Bases: BaseCorrection

Polynomial curvature correction.

Contains routines for setting up the curvature correction, as well as applying it to images.

Attributes:
configdict

Config dictionary for curvture correction.

Circumstantial attributes: reference_image (np.ndarray): image matrix of the reference image. current_image (np.ndarray): image matrix of the updated reference image. width (float): physical width of reference image. height (float): physical height of reference image. in_meters (bool): True if width/height is in meters. Ny (int): number of pixels in vertical direction in reference image. Nx (int): number of pixels in horizontal direction in reference image.

Set up the curvature correction.

Normally initialised from a config file that controls the correction routine. Initialising from an image path (not a darsia.Image) is only meant for building the config file interactively.

Parameters:
**kwargs

Either config or image must be given:

config (dict | str | Path)   config dictionary (default None)
image (Path | np.ndarray)    image source, as a path or an array
width (float)                physical image width (image mode)
height (float)               physical image height (image mode)
in_meters (bool)             whether width/height are in metres
                             (image mode)

Methods

bulge_correction([left, right, top, bottom])

Bulge correction

compute_bulge([img])

Compute the bulge parameters depending on the maximum number of pixels that the image has been displaced on each side.

compute_stretch([img])

Compute the stretch parameters depending on the stretch center, and a known translation.

correct_array(img)

Call method of the curvature correction.

correct_metadata([metadata])

Extract metadata from the config file.

load(path)

Load the curvature correction from a file.

pre_bulge_correction(**kwargs)

Force all straight lines to curve inwards rather than outwards.

read_config_from_file(path)

Reads a json-file to the config disctionary.

return_image()

Returns the current image as a darsia image width provided width and height.

save(path)

Save the curvature correction to a file.

setup_config([config])

show_image()

Shows the current image using matplotlib.pyplot

simple_curvature_correction(img, **kwargs)

General routine for applying stretch and bulge transforms.

stretch_correction(point_source, ...)

Stretch the image from the displacement of a single point.

write_config_to_file(path)

Writes the config dictionary to a json-file.

Attributes

temporary_image

bulge_correction(left=0, right=0, top=0, bottom=0)[source]#

Bulge correction

Corrects bulging of image, depending on the amount of pixels that the image is bulged inwards on each side.

Parameters:
leftint

Amount of bulged pixels on the left side of the image.

rightint

Amount of bulged pixels on the right side of the image.

topint

Amount of bulged pixels on the top of the image.

bottomint

Amount of bulged pixels on the bottom of the image.

compute_bulge(img=None, **kwargs)[source]#

Compute the bulge parameters depending on the maximum number of pixels that the image has been displaced on each side.

Parameters:
imgnp.ndarray, optional

Image array, basis for the computation.

**kwargsoptional keyword arguments

“left” (int): the maximum number of pixels that the image has been displaced on the left side “right” (int): the maximum number of pixels that the image has been displaced on the right side “top” (int): the maximum number of pixels that the image has been displaced on the top “bottom” (int): the maximum number of pixels that the image has been displaced on the bottom.

compute_stretch(img=None, **kwargs)[source]#

Compute the stretch parameters depending on the stretch center, and a known translation.

Parameters:
imgnp.ndarray, optional

Image array, basis for the computation.

**kwargsoptional keyword arguments

“point_source” (list): point that has been translated. “point_destination” (list): the ought to be position. “stretch_center” (list): the stretch center.

correct_array(img)[source]#

Call method of the curvature correction.

Applies the curvature correction to a provided image, and returns the corrected image as an array. If set in the constructor, the image will be resized in the first step.

Parameters:
imgnp.ndarray

Image array.

Returns:
np.ndarray

Curvature corrected image.

correct_metadata(metadata={})[source]#

Extract metadata from the config file.

Parameters:
metadatadict, optional

Metadata dictionary to be updated. Defaults to {}.

Returns:
dict

Metadata.

load(path)[source]#

Load the curvature correction from a file.

Parameters:
pathPath

Path to the file.

pre_bulge_correction(**kwargs)[source]#

Force all straight lines to curve inwards rather than outwards.

Parameters:
**kwargs

Curvature parameters:

horizontal_bulge (float)          horizontal bulge
horizontal_center_offset (int)    x offset of the image centre
                                  from the numerical centre, px
vertical_bulge (float)            vertical bulge
vertical_center_offset (int)      y offset of the image centre
                                  from the numerical centre, px
read_config_from_file(path)[source]#

Reads a json-file to the config disctionary.

Parameters:
pathPath

Path to the json-file.

return_image()[source]#

Returns the current image as a darsia image width provided width and height.

save(path)[source]#

Save the curvature correction to a file.

Parameters:
pathPath

Path to the file.

show_image()[source]#

Shows the current image using matplotlib.pyplot

simple_curvature_correction(img, **kwargs)[source]#

General routine for applying stretch and bulge transforms. This routine in contrast to __call__ does always use the keyword arguments and constructs the transformation instead of using cached values.

Parameters:
imgnp.ndarray

Image array.

**kwargsoptional keyword arguments

See _transform_coordinates for more details.

Returns:
np.ndarray

Corrected image.

stretch_correction(point_source, point_destination, stretch_center)[source]#

Stretch the image from the displacement of a single point.

The image is stretched given a point’s displacement (point_source -> point_destination) and one undisplaced point (stretch_center).

Parameters:
point_sourcelist

Point that has been translated.

point_destinationlist

Its intended position.

stretch_centerlist

The undisplaced (stretch centre) point.

write_config_to_file(path)[source]#

Writes the config dictionary to a json-file.

Parameters:
pathPath

Path to the json file.