ascota_classification.color¶
The color module provides feature extraction and clustering utilities for
grouping pottery images by color similarity.
Current functionality centers on:
- compact Lab-based image summaries,
- PCA feature extraction, and
- HDBSCAN clustering (directly or via a high-level directory pipeline).
Color-based clustering module for images with transparent backgrounds.
This module clusters similar images using a fast pipeline: 1. Convert each image to CIE Lab and compute per-image summary statistics (mean and std of L, a, b over opaque pixels only). 2. Stack summaries and reduce dimension with PCA. 3. Cluster the PCA-transformed features with HDBSCAN.
Designed for near-realtime use: Lab summary and PCA are cheap; HDBSCAN is run on reduced-dimensional data.
_image_to_lab_summary ¶
_image_to_lab_summary(
image, alpha_threshold=DEFAULT_ALPHA_THRESHOLD, resize_max=DEFAULT_RESIZE_MAX
)
Compute a fixed-length Lab color summary for one image (opaque pixels only).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
PIL Image (RGBA or RGB). If RGBA, only pixels with alpha >= alpha_threshold are used. |
required |
alpha_threshold
|
int
|
Pixels with alpha >= this value are included in the summary (0-255). |
DEFAULT_ALPHA_THRESHOLD
|
resize_max
|
Optional[int]
|
If set, resize image so the longer side is at most this many pixels (for speed). |
DEFAULT_RESIZE_MAX
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Length-6 vector: (mean_L, mean_a, mean_b, std_L, std_a, std_b). If no opaque pixels, |
ndarray
|
returns zeros. |
Source code in src/ascota_classification/color.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 | |
extract_lab_pca_features ¶
extract_lab_pca_features(
images,
pca_components=DEFAULT_PCA_COMPONENTS,
alpha_threshold=DEFAULT_ALPHA_THRESHOLD,
resize_max=DEFAULT_RESIZE_MAX,
)
Extract Lab-based summary features and reduce dimension with PCA.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
List[Image]
|
List of PIL Images (RGBA or RGB; transparent backgrounds supported). |
required |
pca_components
|
Union[int, float]
|
Number of components (int) or fraction of variance to retain (float in (0, 1]). |
DEFAULT_PCA_COMPONENTS
|
alpha_threshold
|
int
|
Alpha threshold for opaque pixels (0-255). |
DEFAULT_ALPHA_THRESHOLD
|
resize_max
|
Optional[int]
|
Max size of the longer side before computing Lab summary; None to disable. |
DEFAULT_RESIZE_MAX
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Tuple of (feature_matrix, pca): |
PCA
|
|
Tuple[ndarray, PCA]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If images list is empty. |
Source code in src/ascota_classification/color.py
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 | |
cluster_images_hdbscan ¶
cluster_images_hdbscan(
features,
min_cluster_size=DEFAULT_MIN_CLUSTER_SIZE,
min_samples=DEFAULT_MIN_SAMPLES,
cluster_selection_epsilon=DEFAULT_CLUSTER_SELECTION_EPSILON,
cluster_selection_method=DEFAULT_CLUSTER_SELECTION_METHOD,
**kwargs
)
Cluster samples by features using HDBSCAN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
ndarray
|
Feature matrix of shape (n_samples, n_features). |
required |
min_cluster_size
|
int
|
Minimum size of clusters; smaller groups are treated as noise. |
DEFAULT_MIN_CLUSTER_SIZE
|
min_samples
|
Optional[int]
|
Core point neighborhood size; if None, defaults to min_cluster_size. |
DEFAULT_MIN_SAMPLES
|
cluster_selection_epsilon
|
float
|
Distance threshold for merging clusters (0 = no merge by epsilon). |
DEFAULT_CLUSTER_SELECTION_EPSILON
|
cluster_selection_method
|
str
|
"eom" (excess of mass) or "leaf". |
DEFAULT_CLUSTER_SELECTION_METHOD
|
**kwargs
|
Any
|
Additional arguments passed to hdbscan.HDBSCAN. |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Tuple of (labels, clusters): |
List[List[int]]
|
|
Tuple[ndarray, List[List[int]]]
|
|
Source code in src/ascota_classification/color.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
cluster_similar_images ¶
cluster_similar_images(
images,
min_cluster_size=DEFAULT_MIN_CLUSTER_SIZE,
min_samples=DEFAULT_MIN_SAMPLES,
cluster_selection_epsilon=DEFAULT_CLUSTER_SELECTION_EPSILON,
cluster_selection_method=DEFAULT_CLUSTER_SELECTION_METHOD,
pca_components=DEFAULT_PCA_COMPONENTS,
alpha_threshold=DEFAULT_ALPHA_THRESHOLD,
resize_max=DEFAULT_RESIZE_MAX,
return_pca_and_labels=False,
**hdbscan_kwargs
)
High-level pipeline: extract Lab+PCA features and cluster with HDBSCAN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
List[Image]
|
List of PIL Images (transparent backgrounds supported). |
required |
min_cluster_size
|
int
|
HDBSCAN min_cluster_size. |
DEFAULT_MIN_CLUSTER_SIZE
|
min_samples
|
Optional[int]
|
HDBSCAN min_samples (None => use min_cluster_size). |
DEFAULT_MIN_SAMPLES
|
cluster_selection_epsilon
|
float
|
HDBSCAN cluster_selection_epsilon. |
DEFAULT_CLUSTER_SELECTION_EPSILON
|
cluster_selection_method
|
str
|
HDBSCAN cluster_selection_method ("eom" or "leaf"). |
DEFAULT_CLUSTER_SELECTION_METHOD
|
pca_components
|
Union[int, float]
|
PCA components (int or variance fraction float). |
DEFAULT_PCA_COMPONENTS
|
alpha_threshold
|
int
|
Alpha threshold for opaque pixels. |
DEFAULT_ALPHA_THRESHOLD
|
resize_max
|
Optional[int]
|
Max longer side for Lab summary; None to disable. |
DEFAULT_RESIZE_MAX
|
return_pca_and_labels
|
bool
|
If True, also return fitted PCA and label array. |
False
|
**hdbscan_kwargs
|
Any
|
Passed to HDBSCAN. |
{}
|
Returns:
| Type | Description |
|---|---|
Union[Tuple[List[List[int]], List[int]], Tuple[List[List[int]], List[int], PCA, ndarray]]
|
If return_pca_and_labels is False: (clusters, noise_indices). |
Union[Tuple[List[List[int]], List[int]], Tuple[List[List[int]], List[int], PCA, ndarray]]
|
If return_pca_and_labels is True: (clusters, noise_indices, pca, labels). |
Union[Tuple[List[List[int]], List[int]], Tuple[List[List[int]], List[int], PCA, ndarray]]
|
|
Union[Tuple[List[List[int]], List[int]], Tuple[List[List[int]], List[int], PCA, ndarray]]
|
|
Source code in src/ascota_classification/color.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |