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 = {'upper': [None, None], 'lower': [None, None]}, direction: str = None, absolute_smallest_grain_size: float = None)[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#
absolute_smallest_grain_size#
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.

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

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

Removes noise which are objects smaller than the ‘absolute_smallest_grain_size’.

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 < absolute_smallest_grain_size removed.

Return type:

np.ndarray

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

Remove small objects.

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

Removes 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, first should be the lower threshold, second upper threshold.

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.