Util¶
- apply_mask_image(img, mask)[source]¶
Mask image with the provided binary mask.
- Parameters
img (PIL.Image.Image) – Input image
mask (np.ndarray) – Binary mask
- Returns
Image with the mask applied
- Return type
PIL.Image.Image
- lazyproperty(f)[source]¶
Decorator like @property, but evaluated only on first access.
Like @property, this can only be used to decorate methods having only a self parameter, and is accessed like an attribute on an instance, i.e. trailing parentheses are not used. Unlike @property, the decorated method is only evaluated on first access; the resulting value is cached and that same value returned on second and later access without re-evaluation of the method.
Like @property, this class produces a data descriptor object, which is stored in the __dict__ of the class under the name of the decorated method (‘fget’ nominally). The cached value is stored in the __dict__ of the instance under that same name.
Because it is a data descriptor (as opposed to a non-data descriptor), its __get__() method is executed on each access of the decorated attribute; the __dict__ item of the same name is “shadowed” by the descriptor.
While this may represent a performance improvement over a property, its greater benefit may be its other characteristics. One common use is to construct collaborator objects, removing that “real work” from the constructor, while still only executing once. It also de-couples client code from any sequencing considerations; if it’s accessed from more than one location, it’s assured it will be ready whenever needed.
A lazyproperty is read-only. There is no counterpart to the optional “setter” (or deleter) behavior of an @property. This is critically important to maintaining its immutability and idempotence guarantees. Attempting to assign to a lazyproperty raises AttributeError unconditionally. The parameter names in the methods below correspond to this usage example:
class Obj(object): @lazyproperty def fget(self): return 'some result' obj = Obj()
Not suitable for wrapping a function (as opposed to a method) because it is not callable.
- Parameters
f (Callable[[...], Any]) –
- method_dispatch(func)[source]¶
Decorator like @singledispatch to dispatch on the second argument of a method.
It relies on @singledispatch to return a wrapper function that selects which registered function to call based on the type of the second argument.
This is implementation is required in order to be compatible with Python versions older than 3.8. In the future we could use
functools.singledispatchmethod
.Source: https://stackoverflow.com/a/24602374/7162549
- Parameters
func (Callable[..., Any]) – Method to dispatch
- Returns
Selected method
- Return type
Callable[…, Any]
- np_to_pil(np_img)[source]¶
Convert a NumPy array to a PIL Image.
- Parameters
np_img (np.ndarray) – The image represented as a NumPy array.
- Returns
The image represented as PIL Image
- Return type
PIL.Image.Image
- random_choice_true_mask2d(binary_mask)[source]¶
Return a random pair of indices (column, row) where the
binary_mask
is True.- Parameters
binary_mask (np.ndarray) – Binary array.
- Returns
Random pair of indices (column, row) where the
binary_mask
is True.- Return type
Tuple[int, int]
- rectangle_to_mask(dims, vertices)[source]¶
Return a binary mask with True inside of rectangle
vertices
and False outside.The returned mask has shape
dims
.- Parameters
dims (Tuple[int, int]) – (rows, columns) of the binary mask
vertices (CoordinatePair) – CoordinatePair representing the upper left and bottom right vertices of the rectangle
- Returns
Binary mask with True inside of the rectangle, False outside.
- Return type
np.ndarray
- region_coordinates(region)[source]¶
Extract bbox coordinates from the region.
- Parameters
region (Region) – Region from which to extract the coordinates of the bbox
- Returns
Coordinates of the bbox
- Return type
CoordinatePair
- regions_from_binary_mask(binary_mask)[source]¶
Calculate regions properties from a binary mask.
- Parameters
binary_mask (np.ndarray) – Binary mask from which to extract the regions
- Returns
Properties for all the regions present in the binary mask
- Return type
List[Region]
- regions_to_binary_mask(regions, dims)[source]¶
Create a binary mask given a list of
regions
.For each region
r
, the areas withinr.coords
are filled with True, False outside.- Parameters
regions (List[Region]) – The regions to create the binary mask.
dims (Tuple[int, int]) – Dimensions of the resulting binary mask.
- Returns
Binary mask from the
regions
coordinates.- Return type
np.ndarray
- scale_coordinates(reference_coords, reference_size, target_size)[source]¶
Compute the coordinates corresponding to a scaled version of the image.
- Parameters
reference_coords (CoordinatePair) – Coordinates referring to the upper left and lower right corners respectively.
reference_size (tuple of int) – Reference (width, height) size to which input coordinates refer to
target_size (tuple of int) – Target (width, height) size of the resulting scaled image
- Returns
coords – Coordinates in the scaled image
- Return type
CoordinatesPair
- threshold_to_mask(img, threshold, relate)[source]¶
Mask image with pixel according to the threshold value.
- Parameters
img (PIL.Image.Image) – Input image
threshold (float) – The threshold value to exceed.
relate (callable operator) – Comparison operator between img pixel values and threshold
- Returns
Boolean NumPy array representing a mask where a pixel has a value True if the corresponding input array pixel exceeds the threshold value. if the corresponding input array pixel exceeds the threshold value.
- Return type
np.ndarray