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:
NpzSerializerMixinA 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.RectBivariateSplinebuilt 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 (seeevaluate_at_pixel_points()).The coordinates system used for the image is defined as follows:
The
pixel_coordinatesare defined in the array coordinate system \((u, v)\) where \(u\) is the row index and \(v\) is the column index.The
image_coordinatesare 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
kuandkvas the \(x\)-axis of scipy refer to the \(u\)-axis (ie the \(y\)-axis ofimage_coordinates).Important
An image is immutable: the data, the
roi,ku,kvanddefault_valueare fixed at the creation and cannot be modified. Usewith_settings()to get an image with other settings (it shares the data, without copy) andfrom_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.float64to 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 withfrom_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 withconstruct_interpolation_functions(), and can be freed withdeconstruct_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 columnsx, ..., x + width - 1and the rowsy, ..., y + height - 1. It must be inside the image, withheight > kuandwidth > 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. Warningkurefers to \(u\)-axis of thepixel_coordinates(ie \(y\)-axis of theimage_coordinates- thekxof scipy).kv (Integral, optional (default:
3)) – The degree of the spline in the \(v\)-direction, between 1 and 5. Warningkvrefers to \(v\)-axis of thepixel_coordinates(ie \(x\)-axis of theimage_coordinates- thekyof 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.
|
Create an |
|
Create an |
Implemented methods and attributes#
The following sections provide an overview of the attributes and methods defined
in the pysdic.Image class.
Attributes#
[Get] The value returned by the interpolation function and its derivatives for evaluations out of the |
|
[Get] The data type of the image. |
|
[Get] The height of the image. |
|
[Get] The image data, with shape (height, width) or (height, width, channels) and its original dtype. |
|
[Get] Check if the image is a color image (3D array). |
|
[Get] Check if the image is grayscale (2D array). |
|
[Get] The degree of the spline in the \(u\)-direction. |
|
[Get] The degree of the spline in the \(v\)-direction. |
|
[Get] The number of channels in the image (1 for a grayscale image). |
|
[Get] The number of dimensions of the image. |
|
[Get] The region of interest (ROI) in the image. |
|
(height, width) or (height, width, channels). |
|
[Get] The width of the image. |
Interpolation#
Construct the interpolation function of the image. |
|
Free the interpolation function of the image, for example to save memory when the image is not evaluated anymore. |
|
|
Evaluate the image and/or several of its derivatives at given |
|
Evaluate the image and/or several of its derivatives at given |
Get the interpolation function of the image (all the channels). |
Pixel and image points#
|
Create a grid of pixel points covering the entire image. |
|
Create a grid of image points covering the entire image. |
|
Convert pixel points \((u, v)\) to image points \((x, y)\) by swapping the two coordinates. |
|
Convert image points \((x, y)\) to pixel points \((u, v)\) by swapping the two coordinates. |
Conversion and visualization#
Create a copy of the |
|
Get a modifiable copy of the image data as a numpy array (with its original dtype). |
|
|
Save the image to a file using OpenCV, with its own dtype. |
|
Quickly visualize the image with |