Segmentation API¶
obia.segmentation.segment¶
obia.segmentation.segment
¶
Segments
¶
Segments class for handling and processing image segments.
Attributes:
| Name | Type | Description |
|---|---|---|
_segments |
Integer object labels (also available as |
|
segments |
Public representation of segments data. |
|
method |
Method used for segmentation. |
|
params |
Additional parameters for segmentation. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initializes the Segments object with segments data, a method, and additional parameters. |
to_segmented_image |
Converts a given PIL Image to a segmented image with boundaries overlaid. |
write_segments |
Writes the segments data to a file. |
Source code in obia/segmentation/segment.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 | |
to_segmented_image(image)
¶
:param image: An input image expected to be of type PIL Image. :return: A new image with the boundaries marked on the segments.
Source code in obia/segmentation/segment.py
41 42 43 44 45 46 47 48 49 50 51 52 53 | |
write_segments(file_path)
¶
:param file_path: The path to the file where segments will be written. :return: None
Source code in obia/segmentation/segment.py
55 56 57 58 59 60 | |
segment(image, segmentation_bands=None, statistics_bands=None, method='slic', calc_mean=True, calc_variance=True, calc_skewness=True, calc_kurtosis=True, calc_contrast=True, calc_dissimilarity=True, calc_homogeneity=True, calc_ASM=True, calc_energy=True, calc_correlation=True, feature_options=None, **kwargs)
¶
:param image: Input image for segmentation and feature extraction.
:param segmentation_bands: Bands to be used for segmentation. Default is None.
:param statistics_bands: Bands to be used for statistical calculations. Default is None.
:param method: Scikit-image region method from SEGMENTATION_METHODS. Default is "slic".
:param calc_mean: Flag to calculate mean of segments. Default is True.
:param calc_variance: Flag to calculate variance of segments. Default is True.
:param calc_skewness: Flag to calculate skewness of segments. Default is True.
:param calc_kurtosis: Flag to calculate kurtosis of segments. Default is True.
:param calc_contrast: Flag to calculate contrast of segments. Default is True.
:param calc_dissimilarity: Flag to calculate dissimilarity of segments. Default is True.
:param calc_homogeneity: Flag to calculate homogeneity of segments. Default is True.
:param calc_ASM: Flag to calculate angular second moment of segments. Default is True.
:param calc_energy: Flag to calculate energy of segments. Default is True.
:param calc_correlation: Flag to calculate correlation of segments. Default is True.
:param feature_options: Optional dictionary of create_objects options, including spectral_features,
percentiles, indices, shape_features, and texture_config. These override legacy feature flags.
:param kwargs: Segmentation parameters; see create_labels for supported methods and inputs.
:return: Segments object containing the segmented image and statistics.
Source code in obia/segmentation/segment.py
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 | |
obia.segmentation.segment_boundaries¶
obia.segmentation.segment_boundaries
¶
Create georeferenced objects with scikit-image segmentation algorithms.
create_labels(image, segmentation_bands=None, method='slic', *, mask=None, normalize=True, **kwargs)
¶
Segment a raster into a two-dimensional integer object-label image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
OBIA Image with data shaped (height, width, bands). |
required |
segmentation_bands
|
Sequence[int] | None
|
Zero-based bands to use; defaults to all bands. Watershed, Chan-Vese, morphological snakes, and flood require one band. |
None
|
method
|
str
|
One of |
'slic'
|
mask
|
ndarray | None
|
Boolean (height, width) array of included pixels. SLIC, watershed, and random walker respect it during segmentation. Other methods run on the prepared image and their results are clipped to the mask. |
None
|
normalize
|
bool
|
Scale each selected band to [0, 1] using included pixels. Set False for physical-unit flood tolerances or preprocessed data. |
True
|
**kwargs
|
Any
|
Parameters for the chosen scikit-image function. Random walker
requires |
{}
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: An int32 (height, width) array. Each connected object has a unique
positive ID; zero denotes background, watershed lines, or excluded
pixels. Native algorithm/marker IDs are renumbered to match polygon
|
Source code in obia/segmentation/segment_boundaries.py
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 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 | |
create_segments(image, segmentation_bands=None, method='slic', **kwargs)
¶
Return georeferenced segment polygons for any SEGMENTATION_METHODS entry.
Accepts the same parameters as :func:create_labels, including mask and
normalize. Zero/background pixels are omitted. The source raster is not
modified; use :func:create_labels when only the label image is needed.
Source code in obia/segmentation/segment_boundaries.py
236 237 238 239 240 241 242 243 244 | |
labels_to_segments(labels, image)
¶
Polygonize positive object labels using the raster's transform and CRS.
labels must be a 2D integer array matching the raster, with background 0.
Labels from :func:create_labels give one polygon per unique segment_id.
Custom labels with disconnected regions can produce multiple rows for an ID.
Source code in obia/segmentation/segment_boundaries.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
normalize_band(band)
¶
Return a floating-point band scaled to [0, 1]; constant bands become zero.
Source code in obia/segmentation/segment_boundaries.py
36 37 38 39 40 41 42 43 44 | |
obia.segmentation.segment_statistics¶
obia.segmentation.segment_statistics
¶
Compatibility entry points for raster feature extraction.
New workflows can use :func:obia.features.extract_features directly. The
existing functions keep their boolean flags and legacy output column names.
calculate_spectral_stats(image, statistics_bands, calc_mean=True, calc_variance=True, calc_min=True, calc_max=True, calc_skewness=True, calc_kurtosis=True, *, features=None, percentiles=(), mask=None)
¶
Summarize a band-first (bands, height, width) array without modification.
Legacy flags select the original statistics; features overrides them
with names from SPECTRAL_FEATURES. percentiles adds linear percentiles.
Nonfinite and masked samples are excluded. A spatial mask defines the
object footprint and denominator for valid_fraction.
Source code in obia/segmentation/segment_statistics.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
calculate_textural_stats(image, textural_bands, calc_contrast=True, calc_dissimilarity=True, calc_homogeneity=True, calc_ASM=True, calc_energy=True, calc_correlation=True, *, texture_config=None, mask=None, channel_axis=0)
¶
Calculate texture from a band-first array (or explicit channel_axis=-1).
Only pixel pairs with both endpoints inside mask and finite are counted.
Without texture_config, legacy bN_contrast etc. columns use distance 2,
four directions, 256 gray levels, and the valid band range of this array.
An explicit TextureConfig enables additional features and scale columns.
Use extract_features to fit quantization ranges once across all objects.
Source code in obia/segmentation/segment_statistics.py
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 95 96 | |
create_objects(segments, image, spectral_bands=None, textural_bands=None, calculate_spectral=True, calculate_textural=True, calc_mean=True, calc_variance=True, calc_min=True, calc_max=True, calc_skewness=True, calc_kurtosis=True, calc_contrast=True, calc_dissimilarity=True, calc_homogeneity=True, calc_ASM=True, calc_energy=True, calc_correlation=True, *, spectral_features=None, percentiles=(), indices=None, shape_features=(), shape_units='map', texture_config=None, mask=None)
¶
Enrich segment polygons with selected raster features.
Existing flags and bN_mean/bN_contrast-style columns are retained. New
spectral_features, percentiles, indices, shape_features,
shape_units, texture_config and mask options correspond to
extract_features. All bands are numeric zero-based indices into the
loaded image. calculate_spectral=False disables band/index distributions;
calculate_textural=False disables texture. Shape can be requested alone.
Source code in obia/segmentation/segment_statistics.py
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 | |