topostats.grains#

Find grains in an image.

Attributes#

Classes#

Grains

Find grains in an image.

Module Contents#

topostats.grains.LOGGER#
class topostats.grains.Grains(image: numpy.ndarray, filename: str, pixel_to_nm_scaling: float, threshold_method: str = None, otsu_threshold_multiplier: float = None, threshold_std_dev: dict = None, threshold_absolute: dict = None, absolute_area_threshold: dict = None, direction: str = None, smallest_grain_size_nm2: float = None, remove_edge_intersecting_grains: bool = True)[source]#

Find grains in an image.

image#
filename#
pixel_to_nm_scaling#
threshold_method#
otsu_threshold_multiplier#
threshold_std_dev#
threshold_absolute#
absolute_area_threshold#
direction#
smallest_grain_size_nm2#
remove_edge_intersecting_grains#
thresholds = None#
images#
directions#
minimum_grain_size = None#
region_properties#
bounding_boxes#
grainstats = None#
tidy_border(image: numpy.array, **kwargs) numpy.array[source]#

Remove grains touching the border.

Parameters:

image (np.array) – Numpy array representing image.

Returns:

Numpy array of image with borders tidied.

Return type:

np.array

label_regions(image: numpy.array) numpy.array[source]#

Label regions.

This method is used twice, once prior to removal of small regions, and again afterwards, hence requiring an argument of what image to label.

Parameters:

image (np.array) – Numpy array representing image.

Returns:

Numpy array of image with objects coloured.

Return type:

np.array

calc_minimum_grain_size(image: numpy.ndarray) float[source]#

Calculate the minimum grain size in pixels squared.

Very small objects are first removed via thresholding before calculating the below extreme.

remove_noise(image: numpy.ndarray, **kwargs) numpy.ndarray[source]#

Remove noise which are objects smaller than the ‘smallest_grain_size_nm2’.

This ensures that the smallest objects ~1px are removed regardless of the size distribution of the grains.

Parameters:

image (np.ndarray) – 2D Numpy image to be cleaned.

Returns:

2D Numpy array of image with objects < smallest_grain_size_nm2 removed.

Return type:

np.ndarray

remove_small_objects(image: numpy.array, **kwargs)[source]#

Remove small objects from the input image.

Threshold determined by the minimum grain size, in pixels squared, of the classes initialisation.

Parameters:

image (np.ndarray) – 2D Numpy image to remove small objects from.

Returns:

2D Numpy array of image with objects < minimum_grain_size removed.

Return type:

np.ndarray

area_thresholding(image: numpy.ndarray, area_thresholds: list)[source]#

Remove objects larger and smaller than the specified thresholds.

Parameters:
  • image (np.ndarray) – Image array where the background == 0 and grains are labelled as integers > 0.

  • area_thresholds (list) – List of area thresholds (in nanometres squared, not pixels squared), first should be the lower limit for size, and the second should be the upper limit for size.

Returns:

Image where grains outside the thresholds have been removed, as a re-numbered labeled image.

Return type:

np.ndarray

colour_regions(image: numpy.array, **kwargs) numpy.array[source]#

Colour the regions.

Parameters:

image (np.array) – Numpy array representing image.

Returns:

Numpy array of image with objects coloured.

Return type:

np.array

static get_region_properties(image: numpy.array, **kwargs) list[source]#

Extract the properties of each region.

Parameters:

image (np.array) – Numpy array representing image

Returns:

List of region property objects.

Return type:

List

get_bounding_boxes(direction) dict[source]#

Derive a list of bounding boxes for each region from the derived region_properties.

Parameters:

direction (str) – Direction of threshold for which bounding boxes are being calculated.

Returns:

Dictionary of bounding boxes indexed by region area.

Return type:

dict

find_grains()[source]#

Find grains.