machinevisiontoolbox.Image.__getitem__
- Image.__getitem__(keys: int | str | tuple[slice, slice] | tuple[slice, slice, slice])[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. Akey
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 anImage
. If the image has multiple planes, the result is a multiplaneImage
.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
.
Example:
>>> from machinevisiontoolbox import Image >>> img = Image.Read("flowers4.png") # in RGB order >>> red = img[0] # red plane >>> red # greyscale image Image: 640 x 426 (uint8) >>> green = img["G"] >>> green # greyscale image Image: 640 x 426 (uint8) >>> roi = img[100:200, 300:400] >>> roi # color image Image: 100 x 100 (uint8), R:G:B >>> roi = img[100:200, 300:400, 1:] >>> roi # 2-plane color image Image: 100 x 100 (uint8), G:B >>> roi = img[100, :] # column 100 >>> roi # color image Image: 1 x 426 (uint8), R:G:B >>> pix = img[100, 200, 1] # green pixel at (100,200) as scalar >>> pix 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(u,v)
for faster access, orimg.A[v,u]
for direct access to the underlying NumPy array.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.
Added in version 1.0.0: The order of the indices changed to column, row, plane. Previously it was row, column, plane.