Yield successive tuples of upper left corner of patches of size `patch_size` from an array of dimensions `image_size`. The iteration starts from position `start_pos` in the array, or starting at the origin if this isn't provided. Each patch is chosen in a contiguous grid using a rwo-maj
(
image_size: Sequence[int],
patch_size: Sequence[int] | int | np.ndarray,
start_pos: Sequence[int] = (),
overlap: Sequence[float] | float | Sequence[int] | int = 0.0,
padded: bool = False,
)
| 205 | |
| 206 | |
| 207 | def iter_patch_position( |
| 208 | image_size: Sequence[int], |
| 209 | patch_size: Sequence[int] | int | np.ndarray, |
| 210 | start_pos: Sequence[int] = (), |
| 211 | overlap: Sequence[float] | float | Sequence[int] | int = 0.0, |
| 212 | padded: bool = False, |
| 213 | ): |
| 214 | """ |
| 215 | Yield successive tuples of upper left corner of patches of size `patch_size` from an array of dimensions `image_size`. |
| 216 | The iteration starts from position `start_pos` in the array, or starting at the origin if this isn't provided. Each |
| 217 | patch is chosen in a contiguous grid using a rwo-major ordering. |
| 218 | |
| 219 | Args: |
| 220 | image_size: dimensions of array to iterate over |
| 221 | patch_size: size of patches to generate slices for, 0 or None selects whole dimension |
| 222 | start_pos: starting position in the array, default is 0 for each dimension |
| 223 | overlap: the amount of overlap of neighboring patches in each dimension. |
| 224 | Either a float or list of floats between 0.0 and 1.0 to define relative overlap to patch size, or |
| 225 | an int or list of ints to define number of pixels for overlap. |
| 226 | If only one float/int number is given, it will be applied to all dimensions. Defaults to 0.0. |
| 227 | padded: if the image is padded so the patches can go beyond the borders. Defaults to False. |
| 228 | |
| 229 | Yields: |
| 230 | Tuples of positions defining the upper left corner of each patch |
| 231 | """ |
| 232 | |
| 233 | # ensure patchSize and startPos are the right length |
| 234 | ndim = len(image_size) |
| 235 | patch_size_ = get_valid_patch_size(image_size, patch_size) |
| 236 | start_pos = ensure_tuple_size(start_pos, ndim) |
| 237 | overlap = ensure_tuple_rep(overlap, ndim) |
| 238 | |
| 239 | # calculate steps, which depends on the amount of overlap |
| 240 | if isinstance(overlap[0], float): |
| 241 | steps = tuple(round(p * (1.0 - o)) for p, o in zip(patch_size_, overlap)) |
| 242 | else: |
| 243 | steps = tuple(p - o for p, o in zip(patch_size_, overlap)) |
| 244 | |
| 245 | # calculate the last starting location (depending on the padding) |
| 246 | end_pos = image_size if padded else tuple(s - round(p) + 1 for s, p in zip(image_size, patch_size_)) |
| 247 | |
| 248 | # collect the ranges to step over each dimension |
| 249 | ranges = starmap(range, zip(start_pos, end_pos, steps)) |
| 250 | |
| 251 | # choose patches by applying product to the ranges |
| 252 | return product(*ranges) |
| 253 | |
| 254 | |
| 255 | def iter_patch( |
no test coverage detected
searching dependent graphs…