BaseDistance

Qualified name: CDC.color_models.BaseDistance

class BaseDistance(*, reference_pixels, bands_to_use=None, transform=None)[source]

Bases: ABC

Base class for all color distance models. Can be used to create new methods for calculating the color distance. Sub classes must implement calculate_distance(), calculate_statistics() and show_statistics().

Parameters:
  • reference_pixels (np.ndarray) – Pixels to use as a reference

  • bands_to_use (tuple[int, ...] | list[int] | None) – A list of indexes to choose which “colors” are used in distance calculations

  • transform (BaseTransform | None) – A transform to apply to the images before the distance is calculated.

Methods

calculate_distance

Calculate the color distance for each pixel in the image to the reference.

calculate_statistics

Calculate the necessary statistics for performing the distance calculation, i.e. the covariance and average or similar.

from_image_annotation

Create a class instance from a reference image and an annotated mask.

from_pixel_values

Create a class instance from a list of pixel values before bands_to_use and transforms are applied.

get_bands_to_use

Get correct bands to use from supplied alpha channel number and the number of bands in input.

save_pixel_values

Save raw, transformed and selected bands reference pixels to csv files.

save_pixel_values_to_file

Save pixel values to csv file with tab delimiter.

show_statistics

Print the statistics to screen.

Attributes

color_values

Reference pixel values

color_values_raw

Raw pixel values as reference

color_values_transformed

Transformed reference pixel values

covariance

Covariance of the reference pixels.

average

Average of the reference pixels.

classmethod from_image_annotation(*, reference, annotated, bands_to_use=None, alpha_channel=-1, transform=None, **kwargs)[source]

Create a class instance from a reference image and an annotated mask.

Parameters:
Return type:

BaseDistance

classmethod from_pixel_values(*, pixel_values, bands_to_use=None, alpha_channel=-1, transform=None, **kwargs)[source]

Create a class instance from a list of pixel values before bands_to_use and transforms are applied.

Parameters:
Return type:

BaseDistance

abstractmethod calculate_distance(image)[source]

Calculate the color distance for each pixel in the image to the reference. Subclasses must implement this and call super() as the first thing to apply band selection and transforms.

Parameters:

image (ndarray)

Return type:

ndarray

abstractmethod calculate_statistics()[source]

Calculate the necessary statistics for performing the distance calculation, i.e. the covariance and average or similar. Must be implemented by subclasses.

Return type:

None

get_bands_to_use(alpha_channel, number_of_bands)[source]

Get correct bands to use from supplied alpha channel number and the number of bands in input.

Parameters:
  • alpha_channel (int | None)

  • number_of_bands (int)

Return type:

None

save_pixel_values(output_location, channel_names_in=None, channel_names_out=None)[source]

Save raw, transformed and selected bands reference pixels to csv files.

Parameters:
  • output_location (Path)

  • channel_names_in (str | None)

  • channel_names_out (str | None)

Return type:

None

save_pixel_values_to_file(filename, values, header=None, raw=True)[source]

Save pixel values to csv file with tab delimiter.

Parameters:
Return type:

None

abstractmethod show_statistics()[source]

Print the statistics to screen. Subclasses must implement this and print the calculated statistics to screen.

Return type:

None

average: float | None

Average of the reference pixels.

color_values: ndarray

Reference pixel values

color_values_raw: ndarray | None

Raw pixel values as reference

color_values_transformed: ndarray | None

Transformed reference pixel values

covariance: ndarray | None

Covariance of the reference pixels.