|
import numpy as np |
|
from typing import Literal |
|
|
|
def split_image(image: np.ndarray, width_parts: int, height_parts: int, result: Literal['np.ndarray', 'bboxes'] = 'np.ndarray'): |
|
tile_width = image.shape[0] // width_parts |
|
tile_height = image.shape[1] // height_parts |
|
for height in range(height_parts): |
|
for width in range(width_parts): |
|
width_start = width * tile_width |
|
width_end = tile_width * (width + 1) if (width + 1) < width_parts else image.shape[0] |
|
height_start = height * tile_height |
|
height_end = tile_height * (height + 1) if (height + 1) < height_parts else image.shape[1] |
|
if result == 'np.ndarray': |
|
|
|
yield image[height_start:height_end, width_start:width_end] |
|
else: |
|
|
|
yield (width_start, height_start, width_end, height_end) |
|
|
|
|