简体   繁体   English

将图像分割成 m*n 块重叠

[英]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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python在重叠和旋转的瓷砖中分割图像 - python split image in overlapping and rotating tiles 使用 Python 和 Numpy 创建原始图像的图像图块 (m*n) - Creating image tiles (m*n) of original image using Python and Numpy ImageMagick 将图像分割为六边形图块 - ImageMagick split image to hexagon tiles 如何将图像分割成三角形瓷砖? - How to split an image into triangular tiles? QPixmap的图块(图像)具有重叠的区域 - Tiles of a QPixmap (image) have overlapping regions 如何将图像划分为大小均匀、需要时重叠的图块? - How to divide an image into evenly sized, overlapping if needed, tiles? 将列表拆分为均匀大小的重叠块 n-max - Split list into even size overlapping chunks n-max 如何在python中将图像从(n,m,4)重塑为(n,m,3)? - How to reshape image from (n,m,4) to (n,m,3) in python? 给定一个大小为 m*n 的输入图像 X,一个大小为 T 的块,如何生成 k 个大小为 T*T 的随机非重叠块? - Given an input image X of size m*n, a block size of T, how to generate k random non-overlapping blocks of size T*T? 在 python 中,更快/更懒的方式将 m*n 均匀随机分成 n 组(每个组有 m 个元素) - Faster/lazier way to evenly and randomly split m*n into n group (each has m elements) in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM