gpt4 book ai didi

python-3.x - 逐 block 迭代加载图像,其中 block 部分重叠

转载 作者:行者123 更新时间:2023-12-04 12:16:09 25 4
gpt4 key购买 nike

试图处理较大的卫星图像(约10GB)。为了提高内存效率,每次迭代都会将图像块(block/tile)加载到内存中。

Tiled Image

此示例代码如下:

def process_image(src_img, dst_img, band_id=1):
with rasterio.open(src_img) as src:
kwargs = src.meta
tiles = src.block_windows(band_id)
with rasterio.open(dst_img, 'w', **kwargs) as dst:
for idx, window in tiles:
print("Processing Block: ", idx[0]+1, ", ", idx[1]+1)
src_data = src.read(band_id, window=window)
dst_data = src_data ** 2 # Do the Processing Here
dst.write_band( band_id, dst_data, window=window)
return 0

但是,对于需要进行内核操作的任何类型的处理(例如,像 convolve之类的任何 smoothing过滤器),这都会导致在块边缘附近进行处理的问题。为了解决这个问题,每个块应略有重叠,如下所示:

Overlapped Zoomed

我的目标是按以下方式加载块,其中可以根据需要调整重叠量。

Overlapped Tile

到目前为止,我还没有找到实现这一目标的任何直接方法。在这方面的任何帮助,我将不胜感激。

最佳答案

您可以扩展窗口:

import rasterio as rio
from rasterio import windows

def overlapping_blocks(src, overlap=0, band=1):
nols, nrows = src.meta['width'], src.meta['height']
big_window = windows.Window(col_off=0, row_off=0, width=nols, height=nrows)
for ji, window in src.block_windows(band):

if overlap == 0:
yield ji, window

else:
col_off = window.col_off - overlap
row_off = window.row_off - overlap
width = window.width + overlap * 2
height = window.height + overlap * 2
yield ji, windows.Window(col_off, row_off, width, height).intersection(big_window)

您将像这样使用它:
def process_image(src_img, dst_img, band_id=1):
with rasterio.open(src_img) as src:
kwargs = src.meta
with rasterio.open(dst_img, 'w', **kwargs) as dst:
for idx, window in overlapping_block_windows(src, overlap=1, band=band_id):
print("Processing Block: ", idx[0]+1, ", ", idx[1]+1)
src_data = src.read(band_id, window=window)
dst_data = src_data ** 2 # Do the Processing Here
dst.write_band( band_id, dst_data, window=window)
return 0

这是重叠块窗口和非块窗口的一种方法,并带有附加参数 boundless来指定是否将窗口扩展到栅格范围之外,这对 boundless reading很有用:
from itertools import product
import rasterio as rio
from rasterio import windows


def overlapping_windows(src, overlap, width, height, boundless=False):
""""width & height not including overlap i.e requesting a 256x256 window with
1px overlap will return a 258x258 window (for non edge windows)"""
offsets = product(range(0, src.meta['width'], width), range(0, src.meta['height'], height))
big_window = windows.Window(col_off=0, row_off=0, width=src.meta['width'], height=src.meta['height'])
for col_off, row_off in offsets:

window = windows.Window(
col_off=col_off - overlap,
row_off=row_off - overlap,
width=width + overlap * 2,
height=height + overlap * 2)

if boundless:
yield window
else:
yield window.intersection(big_window)


def overlapping_blocks(src, overlap=0, band=1, boundless=False):

big_window = windows.Window(col_off=0, row_off=0, width=src.meta['width'], height=src.meta['height'])
for ji, window in src.block_windows(band):

if overlap == 0:
yield window

else:
window = windows.Window(
col_off=window.col_off - overlap,
row_off=window.row_off - overlap,
width=window.width + overlap * 2,
height=window.height + overlap * 2)
if boundless:
yield window
else:
yield window.intersection(big_window)

输出窗口:
enter image description here

关于python-3.x - 逐 block 迭代加载图像,其中 block 部分重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54501232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com