Slicing¶
sahi.slicing
¶
Classes¶
SliceImageResult
¶
Source code in sahi/slicing.py
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 |
|
Attributes¶
coco_images
property
¶
Returns CocoImage representation of SliceImageResult.
Returns:
Name | Type | Description |
---|---|---|
coco_images |
List[CocoImage]
|
a list of CocoImage |
filenames
property
¶
Returns a list of filenames for each slice.
Returns:
Name | Type | Description |
---|---|---|
filenames |
List[int]
|
a list of filenames as str |
images
property
¶
Returns sliced images.
Returns:
Name | Type | Description |
---|---|---|
images |
a list of np.array |
starting_pixels
property
¶
Returns a list of starting pixels for each slice.
Returns:
Name | Type | Description |
---|---|---|
starting_pixels |
List[int]
|
a list of starting pixel coords [x,y] |
Functions¶
__init__(original_image_size, image_dir=None)
¶
str
Directory of the sliced image exports.
original_image_size: list of int Size of the unsliced original image in [height, width]
Source code in sahi/slicing.py
SlicedImage
¶
Source code in sahi/slicing.py
Functions¶
__init__(image, coco_image, starting_pixel)
¶
np.array
Sliced image.
coco_image: CocoImage Coco styled image object that belong to sliced image. starting_pixel: list of list of int Starting pixel coordinates of the sliced image.
Source code in sahi/slicing.py
Functions¶
annotation_inside_slice(annotation, slice_bbox)
¶
Check whether annotation coordinates lie inside slice coordinates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
dict
|
Single annotation entry in COCO format. |
required |
|
List[int]
|
Generated from |
required |
Returns:
Type | Description |
---|---|
bool
|
True if any annotation coordinate lies inside slice. |
Source code in sahi/slicing.py
calc_aspect_ratio_orientation(width, height)
¶
calc_ratio_and_slice(orientation, slide=1, ratio=0.1)
¶
According to image resolution calculation overlap params Args: orientation: image capture angle slide: sliding window ratio: buffer value
Returns:
Type | Description |
---|---|
overlap params |
Source code in sahi/slicing.py
calc_resolution_factor(resolution)
¶
According to image resolution calculate power(2,n) and return the closest smaller n
.
Args:
resolution: the width and height of the image multiplied. such as 1024x720 = 737280
Returns:
Source code in sahi/slicing.py
calc_slice_and_overlap_params(resolution, height, width, orientation)
¶
This function calculate according to image resolution slice and overlap params. Args: resolution: str height: int width: int orientation: str
Returns:
Type | Description |
---|---|
Tuple[int, int, int, int]
|
x_overlap, y_overlap, slice_width, slice_height |
Source code in sahi/slicing.py
get_auto_slice_params(height, width)
¶
According to Image HxW calculate overlap sliding window and buffer params factor is the power value of 2 closest to the image resolution. factor <= 18: low resolution image such as 300x300, 640x640 18 < factor <= 21: medium resolution image such as 1024x1024, 1336x960 21 < factor <= 24: high resolution image such as 2048x2048, 2048x4096, 4096x4096 factor > 24: ultra-high resolution image such as 6380x6380, 4096x8192 Args: height: width:
Returns:
Type | Description |
---|---|
Tuple[int, int, int, int]
|
slicing overlap params x_overlap, y_overlap, slice_width, slice_height |
Source code in sahi/slicing.py
get_resolution_selector(res, height, width)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
resolution of image such as low, medium |
required |
|
int
|
|
required |
|
int
|
|
required |
Returns:
Type | Description |
---|---|
Tuple[int, int, int, int]
|
trigger slicing params function and return overlap params |
Source code in sahi/slicing.py
get_slice_bboxes(image_height, image_width, slice_height=None, slice_width=None, auto_slice_resolution=True, overlap_height_ratio=0.2, overlap_width_ratio=0.2)
¶
Generate bounding boxes for slicing an image into crops.
The function calculates the coordinates for each slice based on the provided image dimensions, slice size, and overlap ratios. If slice size is not provided and auto_slice_resolution is True, the function will automatically determine appropriate slice parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
int
|
Height of the original image. |
required |
|
int
|
Width of the original image. |
required |
|
int
|
Height of each slice. Default None. |
None
|
|
int
|
Width of each slice. Default None. |
None
|
|
float
|
Fractional overlap in height of each slice (e.g. an overlap of 0.2 for a slice of size 100 yields an overlap of 20 pixels). Default 0.2. |
0.2
|
|
optional
|
Fractional overlap in width of each slice (e.g. an overlap of 0.2 for a slice of size 100 yields an overlap of 20 pixels). Default 0.2. |
required |
|
bool
|
if not set slice parameters such as slice_height and slice_width, it enables automatically calculate these parameters from image resolution and orientation. |
True
|
Returns:
Type | Description |
---|---|
List[List[int]]
|
List[List[int]]: List of 4 corner coordinates for each N slices. [ [slice_0_left, slice_0_top, slice_0_right, slice_0_bottom], ... [slice_N_left, slice_N_top, slice_N_right, slice_N_bottom] ] |
Source code in sahi/slicing.py
process_coco_annotations(coco_annotation_list, slice_bbox, min_area_ratio)
¶
Slices and filters given list of CocoAnnotation objects with given 'slice_bbox' and 'min_area_ratio'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
List[int]
|
Generated from |
required |
|
float
|
If the cropped annotation area to original annotation ratio is smaller than this value, the annotation is filtered out. Default 0.1. |
required |
Returns:
Type | Description |
---|---|
List[CocoAnnotation]
|
Sliced annotations. |
Source code in sahi/slicing.py
shift_bboxes(bboxes, offset)
¶
Shift bboxes w.r.t offset.
Suppo
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
(Tensor, ndarray, list)
|
The bboxes need to be translated. Its shape can be (n, 4), which means (x, y, x, y). |
required |
|
Sequence[int]
|
The translation offsets with shape of (2, ). |
required |
Returns: Tensor, np.ndarray, list: Shifted bboxes.
Source code in sahi/slicing.py
shift_masks(masks, offset, full_shape)
¶
Shift masks to the original image. Args: masks (np.ndarray): masks that need to be shifted. offset (Sequence[int]): The offset to translate with shape of (2, ). full_shape (Sequence[int]): A (height, width) tuple of the huge image's shape. Returns: np.ndarray: Shifted masks.
Source code in sahi/slicing.py
slice_coco(coco_annotation_file_path, image_dir, output_coco_annotation_file_name, output_dir=None, ignore_negative_samples=False, slice_height=512, slice_width=512, overlap_height_ratio=0.2, overlap_width_ratio=0.2, min_area_ratio=0.1, out_ext=None, verbose=False)
¶
Slice large images given in a directory, into smaller windows. If output_dir is given, export sliced images and coco file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
Location of the coco annotation file |
required |
|
str
|
Base directory for the images |
required |
|
str
|
File name of the exported coco datatset json. |
required |
|
str
|
Output directory |
None
|
|
bool
|
If True, images without annotations are ignored. Defaults to False. |
False
|
|
int
|
Height of each slice. Default 512. |
512
|
|
int
|
Width of each slice. Default 512. |
512
|
|
float
|
Fractional overlap in height of each slice (e.g. an overlap of 0.2 for a slice of size 100 yields an overlap of 20 pixels). Default 0.2. |
0.2
|
|
float
|
Fractional overlap in width of each slice (e.g. an overlap of 0.2 for a slice of size 100 yields an overlap of 20 pixels). Default 0.2. |
0.2
|
|
float
|
If the cropped annotation area to original annotation ratio is smaller than this value, the annotation is filtered out. Default 0.1. |
0.1
|
|
str
|
Extension of saved images. Default is the original suffix. |
None
|
|
bool
|
Switch to print relevant values to screen. Default 'False'. |
False
|
Returns:
Name | Type | Description |
---|---|---|
coco_dict |
List[Union[Dict, str]]
|
dict COCO dict for sliced images and annotations |
save_path |
List[Union[Dict, str]]
|
str Path to the saved coco file |
Source code in sahi/slicing.py
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
|
slice_image(image, coco_annotation_list=None, output_file_name=None, output_dir=None, slice_height=None, slice_width=None, overlap_height_ratio=0.2, overlap_width_ratio=0.2, auto_slice_resolution=True, min_area_ratio=0.1, out_ext=None, verbose=False)
¶
Slice a large image into smaller windows. If output_file_name and output_dir is given, export sliced images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str or Image
|
File path of image or Pillow Image to be sliced. |
required |
|
List[CocoAnnotation]
|
List of CocoAnnotation objects. |
None
|
|
str
|
Root name of output files (coordinates will be appended to this) |
None
|
|
str
|
Output directory |
None
|
|
int
|
Height of each slice. Default None. |
None
|
|
int
|
Width of each slice. Default None. |
None
|
|
float
|
Fractional overlap in height of each slice (e.g. an overlap of 0.2 for a slice of size 100 yields an overlap of 20 pixels). Default 0.2. |
0.2
|
|
float
|
Fractional overlap in width of each slice (e.g. an overlap of 0.2 for a slice of size 100 yields an overlap of 20 pixels). Default 0.2. |
0.2
|
|
bool
|
if not set slice parameters such as slice_height and slice_width, it enables automatically calculate these params from image resolution and orientation. |
True
|
|
float
|
If the cropped annotation area to original annotation ratio is smaller than this value, the annotation is filtered out. Default 0.1. |
0.1
|
|
str
|
Extension of saved images. Default is the original suffix for lossless image formats and png for lossy formats ('.jpg','.jpeg'). |
None
|
|
bool
|
Switch to print relevant values to screen. Default 'False'. |
False
|
Returns:
Name | Type | Description |
---|---|---|
sliced_image_result |
SliceImageResult
|
SliceImageResult: sliced_image_list: list of SlicedImage image_dir: str Directory of the sliced image exports. original_image_size: list of int Size of the unsliced original image in [height, width] |
Source code in sahi/slicing.py
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 350 351 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 398 399 400 401 402 403 404 405 406 407 408 409 410 |
|
Slicing Utilities¶
- Slice an image:
from sahi.slicing import slice_image
slice_image_result = slice_image(
image=image_path,
output_file_name=output_file_name,
output_dir=output_dir,
slice_height=256,
slice_width=256,
overlap_height_ratio=0.2,
overlap_width_ratio=0.2,
)
- Slice a COCO formatted dataset:
from sahi.slicing import slice_coco
coco_dict, coco_path = slice_coco(
coco_annotation_file_path=coco_annotation_file_path,
image_dir=image_dir,
slice_height=256,
slice_width=256,
overlap_height_ratio=0.2,
overlap_width_ratio=0.2,
)
Interactive Demo¶
Want to experiment with different slicing parameters and see their effects? Check out our interactive Jupyter notebook that demonstrates these slicing operations in action.