[英]Split an image into m*n tiles with overlapping
Currently, I can split the image but having trouble doing the overlapping.目前,我可以分割图像,但无法进行重叠。 The overlapping logic is shown in the image below.
重叠逻辑如下图所示。 Ideally, overlapping can be set using the overlap variable which ranges from 0 to 1.
理想情况下,可以使用范围从 0 到 1 的重叠变量来设置重叠。
import numpy as np
from PIL import Image
rows = 2
columns = 3
overlap = 0.3
def split_image_to_tiles(image):
tiles = np.array(image)
tiles = np.array_split(tiles, rows, axis=1)
tiles = [np.array_split(t, columns, axis=0) for t in tiles]
tiles = np.array(tiles, dtype='uint8')
return tiles
def overlap_tiles(tiles):
for i in range(rows):
for j in range(columns):
tile = Image.fromarray(tiles[i][j])
#overlapping logic
tiles[i][j] = np.array(tile)
return tiles
if __name__ == '__main__':
image = Image.open('test')
tiles = split_image_to_tiles(image)
overlap_tiles(tiles)
for i in range(rows):
for j in range(columns):
tile = Image.fromarray(tiles[i][j])
tile.show()
You could use view_as_windows in the following way:您可以通过以下方式使用view_as_windows :
from skimage.util.shape import view_as_windows
def split_to_overlapping_windows(image, window_size, stride):
image_windows = view_as_windows(image,
window_shape=(window_size, window_size, image.shape[-1]),
step=stride).squeeze()
window_layout_shape = image_windows.shape[0:2]
image_windows = image_windows.reshape(-1, *image_windows.shape[2:])
return image_windows, window_layout_shape
view_as_windows
is very memory intensive.view_as_windows
非常密集。 (read the note in the doc)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.