xrf_explorer.server.color_segmentation package¶
Submodules¶
xrf_explorer.server.color_segmentation.color_seg module¶
- calculate_color_difference(lab1: ndarray, lab2: ndarray) int [source]¶
Returns the Euclidean distance between two LAB colors.
- Parameters:
lab1 – color 1
lab2 – color 2
- Returns:
The distance
- combine_bitmasks(bitmasks: list[ndarray]) ndarray [source]¶
Merges array of bitmasks into single bitmask, by setting the Green value of each pixel to store the index of the corresponding bitmask.
- Parameters:
bitmasks – the bitmasks corresponding to each cluster
- Returns:
a single bitmask in the form of an image corresponding to the combination of all bitmasks
- convert_to_hex(clusters: ndarray) ndarray [source]¶
Converts clusters to hex format.
- Parameters:
clusters – the list of clusters in rgb format
- Returns:
clusters in hex format
- get_clusters_using_k_means(data_source: str, image_name: str, k: int = 30, nr_of_attempts: int = 10) tuple[ndarray, list[ndarray]] [source]¶
Extract the color clusters of the RGB image using the k-means clustering method in OpenCV
- Parameters:
data_source – the name of the data source
image_name – the name of the image to apply k-means on
k – number of clusters required at end. Defaults to 30
nr_of_attempts – the number of times the algorithm is executed using different initial labellings. Defaults to 10
- Returns:
an array of labels of the clusters, the array of colors of clusters, and the array of bitmasks
- get_elemental_clusters_using_k_means(data_source: str, image_name: str, elemental_channel: int, elem_threshold: float = 0.1, k: int = 30, nr_of_attempts: int = 10) tuple[ndarray, list[ndarray]] [source]¶
Extract the color clusters of the RGB image per element using the k-means clustering method in OpenCV
- Parameters:
data_source – the name of the data source
image_name – the name of the image to apply k-means on
elemental_channel – channel of the element to compute the color clusters of
elem_threshold – minimum concentration needed for an element to be present in the pixel
k – number of clusters required at end. Defaults to 30
nr_of_attempts – the number of times the algorithm is executed using different initial labellings. Defaults to 10
- Returns:
a dictionary with an array of clusters and one with an array of bitmasks for each element
- image_to_lab(image: ndarray) ndarray [source]¶
Turns an image of RGB triples into an image of LAB triples.
- Parameters:
image – The image in RGB format (range: [0, 255])
- Returns:
The image in LAB format
- image_to_rgb(image: ndarray) ndarray [source]¶
Turns an image of LAB triples into an image of RGB triples.
- Parameters:
image – The image in LAB format (range: L = [0,100], AB = [-127,127])
- Returns:
The image in RGB format
- lab_to_rgb(lab_color: ndarray) ndarray [source]¶
Returns the RGB equivalent of an LAB color.
- Parameters:
lab_color – The LAB color triple
- Returns:
The RGB color
- merge_similar_colors(clusters: ndarray, bitmasks: ndarray, threshold: int = 7) tuple[ndarray, ndarray] [source]¶
Go over every pair of clusters and merge the pair if they are similar according to the threshold. Currently unused function, left in code in case it becomes useful in the future.
- Parameters:
clusters – the currently available clusters
bitmasks – the bitmasks corresponding to the different clusters
threshold – the threshold that indicates how similar the colors have to be in order to be merged in a cluster
- Returns:
the new bitmask with potentially merged clusters and the new clusters
- reshape_image(small_image: ndarray) ndarray [source]¶
Reshape image into 2D array where each row represents a pixel and each pixel is represented as a 3-element array containing the RGB values.
- Parameters:
small_image – the resized image to reshape
- Returns:
the reshaped image
- rgb_to_hex(r: int, g: int, b: int) str [source]¶
Turns a rgb triple into hex format.
- Parameters:
r – the red value
g – the green value
b – the blue value
- Returns:
the hex format
- rgb_to_lab(rgb_triple: ndarray) ndarray [source]¶
Returns the LAB equivalent of an RGB color.
- Parameters:
rgb_triple – The RGB color triple
- Returns:
the LAB format
- save_bitmask_as_png(bitmask: ndarray, full_path: str) bool [source]¶
Saves the given bitmask as a png with the given name in the given path.
- Parameters:
bitmask – the bitmask to be saved as png
full_path – the path (including image name) to save the file to
- Returns:
true if the file was successfully saved, false otherwise
xrf_explorer.server.color_segmentation.helper module¶
Module contents¶
This module handles everything related to color segmentation.