pwspy.dataTypes.Roi

class pwspy.dataTypes.Roi(mask, verts)[source]

Bases: object

This class represents a single Roi used to select a specific region of an image. The Roi consists of a mask (a boolean array specifying which pixels are included in the Roi), a set of of vertices (a 2 x N array specifying the vertices of the polygon enclosing the mask, this is useful if you want to adjust the Roi later. Rather than calling the constructor directly you will generally create one of these objects through one of the class methods that construct one for you.

Parameters
  • mask (ndarray) – A 2D boolean array where the True values indicate pixels that are within the ROI.

  • verts (Union[ndarray, Polygon]) – Can be a sequence of 2D (x, y) coordinates indicating the border of the ROI or a shapely Polygon. If an array of coordinates is used then it will be converted to the shell of a shapely polygon internally. While this information is partially redundant with the mask it is useful for many applications and can be complicated to calculate from mask.

classmethod fromMask(mask)[source]

Use rasterio to create find the vertices of a mask. :type mask: ndarray :param mask: A boolean array. The mask have only one contiguous True region

Return type

Roi

Returns

A new instance of Roi

classmethod fromVerts(verts, dataShape)[source]

Automatically generate the mask for an Roi using just the vertices of an enclosing polygon.

Parameters
  • verts (ndarray) – A sequence of 2D (x, y) coordinates indicating the border of the ROI.

  • dataShape (Tuple[float, float]) – A tuple giving the shape of the array that this Roi is associated with.

Return type

Roi

Returns

A new instance of Roi

Examples

myRoi = Roi.fromVerts(‘nucleus’, 1, polyVerts, (1024, 1024))

transform(matrix)[source]

Return a copy of this Roi that has been transformed by an affine transform matrix like the one returned by opencv.estimateRigidTransform. This can be obtained using the functions in the utility.machineVision module.

Parameters

matrix (ndarray) – A 2x3 numpy array representing an affine transformation.

Return type

Roi

Returns

A new instance of Roi representing this Roi after transformation.

property verts: numpy.ndarray

An array of vertices for the outer ring of the polygon. For most ROIs they only have an outer ring anyway.

Return type

ndarray