cloudmask module
cloudmask module
OmniCloudMask integration for cloud and cloud shadow detection in satellite imagery.
This module provides functions to use OmniCloudMask (https://github.com/DPIRD-DMA/OmniCloudMask) for detecting clouds and cloud shadows in satellite imagery. OmniCloudMask performs semantic segmentation to classify pixels into: Clear (0), Thick Cloud (1), Thin Cloud (2), Cloud Shadow (3).
Supports Sentinel-2, Landsat 8, PlanetScope, and Maxar imagery at 10-50m resolution.
calculate_cloud_statistics(mask)
¶
Calculate statistics from a cloud mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
ndarray
|
Cloud mask array with values 0-3. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Dict[str, Any]
|
Statistics including: - total_pixels: Total number of pixels - clear_pixels: Number of clear pixels - thick_cloud_pixels: Number of thick cloud pixels - thin_cloud_pixels: Number of thin cloud pixels - shadow_pixels: Number of cloud shadow pixels - clear_percent: Percentage of clear pixels - cloud_percent: Percentage of cloudy pixels (thick + thin) - shadow_percent: Percentage of shadow pixels |
Example
from geoai.tools.cloudmask import calculate_cloud_statistics import numpy as np mask = np.random.randint(0, 4, (512, 512)) stats = calculate_cloud_statistics(mask) print(f"Clear: {stats['clear_percent']:.1f}%")
Source code in geoai/tools/cloudmask.py
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | |
check_omnicloudmask_available()
¶
Check if omnicloudmask is installed.
Raises:
| Type | Description |
|---|---|
ImportError
|
If omnicloudmask is not installed. |
Source code in geoai/tools/cloudmask.py
37 38 39 40 41 42 43 44 45 46 47 48 49 | |
create_cloud_free_mask(mask, include_thin_clouds=False, include_shadows=False)
¶
Create a binary mask of cloud-free pixels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
ndarray
|
Cloud mask with values 0-3. |
required |
include_thin_clouds
|
bool
|
If True, treats thin clouds as acceptable. Defaults to False. |
False
|
include_shadows
|
bool
|
If True, treats shadows as acceptable. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Binary mask where 1 = usable, 0 = not usable. |
Example
from geoai.tools.cloudmask import create_cloud_free_mask import numpy as np mask = np.random.randint(0, 4, (512, 512)) cloud_free = create_cloud_free_mask(mask) print(f"Usable pixels: {cloud_free.sum()}")
Source code in geoai/tools/cloudmask.py
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | |
predict_cloud_mask(image, batch_size=1, inference_device=None, inference_dtype='fp32', patch_size=1000, export_confidence=False, model_version=None)
¶
Predict cloud mask from a numpy array using OmniCloudMask.
This function classifies each pixel into one of four categories: - 0: Clear - 1: Thick Cloud - 2: Thin Cloud - 3: Cloud Shadow
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
ndarray
|
Input image array with shape (3, height, width) or (height, width, 3). Should contain Red, Green, and NIR bands. Values should be in reflectance (0-1) or digital numbers (0-10000 typical for Sentinel-2/Landsat). |
required |
batch_size
|
int
|
Number of patches to process per inference batch. Defaults to 1. |
1
|
inference_device
|
str
|
Device for inference ('cpu', 'cuda', or 'mps'). Defaults to None, which will use the device with the most available memory. |
None
|
inference_dtype
|
str
|
Data type for inference ('fp32', 'fp16', or 'bf16'). 'bf16' recommended for speed on compatible hardware. Defaults to 'fp32'. |
'fp32'
|
patch_size
|
int
|
Size of patches for processing large images. Defaults to 1000. |
1000
|
export_confidence
|
bool
|
If True, also returns confidence map. Defaults to False. |
False
|
model_version
|
int
|
Model version to use (1, 2, or 3). Defaults to None, which will use the latest version. |
None
|
Returns:
| Type | Description |
|---|---|
Union[ndarray, Tuple[ndarray, ndarray]]
|
Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]: Cloud mask array with shape (height, width) containing class predictions. If export_confidence=True, returns a tuple of (mask, confidence). |
Raises:
| Type | Description |
|---|---|
ImportError
|
If omnicloudmask is not installed. |
ValueError
|
If image has wrong shape or number of channels. |
Example
import numpy as np from geoai.tools.cloudmask import predict_cloud_mask
Create synthetic image (3 bands: R, G, NIR)¶
image = np.random.rand(3, 512, 512) * 10000 mask = predict_cloud_mask(image) print(f"Clear pixels: {(mask == 0).sum()}")
Source code in geoai/tools/cloudmask.py
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 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 | |
predict_cloud_mask_batch(input_paths, output_dir, red_band=1, green_band=2, nir_band=3, batch_size=1, inference_device='cpu', inference_dtype='fp32', patch_size=1000, export_confidence=False, model_version=3, suffix='_cloudmask', verbose=True)
¶
Predict cloud masks for multiple rasters in batch.
Processes multiple GeoTIFF files with the same cloud detection parameters and saves results to an output directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_paths
|
list of str
|
Paths to input GeoTIFF files. |
required |
output_dir
|
str
|
Directory to save cloud masks. |
required |
red_band
|
int
|
Red band index. Defaults to 1. |
1
|
green_band
|
int
|
Green band index. Defaults to 2. |
2
|
nir_band
|
int
|
NIR band index. Defaults to 3. |
3
|
batch_size
|
int
|
Patches per batch. Defaults to 1. |
1
|
inference_device
|
str
|
Device. Defaults to 'cpu'. |
'cpu'
|
inference_dtype
|
str
|
Dtype. Defaults to 'fp32'. |
'fp32'
|
patch_size
|
int
|
Patch size. Defaults to 1000. |
1000
|
export_confidence
|
bool
|
Export confidence. Defaults to False. |
False
|
model_version
|
str
|
Model version. Defaults to '3.0'. |
3
|
suffix
|
str
|
Suffix for output filenames. Defaults to '_cloudmask'. |
'_cloudmask'
|
verbose
|
bool
|
Print progress. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
List[str]
|
list of str: Paths to output cloud mask files. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If omnicloudmask or rasterio not installed. |
Example
from geoai.tools.cloudmask import predict_cloud_mask_batch files = ["scene1.tif", "scene2.tif", "scene3.tif"] outputs = predict_cloud_mask_batch( ... files, ... output_dir="cloud_masks", ... inference_device="cuda" ... )
Source code in geoai/tools/cloudmask.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
predict_cloud_mask_from_raster(input_path, output_path, red_band=1, green_band=2, nir_band=3, batch_size=1, inference_device=None, inference_dtype='fp32', patch_size=1000, export_confidence=False, model_version=None)
¶
Predict cloud mask from a GeoTIFF file and save the result.
Reads a multi-band raster, extracts RGB+NIR bands, applies OmniCloudMask, and saves the result while preserving geospatial metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path to input GeoTIFF file. |
required |
output_path
|
str
|
Path to save cloud mask GeoTIFF. |
required |
red_band
|
int
|
Band index for Red (1-indexed). Defaults to 1. |
1
|
green_band
|
int
|
Band index for Green (1-indexed). Defaults to 2. |
2
|
nir_band
|
int
|
Band index for NIR (1-indexed). Defaults to 3. |
3
|
batch_size
|
int
|
Patches per inference batch. Defaults to 1. |
1
|
inference_device
|
str
|
Device ('cpu', 'cuda', 'mps'). Defaults to None, which will use the device with the most available memory. |
None
|
inference_dtype
|
str
|
Dtype ('fp32', 'fp16', 'bf16'). Defaults to 'fp32'. |
'fp32'
|
patch_size
|
int
|
Patch size for large images. Defaults to 1000. |
1000
|
export_confidence
|
bool
|
Export confidence map. Defaults to False. |
False
|
model_version
|
int
|
Model version (1, 2, or 3). Defaults to None, which will use the latest version. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
Writes cloud mask to output_path. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If omnicloudmask or rasterio not installed. |
FileNotFoundError
|
If input_path doesn't exist. |
Example
from geoai.tools.cloudmask import predict_cloud_mask_from_raster predict_cloud_mask_from_raster( ... "sentinel2_image.tif", ... "cloud_mask.tif", ... red_band=4, # Sentinel-2 band order ... green_band=3, ... nir_band=8 ... )
Source code in geoai/tools/cloudmask.py
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 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 242 243 244 245 246 247 248 249 250 251 252 | |