Image.__getitem__#

Image.__getitem__(keys: int | str | tuple[slice, slice] | tuple[slice, slice, slice]) → Any[source][source]#

Return pixel value or slice from image

Parameters:

keys (int, str, tuple of int or slice) – slices to extract

Returns:

slice of image

Return type:

Image

This is a Swiss-army knife method for accessing subregions of an Image by uv-region and/or plane. A key can be:

  • a 2-tuple of integers, eg. img[u,v], return this pixel as a scalar. If the image has multiple planes, the result is an ndarray over planes.

  • a 3-tuple of integers, eg, img[u,v,p], for a multiplane image return this pixel from specified plane as a scalar.

  • a 2-tuple containing at least one slice object, eg. img[100:110, 200:300], return this region as an Image. If the image has multiple planes, the result is a multiplane Image.

  • a 3-tuple containing at least one slice objects, return this region of uv and planes as an Image with one or more planes.

  • an int, return this plane as an Image.

  • a string, return this named plane or planes as an Image. A string of plane names without colons, such as "BGR", is read one letter per plane; use colons, such as "a*:b*", for names longer than one letter. The result has its planes, and its colororder, in the order given.

Any other key raises a ValueError. In particular a lone slice such as img[1:3] is not valid, because it could mean columns, rows or planes: give the column and row slices, for example img[1:3, :]. To reverse the planes, or reverse the order of the columns or rows, use slices with a negative step, for example img[:, :, ::-1].

Examples

>>> from machinevisiontoolbox import Image
>>> img = Image.Read("flowers4.png")
>>> img.colororder_str
'R:G:B'
>>> red = img[0] # red plane
>>> red # greyscale image
Image(size=(640, 426), dtype=uint8)
>>> green = img["G"]
>>> green # greyscale image
Image(size=(640, 426), dtype=uint8)
>>> bgr = img["BGR"] # planes reordered
>>> bgr.colororder_str
'B:G:R'
>>> bgr = img[:, :, ::-1] # the same, using a negative step
>>> bgr.colororder_str
'B:G:R'
>>> roi = img[100:200, 300:400]
>>> roi # color image
Image(size=(100, 100), dtype=uint8, nplanes=3, colororder=R:G:B)
>>> roi = img[100:200, 300:400, 1:]
>>> roi # 2-plane color image
Image(size=(100, 100), dtype=uint8, nplanes=2, colororder=G:B)
>>> roi = img[100, :]  # column 100
>>> roi # color image
Image(size=(1, 426), dtype=uint8, nplanes=3, colororder=R:G:B)
>>> pix = img[100, 200, 1] # green pixel at (100,200) as scalar
>>> pix
np.uint8(105)
>>> pix = img[100, 200] # RGB vector at (100,200) as ndarray
>>> pix
array([ 79, 105,  55], dtype=uint8)

Note

If the result is a single row or column the result is a 1xn or nx1 Image instance. If the result is a single plane the result is a greyscale image.

Note

Indexing pixel values this way is slow, use pixel for faster access, or img.A[v,u] for direct access to the underlying NumPy array.

Note

The result is a view that shares its pixels with img when the key is a plane number, a tuple of slices, or a string naming one plane, so changing one changes the other. It is a new array when the string names several planes. Either way the result may not be C-contiguous: a plane of a color image, a region of interest and a reversed slice are all strided, and a string such as "BGR" gives a planar layout. The draw_* methods and OpenCV functions that write into an image refuse such an array. Use Image(img[...].A, copy=True), or plane with copy=True, to make a contiguous copy that shares nothing with img. See Image.

Warning

The order of the indices is column, row and plane. This is the opposite of the order used for NumPy index on the underlying array. It is consistent with the column-first convention used across the Toolbox and is consistent with the \((u,v)\) coordinate system for images.

Changed in version 1.0.0: The order of the indices changed to column, row, plane. Previously it was row, column, plane.