pysdic.Image#

The pysdic.Image stores a grayscale or color image and builds interpolation functions (one scipy.interpolate.RectBivariateSpline per channel) to evaluate the image and its derivatives at any point of a region of interest (ROI).

Important

Two coordinate systems are used: the pixel points \((u, v)\) (row, column) and the image points \((x, y)\) (column, row) of the camera. The spline degrees ku and kv refer to the \(u\) and \(v\) axes of the pixel points.

Image class#

class Image(image, *, roi=None, ku=3, kv=3, default_value=nan, auto_construct=True)[source]#

Bases: NpzSerializerMixin

A class to represent an immutable 2D image to interpolate gray or colors values.

The image is associated with an interpolation function to evaluate pixel values (and their derivatives) at arbitrary points in the image: one pysdic.imaging.rect_bivariate_spline.RectBivariateSpline built on the ROI with all the channels (one scipy spline per channel). All the channels and several derivative orders are evaluated in a single pass (see evaluate_at_pixel_points()).

The coordinates system used for the image is defined as follows:

  • The pixel_coordinates are defined in the array coordinate system \((u, v)\) where \(u\) is the row index and \(v\) is the column index.

  • The image_coordinates are defined in the camera coordinate system \((x, y)\) where \(x\) is the column index and \(y\) is the row index.

Important

To avoid errors, kx and ky used by scipy are renamed ku and kv as the \(x\)-axis of scipy refer to the \(u\)-axis (ie the \(y\)-axis of image_coordinates).

Important

An image is immutable: the data, the roi, ku, kv and default_value are fixed at the creation and cannot be modified. Use with_settings() to get an image with other settings (it shares the data, without copy) and from_array() to create an image with other data.

The immutability guarantees that the objects using an image (views, SDIC analyses, …) always see the same values, and allows several images to share the same data without copy (for example the two halves of a split-screen image, with two ROIs).

Note

The image is stored as given by the user (integer or floating point dtype), in a read-only copy. It is only converted to numpy.float64 to build the interpolation function.

Note

An image can be saved with all its settings in a single NPZ file with to_npz_file() and loaded with from_npz_file(): the data are stored exactly (any dtype, any number of channels), without conversion.

Note

The interpolation function is a cache: it is built at the creation (unless auto_construct=False) or with construct_interpolation_functions(), and can be freed with deconstruct_interpolation_functions() to save memory. This does not change the values of the image.

Parameters:
  • image (ArrayLike) – The image with shape (height, width) or (height, width, channels), with an integer or floating point dtype (finite values only). The array is copied.

  • roi (Optional[Tuple[Integral, Integral, Integral, Integral]], optional (default: None)) – The region of interest (ROI) in the image, defined as a tuple (x, y, width, height) in pixels: the ROI contains the columns x, ..., x + width - 1 and the rows y, ..., y + height - 1. It must be inside the image, with height > ku and width > kv. If None, the entire image is used. Restricting the ROI reduces the cost of building and of evaluating the interpolation function.

  • ku (Integral, optional (default: 3)) – The degree of the spline in the \(u\)-direction, between 1 and 5. Warning ku refers to \(u\)-axis of the pixel_coordinates (ie \(y\)-axis of the image_coordinates - the kx of scipy).

  • kv (Integral, optional (default: 3)) – The degree of the spline in the \(v\)-direction, between 1 and 5. Warning kv refers to \(v\)-axis of the pixel_coordinates (ie \(x\)-axis of the image_coordinates - the ky of scipy).

  • default_value (Real, optional (default: numpy.nan)) – The value returned by the interpolation function and its derivatives for evaluations out of the ROI (NaN and infinity are accepted).

  • auto_construct (bool, optional (default: True)) – Whether to construct the interpolation function at initialization.

Raises:
  • TypeError – If the dtype of the image is not an integer or floating point dtype, or if the ROI is not a tuple.

  • ValueError – If the shape of the image is invalid, if a floating point image contains non-finite values, if a spline degree is out of \([1, 5]\), or if the ROI is out of the image or too small for the spline degrees.

Instantiation#

The Image class can be instantiated in several ways:

  • From an array of shape \((H, W)\) or \((H, W, C)\) with the constructor or the classmethod from_array().

  • From a file read with OpenCV using the classmethod from_file() (color images in BGR order).

Important

Any modification of image, roi, ku or kv resets the interpolation functions: call construct_interpolation_functions() afterwards.

Image.from_array(image_data, **kwargs)

Create an pysdic.Image object from raw image data.

Image.from_file(file_path, **kwargs)

Create an pysdic.Image object by loading image data from a file.

Implemented methods and attributes#

The following sections provide an overview of the attributes and methods defined in the pysdic.Image class.

Attributes#

Image.default_value

[Get] The value returned by the interpolation function and its derivatives for evaluations out of the roi.

Image.dtype

[Get] The data type of the image.

Image.height

[Get] The height of the image.

Image.image

[Get] The image data, with shape (height, width) or (height, width, channels) and its original dtype.

Image.is_color

[Get] Check if the image is a color image (3D array).

Image.is_grayscale

[Get] Check if the image is grayscale (2D array).

Image.ku

[Get] The degree of the spline in the \(u\)-direction.

Image.kv

[Get] The degree of the spline in the \(v\)-direction.

Image.n_channels

[Get] The number of channels in the image (1 for a grayscale image).

Image.ndim

[Get] The number of dimensions of the image.

Image.roi

[Get] The region of interest (ROI) in the image.

Image.shape

(height, width) or (height, width, channels).

Image.width

[Get] The width of the image.

Interpolation#

Image.construct_interpolation_functions()

Construct the interpolation function of the image.

Image.deconstruct_interpolation_functions()

Free the interpolation function of the image, for example to save memory when the image is not evaluated anymore.

Image.evaluate_at_image_points(image_points)

Evaluate the image and/or several of its derivatives at given image_points (ie \((x, y)\)-axis), in a single pass.

Image.evaluate_at_pixel_points(pixel_points)

Evaluate the image and/or several of its derivatives at given pixel_points (ie \((u, v)\)-axis), in a single pass.

Image.get_interpolation_function()

Get the interpolation function of the image (all the channels).

Pixel and image points#

Image.get_image_pixel_points([mask])

Create a grid of pixel points covering the entire image.

Image.get_image_image_points([mask])

Create a grid of image points covering the entire image.

Image.pixel_points_to_image_points(pixel_points)

Convert pixel points \((u, v)\) to image points \((x, y)\) by swapping the two coordinates.

Image.image_points_to_pixel_points(image_points)

Convert image points \((x, y)\) to pixel points \((u, v)\) by swapping the two coordinates.

Conversion and visualization#

Image.copy()

Create a copy of the pysdic.Image object.

Image.to_array()

Get a modifiable copy of the image data as a numpy array (with its original dtype).

Image.to_file(file_path)

Save the image to a file using OpenCV, with its own dtype.

Image.quick_visualize(*[, cmap, clim, ...])

Quickly visualize the image with pysdic.ImagePlotter (color images are assumed in BGR order, as read by from_file()).