Welcome to cie-utils’s documentation!¶
Module Reference¶
- cie_utils.blur_img(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]]) ndarray[tuple[int, ...], dtype[float64]] [source]¶
Applies a Gaussian filter to an image.
Uses skimage.filters.gaussian with a sigma of 1 and truncate of 1, resulting in a 3x3 Gaussian kernel.
- Parameters:
image (npt.NDArray) – The input image. Pixel values are expected to be in the range [0.0, 1.0] (float) or [0, 255] (uint8). The skimage.filters.gaussian function automatically handles data types.
- Returns:
The image with the Gaussian filter applied. The output will be of type float64. If the input was RGB, the output will also be RGB.
- Return type:
npt.NDArray[np.float64]
- cie_utils.clahe_img(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]], illumination_axis: int, top_val: float) ndarray[tuple[int, ...], dtype[_ScalarType_co]] [source]¶
Applies Contrast Limited Adaptive Histogram Equalization (CLAHE) to the image, specifically to the illumination channel.
The illumination channel is scaled to [0, 255] before applying CLAHE, and then rescaled back to its original range [0, top_val].
- Parameters:
image (npt.NDArray) – The input image. Expected to be an image in a color space where one channel represents illumination (e.g., the L channel of LAB).
illumination_axis (int) – The index of the channel representing illumination in the image (e.g., 0 for L in LAB).
top_val (float) – The maximum value the illumination channel can take in its original range (e.g., 100 for L in LAB).
- Returns:
The image with CLAHE applied to the illumination channel, maintaining float64 data type.
- Return type:
npt.NDArray
- cie_utils.classifier_model(clf_model: str, params: tuple, order: str = 'by_pixel', n_ref: int = 1) tuple[Any, Any] [source]¶
Applies a classification model (K-means, GaussianMixture, or AgglomerativeClustering) to images and sorts the results.
- Parameters:
clf_model ({"Kmeans", "GaussianMixture", "AgglomerativeClustering"}) – The type of classification model to use.
params (tuple) –
Specific parameters for the classification model.
For “Kmeans”: (img, k, stop_criteria, number_of_attempts, centroid_initialization_strategy)
For “GaussianMixture”: (img, k)
For “AgglomerativeClustering”: (img, k, linkage)
img can be a single image or a list of images.
order ({"by_pixel", "reference_values"}, optional) – Method to sort the resulting clusters. - “by_pixel”: sorts by the number of pixels in each cluster (descending). - “reference_values”: sorts by the closeness of centroids to reference LAB values. Defaults to “by_pixel”.
n_ref (int, optional) – Reference number (0 or 1) to select the set of reference LAB values when order is “reference_values”. Defaults to 1.
- Returns:
A tuple with:
- centerslist of ndarray
The sorted cluster centroids (or means) for each image.
- ordered_imageslist of list of ndarray
A list of lists, where each sublist contains the segmented images sorted by cluster for the corresponding image.
- Return type:
tuple of list
- Raises:
ValueError – If the clf_model classifier type is invalid.
- cie_utils.create_rgb_spectrum(num_steps: int) list[int] [source]¶
Creates an RGB color spectrum by varying the Hue in HSV space.
The spectrum goes from a hue close to red (0.75) to red (0), keeping saturation and value at maximum.
- Parameters:
num_steps (int) – The number of steps (colors) to generate in the spectrum.
- Returns:
A list of NumPy arrays, where each array represents an RGB color ([R, G, B]) with integer values in the range [0, 255].
- Return type:
list[int]
- cie_utils.display_2d_scatter_plot(imgs: list[ndarray[tuple[int, ...], dtype[float64]]], cols: int, fsize: tuple[int, int] = (10, 2), titles: list[str] | None = None) list[ndarray[tuple[int, ...], dtype[float64]]] [source]¶
Displays 2D scatter plots of pixel frequency for a list of images.
Each scatter plot shows pixel values (x-axis) against their frequency of occurrence (number of pixels with that value, y-axis). Only non-zero pixel values are considered.
- Parameters:
imgs (list[npt.NDArray[np.float64]]) – A list of images (or image channels) for which to generate scatter plots.
cols (int) – The number of columns in the plot display grid.
fsize (tuple[int, int], optional) – The figure size (width, height) in inches. Defaults to (10, 2).
titles (list[str] | None, optional) – A list of titles for each scatter plot. If None, no titles are displayed. Defaults to None.
- Returns:
A list of NumPy arrays, where each array contains the frequency data of pixels ([pixel_value, frequency]) for each input image.
- Return type:
list[npt.NDArray[np.float64]]
- cie_utils.display_3d_scatter_plot(images: list[ndarray[tuple[int, ...], dtype[float64]]], colors: list[str] | None = None, title: str | None = None) None [source]¶
Creates a 3D scatter plot for classified data.
Each dataset in images is represented as a series of points in a 3D space, useful for visualizing cluster distribution.
- Parameters:
images (list[npt.NDArray[np.float64]]) – A list of NumPy arrays, where each array contains the coordinates (e.g., L, a, b from LAB) of the pixels in a cluster or group. Each array is expected to have 3 columns.
colors (list[str] | None, optional) – A list of color strings for each set of points. If None, colors are automatically selected. Defaults to None.
title (str | None, optional) – The title of the plot. Defaults to None.
- cie_utils.display_hist(imgs: list[ndarray[tuple[int, ...], dtype[float64]]], cols: int, fsize: tuple[int, int] = (10, 2), titles: list[str] | None = None, xlabel: list[str] = None, ylabel: list[str] = None, save_imgs: bool = False, save_csv: bool = False, output_dir: Path = PosixPath('/home/runner/work/cie-utils/cie-utils')) None [source]¶
Displays histograms and Probability Density Functions (PDFs) for a list of images.
- Parameters:
imgs (list[npt.NDArray[np.float64]]) – A list of images (or image channels) for which to generate histograms.
cols (int) – The number of columns in the histogram display grid.
fsize (tuple[int, int], optional) – The figure size (width, height) in inches. Defaults to (10, 2).
titles (list[str] | None, optional) – A list of titles for each histogram. If None, no titles are displayed. Defaults to None.
xlabel (list[str], optional) – A list of labels for the x-axis of each histogram. Defaults to [“x”] for all.
ylabel (list[str], optional) – A list of labels for the y-axis of each histogram. Defaults to [“y”] for all.
save_imgs (bool, optional) – If True, saves the histograms to a file instead of displaying them. The filename is based on the first title if available, otherwise “test” is used. Defaults to False.
save_csv (bool, optional) – If True, saves the density data (rng and density) of each histogram to a CSV file. Defaults to False.
output_dir (Path, optional) – The directory where the images will be saved if save_imgs is True. Defaults to the current working directory.
- cie_utils.display_images(imgs: list[ndarray[tuple[int, ...], dtype[float64]]], cols: int = 1, fsize: tuple[int, int] = (10, 5), cmaps: list[str] | None = None, titles: list[str] | None = None, save_imgs: bool = False, output_dir: Path = PosixPath('/home/runner/work/cie-utils/cie-utils')) None [source]¶
Displays a list of images in a grid.
- Parameters:
imgs (list[npt.NDArray[np.float64]]) – A list of images to display.
cols (int, optional) – The number of columns in the display grid. Defaults to 1.
fsize (tuple[int, int], optional) – The figure size (width, height) in inches. Defaults to (10, 5).
cmaps (list[str] | None, optional) – A list of colormaps to apply to each image. If None, no colormap is applied. Defaults to None.
titles (list[str] | None, optional) – A list of titles for each image. If None, no titles are displayed. Defaults to None.
save_imgs (bool, optional) – If True, saves the images to a file instead of displaying them. The filename is based on the first title if available, otherwise “test” is used. Defaults to False.
output_dir (Path, optional) – The directory where the images will be saved if save_imgs is True. Defaults to the current working directory.
- cie_utils.display_plot(imgs: list[ndarray[tuple[int, ...], dtype[float64]]], fsize: tuple[int, int] = (10, 2), cmaps: list[str] | None = None, titles: list[str] | None = None) None [source]¶
Displays a combination of image, histogram, and pixel frequency scatter plot for each image in the list.
For each image: 1. The original image is displayed. 2. A histogram and its Probability Density Function (PDF) are generated. 3. A scatter plot of pixel value frequency is created.
- Parameters:
imgs (list[npt.NDArray[np.float64]]) – A list of images (or image channels) to visualize.
fsize (tuple[int, int], optional) – The figure size (width, height) in inches. Defaults to (10, 2).
cmaps (list[str] | None, optional) – A list of colormaps to apply to each image. If None, no colormap is applied. Defaults to None.
titles (list[str] | None, optional) – A list of titles for each set of plots. If None, no titles are displayed. Defaults to None.
- cie_utils.enter_str_input(text: str) str [source]¶
Prompts the user for a text input and ensures it is not empty.
- Parameters:
text (str) – The message to display to the user.
- Returns:
The text string entered by the user.
- Return type:
str
- Raises:
ValueError – If the user’s input is None or an empty string.
- cie_utils.extract_segmentation(img_path: str, rimg_path: str, lbl_path: str, dest: str, dsfile: str) None [source]¶
Extracts image segments based on label information and a dataset file.
This function reads an image, segmentation coordinates from a label file, and category names from a YAML dataset file. It then creates masks, crops the regions of interest (ROI), and saves the segmented images and their references (if provided) into folders organized by category.
- Parameters:
img_path (str) – Absolute path to the image to be segmented.
rimg_path (str) – Absolute path to the reference image. Can be an empty string if no reference image.
lbl_path (str) – Absolute path to the text file with segmentation label information.
dest (str) – Absolute path to the destination folder where segmented images will be saved.
dsfile (str) – Absolute path to the dataset.yaml file containing category names.
- cie_utils.extract_segmentation_main() None [source]¶
Main function for image segmentation extraction.
Prompts the user for the necessary paths for the image, reference image, label file, and destination folder, then calls extract_segmentation.
- cie_utils.false_color_scale(channel: Any, color_list: Any, ranges_list: Any) Any [source]¶
Applies a false color scale to a single-channel image.
Assigns colors from a color_list to the pixels of channel based on the ranges specified in ranges_list.
- Parameters:
channel (Any) – The single-channel image to which the color scale will be applied. Expected to be a 2D NumPy array.
color_list (Any) – A list of RGB colors ([R, G, B]) to assign to the ranges.
ranges_list (Any) – A list of tuples or lists, where each tuple represents a range (min_val, max_val]. Pixel values within this range will be mapped to the corresponding color.
- Returns:
A new image with the false color scale applied, of integer type and 3 channels.
- Return type:
Any
- cie_utils.get_csv_data(now: str, data: ndarray[tuple[int, ...], dtype[float64]], filename: str, folder: Path, col_names: list[str] | None = None) None [source]¶
Saves data to a CSV file within a specific folder structure.
The save path is folder/csv/now/filename.csv.
- Parameters:
now (str) – A timestamp string to be used as a subfolder name within ‘csv’.
data (npt.NDArray[np.float64]) – The data to save, must be a 2D NumPy array.
filename (str) – The name of the CSV file (without the .csv extension).
folder (Path) – The Path object of the base folder where the ‘csv/now/’ structure will be created.
col_names (list[str] | None, optional) – A list of names for the CSV columns. If None, no column names will be used. Defaults to None.
- cie_utils.get_csv_data_from_images(imgs: list[~typing.Any], folder: ~pathlib.Path, col_names: list[str] | None = None, timezone=<DstTzInfo 'America/Bogota' LMT-1 day, 19:04:00 STD>) None [source]¶
Extracts pixel frequency data from a list of images and saves them to CSV files.
For each image in imgs, pixel frequencies are calculated (pixel values vs. number of occurrences) and saved to a CSV file. Files are organized by date and time within the specified folder.
- Parameters:
imgs (list[Any]) – A list of tuples where each tuple contains (image_name: str, image: npt.NDArray[np.float64]). The image must be a NumPy array. Pixels with zero value are ignored.
folder (Path) – The Path object of the base folder where the CSV file structure will be created.
col_names (list[str] | None, optional) – A list of names for the CSV columns (e.g., [“pixel_value”, “frequency”]). If None, no column names will be used. Defaults to None.
- cie_utils.get_pdf(img: ndarray[tuple[int, ...], dtype[float64]]) tuple[Any, Any, Any, int] [source]¶
Calculates the Probability Density Function (PDF) for image data.
This function filters non-zero pixels, calculates the number of bins using the Freedman-Diaconis rule, and estimates the PDF using Gaussian Kernel Density Estimation (KDE).
- Parameters:
img (npt.NDArray[np.float64]) – The input image as a NumPy array.
- Returns:
- datanpt.NDArray[np.float64]
The pixel data (non-zero values) of the image, rounded to one decimal place.
- rngnpt.NDArray[np.float64]
The range of values over which the density is calculated.
- densityAny
The density values of the estimated probability density function.
- binsint
The number of bins calculated using the Freedman-Diaconis rule.
- Return type:
tuple[npt.NDArray[np.float64], npt.NDArray[np.float64], Any, int]
- cie_utils.hsv_to_rgb(h: float, s: float, v: float) Any [source]¶
Converts a color from HSV to RGB.
HSV values are in the range [0, 1]. The RGB output is in the range [0, 255] and are integers.
- Parameters:
h (float) – Hue component, in the range [0, 1].
s (float) – Saturation component, in the range [0, 1].
v (float) – Value/Brightness component, in the range [0, 1].
- Returns:
A NumPy array of integers representing the color in RGB format [R, G, B], with values in the range [0, 255].
- Return type:
Any
- cie_utils.img_preparation(img: ndarray[tuple[int, ...], dtype[float64]], rimg: ndarray[tuple[int, ...], dtype[float64]], sd_val: float) list[ndarray[tuple[int, ...], dtype[float64]]] [source]¶
Prepares an image by applying normalization, background removal, and color space transformations.
The input image is normalized with a reference image, its background is removed using a standard deviation threshold, and then it is converted to CIELAB and CIELCh color spaces to extract their channels.
- Parameters:
img (npt.NDArray[np.float64]) – The input image in RGB format (float64 values).
rimg (npt.NDArray[np.float64]) – The reference image for normalization in RGB format (float64 values).
sd_val (float) – The standard deviation threshold value for background removal.
- Returns:
A list containing the L, a, b channels of the LAB image, and the C (chroma) and H (hue) channels of the LCh image. If the dimensions of img and rimg do not match, returns a list containing only the original input image.
- Return type:
list[npt.NDArray[np.float64]]
- cie_utils.min_max(img: ndarray[tuple[int, ...], dtype[float64]]) ndarray[tuple[int, ...], dtype[float64]] [source]¶
Normalizes an image using min-max scaling.
This method independently normalizes each color channel of the image to a range of [0, 1] based on the minimum and maximum values of each channel. It is sensitive to outliers.
- Parameters:
img (npt.NDArray[np.float64]) – The input image as a NumPy array of type float64, with 3 channels.
- Returns:
The normalized image in the range [0, 1].
- Return type:
npt.NDArray[np.float64]
- cie_utils.mix_images(img1: Any, img2: Any) Any [source]¶
Blends two images: an original image and a false-color image.
Pixels in the overlay image that are not completely black (i.e., do not have an RGB component sum equal to zero) replace the corresponding pixels in the original image. If a pixel in img2 is black, the original image’s pixel is retained.
- Parameters:
img1 (Any) – The original image (e.g., RGB). A NumPy array with 3 channels is expected.
img2 (Any) – The image to overlay. A NumPy array with 3 channels is expected.
- Returns:
The blended image, where non-black pixels from img2 have replaced pixels from img1.
- Return type:
Any
- cie_utils.normalize_img(img: ndarray[tuple[int, ...], dtype[float64]], rimg: ndarray[tuple[int, ...], dtype[float64]]) ndarray[tuple[int, ...], dtype[float64]] [source]¶
Normalizes an image based on a reference image.
Pixel values of the input image are divided by the corresponding values of the reference image. NaN values resulting from division by zero are replaced by zero, and values are clipped to the range [0, 1].
- Parameters:
img (npt.NDArray[np.float64]) – The image to normalize. Must be a NumPy array of type float64.
rimg (npt.NDArray[np.float64]) – The reference image for normalization. Must be a NumPy array of type float64.
- Returns:
The normalized image, with pixel values in the range [0, 1].
- Return type:
npt.NDArray[np.float64]
- cie_utils.pca_img(image: ndarray[tuple[int, ...], dtype[_ScalarType_co]], comp_num: int) ndarray[tuple[int, ...], dtype[float64]] [source]¶
Performs Principal Component Analysis (PCA) on the pixels of an image.
Transforms each pixel (interpreted as a point in a 3D space, e.g., RGB or LAB) to its 3 principal components.
- Parameters:
image (npt.NDArray) – The input image. A NumPy array with pixels expected in the last dimension (e.g., (height, width, channels)).
comp_num (int) – The number of principal components to retain.
- Returns:
A NumPy array representing the transformed image in PCA space, where each pixel now has 3 values corresponding to its principal components. The shape will be (N, 3) where N is the total number of pixels.
- Return type:
npt.NDArray[np.float64]
- cie_utils.plot_rgb_3d(images: Any, colors: Any, title: Any) None [source]¶
Generates a 3D scatter plot of RGB pixels.
Allows visualizing the distribution of colors in 3D space (R, G, B).
- Parameters:
images (Any | list[Any]) – An image or a list of images. Images should be NumPy arrays representing pixel data (e.g., RGB). If it’s a 3D image (height, width, 3), it will be flattened to (N, 3).
colors (Any | list[Any]) – The color or a list of colors for the points in the scatter plot. Must match the number of images if it’s a list. # Example: colors = [np.array([255, 0, 0]), np.array([0, 255, 0]), np.array([0, 0, 255])]
title (Any | list[Any]) – The title or a list of titles for the plot(s). Must match the number of images if it’s a list.
- cie_utils.rm_bg(img: ndarray[tuple[int, ...], dtype[float64]], sd_val: float, sdimg: Any | None = None) ndarray[tuple[int, ...], dtype[float64]] [source]¶
Removes the background from a normalized image based on a standard deviation value.
Pixels whose value (or the value in sdimg if provided) is less than or equal to sd_val are considered part of the background and are set to zero.
- Parameters:
img (npt.NDArray[np.float64]) – The normalized input image, of type float64.
sd_val (float) – The standard deviation threshold value. Pixels with a standard deviation below this value are considered background.
sdimg (Any | None, optional) – An optional standard deviation image to use as a mask. If None, the input image img is used. Defaults to None.
- Returns:
The image without background, where background pixels have been set to zero and values have been clipped to the range [0, 255].
- Return type:
npt.NDArray[np.float64]
- cie_utils.rm_bg2channel(chn: ndarray[tuple[int, ...], dtype[float64]], sd_val: float, sdimg: ndarray[tuple[int, ...], dtype[float64]] | None = None) ndarray[tuple[int, ...], dtype[float64]] [source]¶
Removes the background from an image channel based on a standard deviation value.
Pixels whose value in the channel (or the value in sdimg if provided) is less than or equal to sd_val are considered part of the background and are set to zero.
- Parameters:
chn (npt.NDArray[np.float64]) – The input image channel, of type float64.
sd_val (float) – The standard deviation threshold value. Pixels with a standard deviation below this value are considered background.
sdimg (npt.NDArray[np.float64] | None, optional) – An optional standard deviation image to use as a mask. If None, the input channel chn is used. Defaults to None.
- Returns:
The image channel without background, where background pixels have been set to zero and values have been clipped to the range [0, 1].
- Return type:
npt.NDArray[np.float64]
- cie_utils.sd_by_elem(img: ndarray[tuple[int, ...], dtype[float64]]) ndarray[tuple[int, ...], dtype[float64]] [source]¶
Calculates the standard deviation per color component (R, G, B) for each pixel.
For each color channel (red, green, blue), this function calculates the standard deviation of pixels that are greater than zero. If a pixel is zero, its individual standard deviation is considered zero.
- Parameters:
img (npt.NDArray[np.float64]) – The input image as a NumPy array of type float64, with 3 channels.
- Returns:
An array with the same shape as the input image, where each element represents the standard deviation of the corresponding color component.
- Return type:
npt.NDArray[np.float64]
- cie_utils.sd_by_px(img: ndarray[tuple[int, ...], dtype[float64]]) ndarray[tuple[int, ...], dtype[float64]] [source]¶
Calculates the standard deviation per RGB pixel.
For each pixel in the image, calculates the standard deviation of its three color components (R, G, B).
- Parameters:
img (npt.NDArray[np.float64]) – The input image as a NumPy array of type float64, with 3 channels.
- Returns:
A 2D array containing the standard deviation of the RGB values for each pixel.
- Return type:
npt.NDArray[np.float64]
- cie_utils.sort_classifier_results(image, labels, centers, ordered, n_ref, verbose=False)[source]¶
Sorts clustering results (e.g., K-means) based on the number of pixels or closeness to LAB reference values.
- Parameters:
image (npt.NDArray) – The original image (flattened if used for clustering) in LAB color space.
labels (npt.NDArray) – The cluster identifiers assigned to each pixel.
centers (npt.NDArray) – The coordinates (centroids) of the clusters in LAB color space.
ordered (str) – The sorting method to use. Must be “by_pixel” (by number of pixels in each cluster, descending) or “reference_values” (by closeness to predefined LAB reference values).
n_ref (int) – Index of the reference points to use if ordered is “reference_values”. Must be 0 or 1, selecting between two sets of LAB references.
verbose (bool, optional) – If True, prints detailed information about the sorting process and the average colors of the clusters. Defaults to False.
- Returns:
- centersnpt.NDArray
The sorted cluster centroids.
- ordered_imageslist[npt.NDArray]
A list of images, where each element is the segmented image corresponding to a cluster, sorted according to the specified method.
- Return type:
tuple[npt.NDArray, list[npt.NDArray]]
- Raises:
ValueError – If the ordered sorting method is invalid.
- cie_utils.std4elem(x: ndarray[tuple[int, ...], dtype[float64]]) ndarray[tuple[int, ...], dtype[float64]] [source]¶
Compute the element-wise standard deviation relative to the mean of non-zero elements in the input array.
This function calculates the deviation of each element from the mean of all non-zero elements in x, returning an array where elements originally equal to zero remain zero.
- Parameters:
x (numpy.ndarray) – A 1D NumPy array of type float64. Elements with value zero are ignored in the mean and standard deviation calculation.
- Returns:
An array of the same shape as x containing the standard deviation for each element with respect to the mean of non-zero elements. Zero entries remain zero.
- Return type:
numpy.ndarray
Examples
>>> import numpy as np >>> from cie_utils import std4elem >>> x = np.array([1.0, 2.0, 0.0, 4.0], dtype=np.float64) >>> std4elem(x) array([1.52752523, 0.15275252, 0., 1.37477271])
- cie_utils.transform_img(image: ndarray[tuple[int, ...], dtype[float64]], bg_pixel: ndarray[tuple[int, ...], dtype[float64]] | None = None, blur: bool = True, lab: bool = True, clahe: bool = True) list[ndarray[tuple[int, ...], dtype[float64]]] [source]¶
Applies a series of transformations to an image: Gaussian blur, conversion to L*a*b* color space, and adaptive histogram equalization (CLAHE).
Pixels that are black (considered background) can be replaced by a specified bg_pixel before transformations.
- Parameters:
image (npt.NDArray[np.float64]) – The normalized input image, in the range [0.0, 1.0]. RGB format is expected.
bg_pixel (npt.NDArray[np.float64] | None, optional) – An RGB pixel (float64) to insert as background where the original image was black. If None, black pixels are retained. Defaults to None.
blur (bool, optional) – If True, applies a Gaussian blur to the image. Defaults to True.
lab (bool, optional) – If True, converts the image to CIELAB color space. Defaults to True.
clahe (bool, optional) – If True, applies CLAHE to the illumination channel of the image (only if lab is True). Defaults to True.
- Returns:
A list containing:
- blur_imagenpt.NDArray[np.float64]
The blurred image (or the original image if blur is False).
- rgb_image_eqnpt.NDArray[np.float64]
The resulting image after all transformations, converted back to RGB.
- lab_image_eqnpt.NDArray[np.float64]
The resulting image in CIELAB color space, flattened to (N, 3) where N is the total number of pixels.
- Return type:
list[npt.NDArray[np.float64]]