def split_tiles(image, shape, overlap=16):
""" Rescale and split the input images to get several overlapping images of a given shape.
*** The inpput must be CHANNELS FIRST ***
The input image is rescaled so that height matches the output height.
It is split into possibly overlapping tiles, each sized to match the output shape
"""
# image_channels = image.shape[0]
image_height = image.shape[-2]
# image_width = image.shape[-1]
output_height = shape[-2]
output_width = shape[-1]
# Rescale to match vertical size
scale = output_height / float(image_height)
scaled_image = rescale(image.transpose(1, 2, 0), (scale, scale), order=0, preserve_range=True).transpose(2, 0, 1)
scaled_width = scaled_image.shape[-1]
if scaled_width < output_width:
padding = output_width - scaled_width
if len(scaled_image.shape) == 3:
scaled_image = np.pad(scaled_image, ((0, 0), (0, 0), (padding / 2, padding - padding / 2)), mode='constant')
else:
scaled_image = np.pad(scaled_image, ((0, 0), (padding / 2, padding - padding / 2)), mode='constant')
# Since the input is not a multiple of the output width, we will evenly divide the image
# to produce overlapping tiles. Work it out.
# -- The last tile always fits, and does not overlap with the _next_ tile (there is none)
# -- The remaining tiles each overlap with the following tile. The width of uncovered portion
# evenly divides the rest of the strip
# -- I need an integer number of tiles to cover the remaining strip (so I use a ceil)
num_tiles = 1 + int(np.ceil(max(0, (scaled_width - output_width)) / float(output_width - overlap)))
for x in np.linspace(0, scaled_width - output_width, num_tiles):
yield scaled_image[:, :, int(x):int(x) + output_width]
评论列表
文章目录