Image.pixels_mask#

Image.pixels_mask(mask: Image | Polygon2 | list[Polygon2], coords: bool = False, return_mask: bool = False)[source][source]#

Return pixel values at locations specified by a mask

Parameters:
  • mask (Image, a single Polygon2 or a list of Polygon2) – the selection mask as either non-zero pixels in a 2D image or the area covered by a polygon or list of polygons

  • coords (bool, optional) – include pixel coordinates in output, defaults to False

  • return_mask (bool, optional) – also return the mask as an Image, defaults to False

Returns:

array of pixel values and optionally coordinates of pixels selected by the mask, optionally also the mask as an Image

Return type:

Array2d, Array2d and Image

For an image with P planes and a mask that selects N pixels, return an PxN array of pixel values. If coords is True, the result is an (P+2)xN array where the first two rows are the u and v coordinates of the selected pixels.

Example:

>>> from machinevisiontoolbox import Image
>>> from spatialmath import Polygon2
>>> img = Image.Read("flowers4.png")
>>> polygon = Polygon2([(300, 400), (360, 400), (330, 450)], close=True)
>>> pixels = img.pixels_mask(polygon, coords=True)
>>> pixels.shape
(5, 1196)
>>> pixels[:,:5] # first 5 pixels, with coordinates
array([[300, 301, 302, 303, 304],
       [400, 400, 400, 400, 400],
       [116, 112,  97,  87,  96],
       [174, 173, 158, 150, 152],
       [ 92,  82,  75,  65,  70]], shape=(5, 5))
Seealso:

pixel __getitem__ roi