The Image object#
The Image class is essential for all image
operations and processing within this Toolbox. The class
encapsulates a NumPy array that contains the pixel values of a greyscale or color image
as a 2D or 3D array respectively. An Image
instance has a very large number of methods that perform useful operations on an image
and wrap low-level operations performed using NumPy or OpenCV.
- class Image(image: Image | ndarray | None = None, colororder: str | dict | None = None, copy: bool = False, size: tuple | list | None = None, dtype: DTypeLike | bool | None = None, name: str | None = None, id: int | None = None, domain=None, binary: bool = False, **kwargs)[source]#
Create an Image instance
- Parameters:
image (array_like(H,W),
Image) – image datacolororder (str, dict) – order of color channels
copy (bool, optional) – copy the image data, defaults to False
size (tuple, optional) – new size for the image, defaults to None
dtype (str or NumPy dtype, optional) – data type for image, defaults to same type as
imagename (str, optional) – name of image, defaults to None
id (int, optional) – numeric id of image, typically a sequence number, defaults to None
domain (array_like(W), array_like(H), optional) – domain of image, defaults to None
binary (bool, optional) – create binary image, non-zero values are set to True, defaults to False
- Raises:
TypeError – unknown type passed to constructor
Create a new
Imageinstance which contains pixel values as well as information about image size, datatype, color planes and domain. The pixel data is stored in a NumPy array, encapsulated by the object.An image is considered to be a two-dimensional (width x height) grid of pixel values, that lie within one or more “color” planes.
The image data can be specified by:
a NumPy 2D or 3D array for a greyscale or color image respectively. For the latter case, the last index represents the color plane:
Image(nd.zeros((100, 200))) Image(nd.zeros((100, 200, 4)), colororder="WXYZ")
a lists of lists of pixel values, each inner list must have the same number of elements (columns):
Image([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
a range of class methods that serve as constructors for common image types such as
Zeros,Constant,RandomandString, or read an image from a fileRead.an existing
Imageinstance.
Pixel datatype
If
dtypeis a string or a NumPy dtype, the pixel data type is set to that type.If
dtypeisTruethe pixel data type is inherited from the input if it is a NumPy array.If
dtypeis not given and:the input is a NumPy array,the image data type is determined by the dtype of the array:
if floating point, then the Image inherits the dtype of the array
if integer, then the Image is assigned the smallest integer dtype that can contain the values in the array. If the minimum value is negative, a signed integer type is used; otherwise an unsigned type is used.
the input is a list of lists then the image data type is:
float32 if the list contains any floating point values, otherwise
the smallest signed or unsigned int that can represent its value span.
An image can have boolean pixel values which are stored as
uint8values. When used in a numerical expression, its values will be cast to integer values of 0 or 1 representing False and True respectively.Color planes
Images can have multiple planes, typically three (representing the primary colors red, green and blue) but any number is possible. In the underlying Numpy array, these planes are identified by an integer plane index (the last dimension of the 3D array).
Rather than rely on a limiting convention such as planes being in the order RGB or BGR, the
Imagecontains a dictionary that maps the name of a color plane to its index value. The color plane order can be specified as a dict or a string, eg:Image(img, colororder="RGB") Image(img, colororder="red:green:blue:alpha") Image(img, colororder=dict("R"=2, "G"=1, "B"=0))
Image planes can be referenced by their index or by their name, eg.:
img.plane(0) img.plane("alpha")
- Seealso:
colorordercolororder_str
Image domain
An
Imagehas a width and height in units of pixels, but for some applications it is useful to specify the pixel coordinates in other units, perhaps metres, or latitude/longitude angles, or for a spherical image as azimuth and colatitude angles. The domain is specified by two 1D arrays that map the pixel coordinate to the domain variable.Binary images
If
binaryis True, the image is converted to a binary image, where zero valued pixels are set to False and all other values are set to True. To create an image where pixels have integer values of 0 and 1 use thedtypeoption:Image([[0, 3], [4, 0]]) # pixel values are 0, 3, 4, 0 Image([[0, 3], [4, 0]], binary=True) # pixel values are: False, True, True, False Image([[0, 3], [4, 0]], binary=True, dtype="uint8") # pixel values are 0, 1, 1, 0
Reshaping
Frequently we need to create an image from 1D data, for example:
1: Y0 Y1 Y2 ... # (N,) 2: R0 G0 B0 R1 G1 B1 ... # (3N,)
Or a 2D array with one or more rows:
3: Y0 Y1 Y2 ... # (1, N) 4: R0 G0 B0 R1 G1 B1 ... # (1,3N) 5: R0 R1 R2 ... # (3,N) G0 G1 G2 ... B0 B1 B2 ...
Or a 2D array with one or more columns:
6: Y0 # (N,1) Y1 Y2 . . 7: R0 # (3N,1) G0 B0 R1 . . 8: R0 G0 B0 # (N,3) R1 G1 B1 R2 G2 B2 . .
The
sizeoption can be used to reshape the data to the specified size. The number of planes is determined from any of:colororder, the third element ofsize, or the number of row/columns in the data (formats 5 or 8). For formats 2, 4 or 7 the number of planes must be given explicitly incolororder, the third element ofsize.- Seealso:
view1d
Example:
>>> from machinevisiontoolbox import Image >>> import numpy as np >>> img = Image([[1, 2], [3, 4]]) >>> print(img) Image: 2 x 2 (uint8), 1 anonymous plane span=[1, 4]; mean=2.5, 𝜎=1.11803; median=2.5 >>> img.print() 1 2 3 4 >>> Image(np.array([[1, 2], [3, 4]])) Image(size=(2, 2), dtype=uint8) >>> Image([[1, 2], [3, 1000]]) Image(size=(2, 2), dtype=uint16) >>> Image([[0.1, 0.2], [0.3, 0.4]]) Image(size=(2, 2), dtype=float32) >>> Image([[True, False], [False, True]]) Image(size=(2, 2), dtype=bool)
Warning
If an image is constructed from an existing
Imageinstance or a Numpy array, the encapsulated Numpy array is, by default, a reference to the passed image data. Use the optioncopy=Trueif you want to copy the data.
Image attributes and datatype#
Image attributes#
Describe the attributes of an Image.
|
Readable representation of image parameters |
|
Summary of image parameters |
|
Coordinate of center pixel |
|
Coordinate of centre pixel as integer |
|
Coordinate of centre pixel |
|
Coordinate of centre pixel as integer |
|
Image height |
|
Set/get image name |
|
Number of pixels in image plane |
|
Image size |
|
Image width |
|
Number of color planes |
Predicates#
Test attributes of an Image.
|
Image has BGR color order? |
|
Image has bolean values? |
|
Image has color pixels? |
|
Image has floating point pixel values? |
|
Image has integer values? |
|
Image has RGB color order? |
Color planes and channels#
Return information about the color planes of an Image instance.
Image coordinates#
Describe the pixel coordinates of an Image.
|
Test if coordinate lies within image |
|
|
|
Image maximum u-coordinate |
|
Linear span of image horizontally |
|
Image maximum v-coordinate |
|
Linear span of image vertically |
NumPy pixel data#
Return Image pixel data as a NumPy array.
|
Image as NumPy array |
|
Convert Image to NumPy array of specified type |
|
Image as NumPy array in BGR color order |
|
Number of image array dimensions |
|
Image as NumPy array in RGB color order |
|
Image shape |
|
Image as float NumPy array |
|
Image as integer NumPy array |
|
Convert image to a column view |
Getting pixels#
Access individual pixels or groups of pixels.
|
Return pixel value or slice from image |
|
Return pixel value |
|
Return pixel values at locations specified by a mask |
Image datatype#
Describe or change the datatype of Image pixel values.
|
Cast image datatype |
|
Cast value to same type as image |
|
Datatype of image |
|
False value for logical image |
|
Fix bad values in image |
|
Convert value to the same type as image |
|
Maximum value of image datatype |
|
Minimum value of image datatype |
|
Number of Inf pixels in image |
|
Number of NaN pixels in image |
|
Convert image datatype |
|
True value for logical image |
Image processing#
Sub images#
Extract sub-images or planes from an Image instance.
|
Extract the blue plane of a color image |
|
Create image copy |
|
Extract the green plane of a color image |
|
Extract plane(s) from color image |
|
Extract the red plane of a color image |
|
Extract region of interest |
Color space and gamma#
Convert between color spaces and perform gamma encoding and decoding.
|
Create chromaticity image |
|
Colorize a greyscale image |
|
Transform a color image between color representations |
|
Gamma decoding |
|
Gamma encoding |
|
k-means color clustering |
|
Convert color image to monochrome |
Composition#
Combine multiple Image instances into a single Image instance.
|
Convert stereo images to an anaglyph image |
|
Image blending |
|
Horizontal concatenation of images |
|
Overlay two greyscale images in different colors |
|
Concatenation of image planes |
|
Interactive display of stereo image pair |
|
Tile images into a grid |
|
Vertical concatenation of images |
Monadic functions#
Operate elementwise on an Image instance and returns a new Image instance.
|
Absolute value of image |
|
Apply a function to an image |
|
Clip pixel values |
|
Invert image |
|
Apply lookup table |
|
Histogram normalisaton |
|
Square root of image |
|
Image normalisation |
|
Image threshold |
|
Adaptive threshold |
|
Interactive thresholding |
Dyadic functions#
Operate elementwise on two Image instances and return a new Image instance.
|
Apply a function to two images |
|
Pixel-wise image merge |
|
Paste an image into an image |
Linear filtering#
Linear filtering operations including convolution, corner and edge detection.
|
Canny edge detection |
|
Image convolution |
|
Compute horizontal and vertical gradients |
|
Harris corner strength image |
|
Pyramidal image decomposition |
|
Compute image scalespace sequence |
|
Smooth image |
Non-linear (morphological) filtering#
Variety of non-linear morphological operations.
|
Morphological closing |
|
Morphological dilation |
|
Distance transform |
|
Find end points on a binary skeleton image |
|
Morphological erosion |
|
Hit or miss transform |
|
Median filter |
|
Morphological neighbourhood processing |
|
Morphological opening |
|
Rank filter |
|
Morphological skeletonization |
|
Morphological skeletonization with animation |
|
Find triple points |
|
Generalized spatial operator |
|
Compute zero crossing |
Image labeling#
Binary, greyscale and color image segmentation using various algorithms.
|
Blob labelling |
|
Blob labelling using graph-based segmentation |
|
Blob labelling using MSER |
Image similarity#
Various scalar image similarity measures.
|
Normalised cross correlation |
|
Sum of absolute differences |
|
Locate template in image |
|
Sum of squared differences |
|
Zero-mean normalized cross correlation |
|
Zero-mean sum of absolute differences |
|
Zero-mean sum of squared differences |
Shape changing#
Changing the shape of an Image instance.
|
Decimate an image |
|
Dice an image into a grid of subimages |
|
Pad the edges of the image |
|
Replicate image pixels |
|
Automatic image trimming |
|
Scale an image |
|
Trim pixels from the edges of the image |
Image distortion#
Distorting the image within an Image instance.
|
Image warping |
|
Roll image by row or column |
|
Rotate an image |
|
Rotate a spherical image |
|
Undistort image |
|
Image warping |
|
Affine warp of image |
|
Perspective warp |
Multiview operations#
Stereo image processing, rectification, and display.
|
Refine disparity from disparity space image |
|
Create rectification homographies |
|
Stereo block matching |
|
Stereo semi-global block matching |
|
Simple stereo matching |
Operators#
Binary arithmetic and relational operators#
Image☆Image
Image☆ scalarscalar ☆
Image
The result is always an Image.
For the first case, the images must have:
the same shape,
the same width and height but can have different number of color planes. The image with one plane is broadcast across the color planes of the other image.
A scalar value is broadcast across the whole image.
Arithmetic and bitwise logical operations can be performed elementwise on:
|
Overloaded |
|
Overloaded |
|
Overloaded |
|
Overloaded |
|
Overloaded |
|
Overloaded |
|
Overloaded unary |
|
Overloaded |
|
Overloaded |
|
|
|
|
|
|
|
Overloaded |
|
|
|
|
|
Overloaded |
|
Overloaded |
|
Overloaded |
Logical operations can be performed elementwise on: Image ☆ Image.
The result is always an Image with boolean pixel values:
|
Overloaded |
|
Overloaded |
|
Overloaded |
|
Overloaded |
|
Overloaded |
|
Overloaded |
Inplace arithmetic operators#
Arithmetic and bitwise logical operations can be performed elementwise on:
Image☆=Image
Image☆= scalar
The result is always an Image. A scalar value is broadcast across the whole image.
|
Overloaded in-place |
|
Overloaded in-place |
|
Overloaded in-place |
|
Overloaded in-place |
|
Overloaded in-place |
|
Overloaded in-place |
|
Overloaded in-place |
|
Overloaded in-place |
|
Overloaded in-place |
|
Overloaded in-place |
Plane stacking operators#
Stacking operations can be performed on multiple Image instances.
A scalar value is broadcast across the whole image to create a new Image instance.
In place stacking allows for planes to be appended.
|
Overloaded in-place |
|
Overloaded |
Image statistics#
maxMaximum value of all pixels
meanMean value of all pixels
medianMedian value of all pixels
minMinimum value of all pixels
statsPixel value statistics
stdStandard deviation of all pixels
varVariance of all pixels
histImage histogram
Image feature extraction#
Whole image features#
Histograms#
|
Image histogram |
Image moments#
|
Hu image moment invariants |
|
Image moments |
|
Image moments |
|
Normalized central image moments |
|
Central image moments |
Other#
|
Find non-zero pixel values as 1D indices |
|
Find non-zero pixel values as 2D coordinates |
|
Otsu threshold selection |
|
Find local maxima in image |
|
Sum of all pixels |
Region features#
Find homogeneous regions, text or fiducual tags.
|
Find and describe blobs in image |
|
Find MSER features in image |
|
Optical character recognition |
Fiducial features#
|
Find fiducial markers in image |
Line features#
Find lines in an image.
|
Find Hough line features |
Point/corner features#
Find distincitive points in an image.
|
Find AKAZE features in image |
|
Find BRISK features in image |
|
Combination feature detector and descriptor |
|
Find Harris features in image |
|
Find ORB features in image |
|
Find SIFT features in image |
Image i/o#
File#
|
Get image EXIF metadata |
|
Read image from file |
|
Write image to file |
Graphical#
|
Display image |
|
Display image with pixel values |
Text#
|
Print image pixels in compact format |
|
Format several small images concatenated horizontally |
Constant images#
Create images that are constant, random, or have a simple geometric pattern.
|
Create chequerboard pattern |
|
Create image containing grid of circles |
|
Create image with all pixels having same value |
|
Create an image containing filled polygons |
|
Create image of linear ramps |
|
Create image with random pixel values |
|
Create image of sinusoidal intensity pattern |
|
Create image containing grid of squares |
|
Create a small image from text string |
|
Create image with zero value pixels |
Graphical annotation#
Render simple graphical annotations into an image. The equivalent functions plot_xxx
from SpatialMath Toolbox create graphical overlays rather than changing the the image data.
|
Draw box into image |
|
Draw circle into image |
|
Draw label box into image |
|
Draw line into image |
|
Draw a marker in image |
|
Draw text into image :param pos: text position (u,v) :type pos: array_like(2) :param text: text to draw :type text: str :param kwargs: parameters passed to |
Test images#
Sometimes, for pedagogy and unit tests, it is helpful to create, process and numerically display small example images.