Utils API¶
obia.utils.image¶
obia.utils.image
¶
apply_clahe(image)
¶
Applies CLAHE (Contrast Limited Adaptive Histogram Equalization) to an image for improving contrast in both grayscale and color images.
CLAHE works by dividing the image into small tiles and enhancing each tile separately. It is particularly useful for improving the visibility of features in images with varying lighting conditions or shadows.
:param image: The input image to which CLAHE should be applied. This can be either a grayscale or a color image. If the image has three dimensions, it is assumed to be in the BGR color format where CLAHE is applied to each channel separately. :return: The image with applied CLAHE, having enhanced contrast compared to the original input.
Source code in obia/utils/image.py
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 | |
apply_histogram_equalization(image)
¶
Apply histogram equalization to the input image. Histogram equalization enhances the contrast of an image by effectively spreading out the most frequent intensity values. This function supports both grayscale and RGB images, converting them to grayscale before applying the equalization process. The equalized grayscale image is then stacked into a 3-channel image.
:param image: The image to be equalized. It can be a 2D array for grayscale or a 3D array for RGB images. :type image: numpy.ndarray
:return: A new image where the histogram has been equalized. The returned image will be a 3-channel RGB image irrespective of whether the input was grayscale or RGB. :rtype: numpy.ndarray
Source code in obia/utils/image.py
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 | |
rescale_to_8bit(image, min=2, max=98)
¶
Rescales an image to an 8-bit unsigned integer representation by stretching and clamping its intensity values between specified percentile ranges. This transformation enhances the dynamic range of the image and is useful for various image processing tasks.
:param image: A NumPy array representing the input image to be rescaled. This image should have intensity values that need adjustment. :param min: The lower percentile cutoff for rescaling, with a default value of 2, indicating that the intensity values below this percentile will be clipped. :param max: The upper percentile cutoff for rescaling, with a default value of 98, indicating that the intensity values above this percentile will be clipped. :return: A NumPy array of the same shape as the input image, with the intensity values rescaled to the range [0, 255] as 8-bit unsigned integers.
Source code in obia/utils/image.py
8 9 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 | |
rgb_to_gray(rgb)
¶
ITU-R 601 grayscale (expects float32 array in [0,1]).
Source code in obia/utils/image.py
97 98 99 100 | |
variance_of_laplacian(gray, win)
¶
Local variance of the 3×3 Laplacian, window = win×win pixels.
Source code in obia/utils/image.py
103 104 105 106 107 108 | |
obia.utils.tiling¶
obia.utils.tiling
¶
create_tiled_segments(input_raster, output_dir, input_mask=None, method='slic', tile_size=200, buffer=30, crown_radius=5, **kwargs)
¶
:param input_raster: Path to the input raster file. :param output_dir: Directory where output files will be saved. :param input_mask: Optional path to an input mask file for masking specific regions. :param method: Segmentation method to be used, defaults to "slic". Currently, only the 'slic' method is supported. :param tile_size: Size of the tiles into which the raster is divided, default is 200. :param buffer: Buffer size for tile overlap, default is 30. :param crown_radius: Radius used to compute the number of segments/crowns, default is 5. :param kwargs: Additional keyword arguments passed to the segmentation function. :return: None
Source code in obia/utils/tiling.py
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 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 253 254 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 | |
get_raster_bbox(dataset)
¶
:param dataset: Input dataset from which to retrieve the bounding box. Must have GetGeoTransform, RasterXSize, and RasterYSize methods. :return: A tuple representing the bounding box (min_x, min_y, max_x, max_y).
Source code in obia/utils/tiling.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
obia.utils.training¶
obia.utils.training
¶
generate_tiles(bounds, step, tile_size)
¶
Generator that yields bounding boxes (minx, miny, maxx, maxy) by stepping through the full raster bounds in increments of 'step', producing tiles of size 'tile_size'.
Source code in obia/utils/training.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
tile_and_process(raster_path, mask_path=None, boxes_gpkg_path=None, output_dir='output_tiles', tile_size=150.0, overlap=50.0, selected_bands=(4, 2, 1), feather_radius=0.0, blur_kernel=5, darken_factor=0.8, apply_clahe_flag=True, rescale=True)
¶
Tiles an input raster (and corresponding binary mask if provided), optionally applies CLAHE, optionally feathers the canopy edges using a distance transform, and writes out per-tile images (JPEG). Also converts polygons to bounding-box annotations if 'boxes_gpkg_path' is provided.
Additionally, it saves a 'transforms.json' containing each tile's transform (as [a,b,c,d,e,f]) and its CRS in string form, so you can later reconstruct georeferencing for each tile.
Parameters¶
raster_path : str Path to the main image raster (e.g., "image.tif"). mask_path : str or None If provided, path to a binary canopy mask (0=background, 1=canopy). If None, no masking or feathering will be done. boxes_gpkg_path : str or None If provided, path to a GeoPackage with polygon annotations to be converted to bounding boxes per tile. If None, no annotations are generated. output_dir : str Directory to save output JPEG tiles, annotations.json, and transforms.json. tile_size : float Size of each tile in the raster's coordinate units (e.g., meters). overlap : float Overlap (in same units) between adjacent tiles. selected_bands : tuple of ints Which bands (1-based in raster) to extract from the raster. E.g., (4,2,1). feather_radius : float If > 0, a distance transform is used to create a soft alpha around canopy edges. 0 = no feathering. Only relevant if mask_path is not None. blur_kernel : int or tuple Size of the GaussianBlur kernel (must be an odd int or an odd tuple), e.g. 5 or (5,5). If 0, skip blur entirely. darken_factor : float Multiplier for background brightness, e.g., 0.8 = 80% brightness, 0.2 = 20% brightness. Only relevant if mask_path is not None. apply_clahe_flag : bool If True, apply CLAHE to the rescaled 8-bit tile before blending. If False, skip CLAHE and use the raw 8-bit image. rescale : bool If True, apply a percentile-based stretch to 8-bit. If False, do a min-max linear scaling (or no scaling if tile_min==tile_max).
Returns¶
None Writes: - Per-tile JPEG images in 'output_dir'. - 'annotations.json' (if polygons are provided). - 'transforms.json' with each tile's geotransform & CRS.
Source code in obia/utils/training.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 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 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 253 254 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 | |
obia.utils.utils¶
obia.utils.utils
¶
crop_image_to_bbox(image, geom)
¶
Crop the image data to the bounding box of the given geometry.
:param image: The Image object containing the image data and rasterio object. :param geom: The geometry (Polygon) used to derive the bounding box for cropping. :return: Cropped image data as a NumPy array and the updated transform.
Source code in obia/utils/utils.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
label_segments(segments, labelled_points)
¶
:param segments: A GeoDataFrame representing the segments to be labeled. :param labelled_points: A GeoDataFrame representing the labeled points used for segment labeling. :return: A tuple containing a GeoDataFrame with labeled segments and a list of segment IDs for mixed segments.
Source code in obia/utils/utils.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
mask_image_with_polygon(cropped_img_data, polygon, cropped_transform)
¶
Masks all pixels outside the polygon for the given cropped image.
:param cropped_img_data: The cropped image data as a NumPy array. :param polygon: The geometry (Polygon) used for masking. :param cropped_transform: The affine transform for the cropped image. :return: Masked image data as a NumPy array.
Source code in obia/utils/utils.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
save_deepforest_predictions_to_gpkg(df, tile_name, transforms_json, output_gpkg)
¶
Convert DeepForest predictions (pixel coords) to georeferenced polygons and save them in a GeoPackage.
Parameters¶
df : pd.DataFrame Must have columns: ["xmin","ymin","xmax","ymax","label","score"]. Optional "image_path" if you prefer to verify or filter by tile_name. tile_name : str The tile's filename (e.g. "img_007.jpg") that matches the key in transforms.json. transforms_json : str File path to the transforms.json created by tile_and_process(...). We'll load the tile's Affine transform + CRS from there. output_gpkg : str Path to the output GeoPackage file to create/overwrite.
Source code in obia/utils/utils.py
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 | |