cellects.image_analysis.image_segmentation
cellects.image_analysis.image_segmentation
Module for image segmentation operations including filtering, color space conversion, thresholding, and quality assessment.
This module provides tools to process images through grayscale conversion, apply various filters (e.g., Gaussian, Median, Butterworth), perform thresholding methods like Otsu's algorithm, combine color spaces for enhanced segmentation, and evaluate binary image quality. Key functionalities include dynamic background subtraction, rolling window segmentation with localized thresholds, and optimization of segmentation masks using shape descriptors.
Functions apply_filter : Apply skimage or OpenCV-based filters to grayscale images. get_color_spaces : Convert BGR images into specified color space representations (e.g., LAB, HSV). combine_color_spaces : Merge multiple color channels with coefficients to produce a segmented image. generate_color_space_combination : Create custom grayscale combinations using two sets of channel weights and backgrounds. otsu_thresholding : Binarize an image using histogram-based Otsu thresholding. segment_with_lum_value : Segment video frames using luminance thresholds adjusted for background variation. rolling_window_segmentation : Apply localized Otsu thresholding across overlapping patches to improve segmentation accuracy. binary_quality_index : Calculate a quality metric based on perimeter and connected components in binary images. find_threshold_given_mask : Binary search optimization to determine optimal threshold between masked regions.
Notes Uses Numba's @njit decorator for JIT compilation of performance-critical functions like combine_color_spaces and _get_counts_jit.
apply_filter(image, filter_type, param, rescale_to_uint8=False)
Apply various filters to an image based on the specified filter type.
This function applies a filter to the input image according to the
specified filter_type and associated parameters. Supported filters
include Gaussian, Median, Butterworth, Frangi, Sato, Meijering,
Hessian, Laplace, Mexican hat, Farid, Prewitt, Roberts, Scharr, and Sobel.
Except from Sharpen and Mexican hat, these filters are implemented using the skimage.filters module.
Additionally, the function can rescale the output image to uint8
format if specified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
NDArray
|
The input image to which the filter will be applied. |
required |
filter_type
|
str
|
The type of filter to apply. Supported values include: "Gaussian", "Median", "Butterworth", "Frangi", "Sato", "Meijering", "Hessian", "Laplace", "Mexican hat", "Sharpen", "Farid", "Prewitt", "Roberts", "Scharr", and "Sobel". |
required |
param
|
list or tuple
|
Parameters specific to the filter type. The structure of |
required |
rescale_to_uint8
|
bool
|
Whether to rescale the output image to uint8 format. Default is False. |
False
|
Notes
The Sharpen filter is implemented through: cv2.filter2D(image, -1, np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])) The Maxican hat filter is implemented through: cv2.filter2D(image, -1, np.array( [[0, 0, -1, 0, 0], [0, -1, -2, -1, 0], [-1, -2, 16, -2, -1], [0, -1, -2, -1, 0], [0, 0, -1, 0, 0]])) All other filters are skimage filters.
Returns:
| Type | Description |
|---|---|
NDArray
|
The filtered image. If |
Examples:
>>> image = np.zeros((3, 3))
>>> image[1, 1] = 1
>>> filtered_image = apply_filter(image, "Gaussian", [1.0])
>>> print(filtered_image)
[[0.05855018 0.09653293 0.05855018]
[0.09653293 0.15915589 0.09653293]
[0.05855018 0.09653293 0.05855018]]
Filtered image with Gaussian filter.
>>> image = np.zeros((3, 3))
>>> image[1, 1] = 1
>>> filtered_image = apply_filter(image, "Median", [])
>>> print(filtered_image)
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
Filtered image with Median filter.
>>> image = np.zeros((3, 3))
>>> image[1, 1] = 1
>>> filtered_image = apply_filter(image, "Butterworth", [0.005, 2])
>>> print(filtered_image)
[[-0.1111111 -0.11111111 -0.1111111 ]
[-0.11111111 0.88888886 -0.11111111]
[-0.1111111 -0.11111111 -0.1111111 ]]
Filtered image with Butterworth filter.
Source code in src/cellects/image_analysis/image_segmentation.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
binary_quality_index(binary_img)
Calculate the binary quality index for a binary image.
The binary quality index is computed based on the perimeter of the largest connected component in the binary image, normalized by the total number of pixels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
binary_img
|
ndarray of uint8
|
Input binary image array. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
float
|
The binary quality index value. |
Source code in src/cellects/image_analysis/image_segmentation.py
combine_color_spaces(c_space_dict, all_c_spaces, subtract_background=None)
Combine color spaces from a dictionary and generate an analyzable image.
This function processes multiple color spaces defined in c_space_dict, combines
them according to given coefficients, and produces a normalized image that can be
converted to uint8. Optionally subtracts background from the resultant image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c_space_dict
|
dict
|
Dictionary containing color spaces and their respective coefficients. |
required |
all_c_spaces
|
Dict
|
Dictionary of all available color spaces in the image. |
required |
subtract_background
|
NDArray
|
Background image to subtract from the resultant image. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
out |
NDArray
|
Processed and normalized image in float64 format, ready for uint8 conversion. |
Examples:
>>> c_space_dict = Dict()
>>> c_space_dict['hsv'] = [0, 1, 1]
>>> all_c_spaces = Dict()
>>> all_c_spaces['bgr'] = list(np.random.rand(5, 5, 3))
>>> all_c_spaces['hsv'] = list(np.random.rand(5, 5, 3))
>>> background = np.zeros((5, 5))
>>> result = combine_color_spaces(c_space_dict, all_c_spaces)
>>> print(result.shape)
(5, 5)
Source code in src/cellects/image_analysis/image_segmentation.py
convert_subtract_and_filter_video(video, color_space_combination, background=None, background2=None, lose_accuracy_to_save_memory=False, filter_spec=None)
Convert a video to grayscale, subtract the background, and apply filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
video
|
NDArray
|
The input video as a 4D NumPy array. |
required |
color_space_combination
|
dict
|
A dictionary containing the combinations of color space transformations. |
required |
background
|
NDArray
|
The first background image for subtraction. If |
None
|
background2
|
NDArray
|
The second background image for subtraction. If |
None
|
lose_accuracy_to_save_memory
|
bool
|
Flag to reduce accuracy and save memory by using |
False
|
filter_spec
|
dict
|
A dictionary containing the specifications for filters to apply. |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[NDArray, NDArray]
|
A tuple containing:
- |
Notes
- The function reduces accuracy of the converted video when `lose_accuracy_to_save_memory` is set to True.
- If `color_space_combination['logical']` is not 'None', a second converted video will be created.
- This function uses the `generate_color_space_combination` and `apply_filter` functions internally.
Source code in src/cellects/image_analysis/image_segmentation.py
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 | |
extract_first_pc(bgr_image, standardize=True)
Extract the first principal component from a BGR image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bgr_image
|
ndarray
|
A 3D or 2D array representing the BGR image. Expected shape is either (height, width, 3) or (3, height, width). |
required |
standardize
|
bool
|
If True, standardizes the image pixel values by subtracting the mean and dividing by the standard deviation before computing the principal components. Default is True. |
True
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The first principal component image, reshaped to the original image height and width. |
float
|
The explained variance ratio of the first principal component. |
ndarray
|
The first principal component vector. |
Notes
The principal component analysis (PCA) is performed using Singular Value Decomposition (SVD). Standardization helps in scenarios where the pixel values have different scales. Pixels with zero standard deviation are handled by setting their standardization denominator to 1.0 to avoid division by zero.
Examples:
>>> bgr_image = np.random.rand(100, 200, 3) # Example BGR image
>>> first_pc_image, explained_variance_ratio, first_pc_vector = extract_first_pc(bgr_image)
>>> print(first_pc_image.shape)
(100, 200)
>>> print(explained_variance_ratio)
0.339
>>> print(first_pc_vector.shape)
(3,)
Source code in src/cellects/image_analysis/image_segmentation.py
find_threshold_given_mask(greyscale, mask, min_threshold=0)
Find the optimal threshold value for a greyscale image given a mask.
This function performs a binary search to find the optimal threshold that maximizes the separation between two regions defined by the mask. The search is bounded by a minimum threshold value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
greyscale
|
ndarray of uint8
|
The greyscale image array. |
required |
mask
|
ndarray of uint8
|
The binary mask array where positive values define region A and zero values define region B. |
required |
min_threshold
|
uint8
|
The minimum threshold value for the search. Defaults to 0. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
out |
uint8
|
The optimal threshold value found. |
Examples:
>>> greyscale = np.array([[255, 128, 54], [0, 64, 20]], dtype=np.uint8)
>>> mask = np.array([[1, 1, 0], [0, 0, 0]], dtype=np.uint8)
>>> find_threshold_given_mask(greyscale, mask)
54
Source code in src/cellects/image_analysis/image_segmentation.py
generate_color_space_combination(bgr_image, c_spaces, first_dict, second_dict=Dict(), background=None, background2=None, convert_to_uint8=False, all_c_spaces={})
Generate color space combinations for an input image.
This function generates a grayscale image by combining multiple color spaces from an input BGR image and provided dictionaries. Optionally, it can also generate a second grayscale image using another dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bgr_image
|
ndarray of uint8
|
The input image in BGR color space. |
required |
c_spaces
|
list
|
List of color spaces to consider for combination. |
required |
first_dict
|
Dict
|
Dictionary containing color space and transformation details for the first grayscale image. |
required |
second_dict
|
Dict
|
Dictionary containing color space and transformation details for the second grayscale image. |
Dict()
|
background
|
ndarray
|
Background image to be used. Default is None. |
None
|
background2
|
ndarray
|
Second background image to be used for the second grayscale image. Default is None. |
None
|
convert_to_uint8
|
bool
|
Flag indicating whether to convert the output images to uint8. Default is False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
out |
tuple of ndarray of uint8
|
A tuple containing the first and second grayscale images. |
Examples:
>>> bgr_image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> c_spaces = ['bgr', 'hsv']
>>> first_dict = Dict()
>>> first_dict['bgr'] = [0, 1, 1]
>>> second_dict = Dict()
>>> second_dict['hsv'] = [0, 0, 1]
>>> greyscale_image1, greyscale_image2 = generate_color_space_combination(bgr_image, c_spaces, first_dict, second_dict)
>>> print(greyscale_image1.shape)
(100, 100)
Source code in src/cellects/image_analysis/image_segmentation.py
get_color_spaces(bgr_image, space_names='')
Convert a BGR image into various color spaces.
Converts the input BGR image to specified color spaces and returns them as a dictionary. If no space names are provided, converts to all default color spaces (LAB, HSV, LUV, HLS, YUV). If 'logical' is in the space names, it will be removed before conversion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bgr_image
|
ndarray of uint8
|
Input image in BGR color space. |
required |
space_names
|
list of str
|
List of color spaces to convert the image to. Defaults to none. |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
out |
dict
|
Dictionary with keys as color space names and values as the converted images. |
Examples:
>>> bgr_image = np.zeros((5, 5, 3), dtype=np.uint8)
>>> c_spaces = get_color_spaces(bgr_image, ['lab', 'hsv'])
>>> print(list(c_spaces.keys()))
['bgr', 'lab', 'hsv']
Source code in src/cellects/image_analysis/image_segmentation.py
get_otsu_threshold(image)
Calculate the optimal threshold value for an image using Otsu's method.
This function computes the Otsu's thresholding which automatically performs histogram shape analysis for threshold selection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
NDArray
|
The input grayscale image, represented as a NumPy array. |
required |
Returns:
| Type | Description |
|---|---|
int or float
|
The computed Otsu's threshold value. |
Source code in src/cellects/image_analysis/image_segmentation.py
kmeans(greyscale, greyscale2=None, kmeans_clust_nb=2, bio_mask=None, back_mask=None, logical='None', bio_label=None, bio_label2=None, previous_binary_image=None)
Perform K-means clustering on a greyscale image to generate binary images.
Extended Description
This function applies the K-means algorithm to a greyscale image or pair of images to segment them into binary images. It supports optional masks and previous segmentation labels for refining the clustering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
greyscale
|
NDArray
|
The input greyscale image to segment. |
required |
greyscale2
|
NDArray
|
A second greyscale image for logical operations. Default is |
None
|
kmeans_clust_nb
|
int
|
Number of clusters for K-means. Default is |
2
|
bio_mask
|
NDArray[uint8]
|
Mask for selecting biological objects. Default is |
None
|
back_mask
|
NDArray[uint8]
|
Mask for selecting background regions. Default is |
None
|
logical
|
str
|
Logical operation flag to enable processing of the second image. Default is |
'None'
|
bio_label
|
int
|
Label for biological objects in the first segmentation. Default is |
None
|
bio_label2
|
int
|
Label for biological objects in the second segmentation. Default is |
None
|
previous_binary_image
|
NDArray[uint8]
|
Previous binary image for refinement. Default is |
None
|
Other Parameters:
| Name | Type | Description |
|---|---|---|
**greyscale2 |
NDArray
|
|
logical |
NDArray
|
|
bio_label2 |
NDArray
|
|
Returns:
| Type | Description |
|---|---|
tuple
|
A tuple containing:
- |
Notes
- The function performs K-means clustering with random centers.
- If
logicalis not'None', both images are processed. - Default clustering uses 2 clusters, modify
kmeans_clust_nbfor different needs.
Source code in src/cellects/image_analysis/image_segmentation.py
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | |
otsu_thresholding(image)
Apply Otsu's thresholding to a grayscale image.
This function calculates the optimal threshold using Otsu's method and applies it to binarize the input image. The output is a binary image where pixel values are either 0 or 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
ndarray
|
Input grayscale image with any kind of value. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
ndarray of uint8
|
Binarized image with pixel values 0 or 1. |
Examples:
Source code in src/cellects/image_analysis/image_segmentation.py
rolling_window_segmentation(greyscale_image, possibly_filled_pixels, patch_size=(10, 10))
Perform rolling window segmentation on a greyscale image, using potentially filled pixels and a specified patch size.
The function divides the input greyscale image into overlapping patches defined by patch_size,
and applies Otsu's thresholding method to each patch. The thresholds can be optionally
refined using a minimization algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
greyscale_image
|
ndarray of uint8
|
The input greyscale image to segment. |
required |
possibly_filled_pixels
|
ndarray of uint8
|
An array indicating which pixels are possibly filled. |
required |
patch_size
|
tuple
|
The dimensions of the patches to segment. Default is (10, 10). Must be superior to (1, 1). |
(10, 10)
|
Returns:
| Name | Type | Description |
|---|---|---|
output |
ndarray of uint8
|
The segmented binary image where the network is marked as True. |
Examples:
>>> greyscale_image = np.array([[1, 2, 1, 1], [1, 3, 4, 1], [2, 4, 3, 1], [2, 1, 2, 1]])
>>> possibly_filled_pixels = greyscale_image > 1
>>> patch_size = (2, 2)
>>> result = rolling_window_segmentation(greyscale_image, possibly_filled_pixels, patch_size)
>>> print(result)
[[0 1 0 0]
[0 1 1 0]
[0 1 1 0]
[0 0 1 0]]
Source code in src/cellects/image_analysis/image_segmentation.py
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 | |
segment_with_lum_value(converted_video, basic_bckgrnd_values, l_threshold, lighter_background)
Segment video frames based on luminance threshold.
This function segments the input video frames by comparing against a dynamic luminance threshold. The segmentation can be based on either lighter or darker background.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
converted_video
|
ndarray
|
The input video frames in numpy array format. |
required |
basic_bckgrnd_values
|
ndarray
|
Array containing background values for each frame. |
required |
l_threshold
|
int or float
|
The luminance threshold value for segmentation. |
required |
lighter_background
|
bool
|
If True, the segmentation is done assuming a lighter background. Defaults to False. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
segmentation |
ndarray
|
Array containing the segmented video frames. |
l_threshold_over_time |
ndarray
|
Computed threshold over time for each frame. |
Examples:
>>> converted_video = np.array([[[100, 120], [130, 140]], [[160, 170], [180, 200]]], dtype=np.uint8)
>>> basic_bckgrnd_values = np.array([100, 120])
>>> lighter_background = False
>>> l_threshold = 130
>>> segmentation, threshold_over_time = segment_with_lum_value(converted_video, basic_bckgrnd_values, l_threshold, lighter_background)
>>> print(segmentation)
[[[0 1]
[1 1]]
[[1 1]
[1 1]]]
Source code in src/cellects/image_analysis/image_segmentation.py
windowed_thresholding(image, lighter_background=None, side_length=None, step=None, min_int_var=None)
Perform grid segmentation on the image.
This method applies a sliding window approach to segment the image into
a grid-like pattern based on intensity variations and optionally uses a mask.
The segmented regions are stored in self.binary_image.
Args: lighter_background (bool): If True, areas lighter than the Otsu threshold are considered; otherwise, darker areas are considered. side_length (int, optional): The size of each grid square. Default is None. step (int, optional): The step size for the sliding window. Default is None. min_int_var (int, optional): Threshold for intensity variation within a grid. Default is 20. mask (NDArray, optional): A binary mask to restrict the segmentation area. Default is None.