MaskProxy

class Stoner.Image.attrs.MaskProxy(*args)[source]

Bases: object

Provides a wrapper to support manipulating the image mask easily.

The actual mask of a Stonmer.ImageFile is held by the mask attribute of the underlying numpy.ma.MaskedArray, but this class implements an attribute for an ImageFile that not only provides a means to index into the mask data, but supported other operations too.

color

This defines the colour of the mask that is used when showing a masked image in a window. It can be a named colour - such as red, blue etc. or a tuple of 3 or 4 numbers between 0 and 1 - which define an RGB(Alpha) colour. Note that the Alpha channel is actually an opacity channel = so 1.0 is solid and 0 is transparent.

Type:

matplotlib colour

data

This accesses the actual boolean masked array if it is necessary to get at the full array. It is equivalent to .mask[:]

Type:

numpy array of bool

image

This is a synonym for MaskProxy.data.

Type:

numpoy array of bool

draw

This allows the mask to drawn on like the image. This is particularly useful as it provides a convenient programmatic way to define regions of interest in the mask with simply geometric shapes.

Type:

DrawProxy

Indexing of the MaskProxy simply passes through to the underlying mask data - thus getting, setting and deleting element directly changes the mask data.

The string representation of the mask is an ascii art version of the mask where . is unmasked and X is masked.

Conversion to a boolean is equaivalent to testing whether any elements of the mask are True.

The mask also supports the invert and negate operators which both return the inverse of the mask (but do not change the mask itself - unlike MaskProxy.invert()).

For rich displays, the class also supports a png representation which is simply a black and white version of the mask with black pixels being masked elements and white unmasked elements.

Attributes Summary

colour

Get the colour of the mask.

data

Get the underlying data as an array - compatibility accessor.

draw

Access the draw proxy object.

image

Get the underlying data as an array - compatibility accessor.

Methods Summary

clear()

Clear a mask.

invert()

Invert the mask.

select(**kargs)

Interactive selection mode.

threshold([thresh])

Mask based on a threshold.

Attributes Documentation

colour

Get the colour of the mask.

data

Get the underlying data as an array - compatibility accessor.

draw

Access the draw proxy object.

image

Get the underlying data as an array - compatibility accessor.

Methods Documentation

clear()[source]

Clear a mask.

invert()[source]

Invert the mask.

select(**kargs)[source]

Interactive selection mode.

This method allows the user to interactively choose a mask region on the image. It will require the Matplotlib backen to be set to Qt or other non-inline backend that supports a user vent loop.

The image is displayed in the window and athe user can interact with it with the mouse and keyboard.

  • left-clicking the mouse sets a new vertex

  • right-clicking the mouse removes the last set vertex

  • pressing “i” inverts the mask (i.e. controls whether the shape the user is drawing is masked or clear)

  • pressing “p” sets polygon mode (the default) - each vertex is then the corener of a polygon. The polygon vertices are defined in order going around the shape.

  • pressing “r” sets rectangular mode. The first vertex defined is one corner. With only two vertices the rectangle is not-rotated and the two vertices define opposite corners. If three vertices are defined then the first two form one side and then third vertex controls the extent of the rectangle in the direction perpendicular to the side defined.

  • pressing “c” sets circle/ellipse mode. The first vertex defines one point on the circumference of the circle, the next point will define a point on the opposite side of the circumference. If three vertices are defined then a circle that passes through all three of them is used. Defining 4 vertices causes the mode to attempt to find the non-rotated ellipse through the points and further vertices allows the ellipse to be rotated.

This method directly sets the mask and then returns a copy of the parent Stoner.ImageFile.

threshold(thresh=None)[source]

Mask based on a threshold.

Keyword Arguments:

thresh (float) – Threshold to apply to the current image - default is to calculate using threshold_otsu