图像预处理-大图切割-python实现
简介
深度学习中,数据集的预处理往往是很基础的一步,很多场景都需要将一张大图进行切割。本篇提供一种重叠矩形框的生成方法,数据集中的图像尺寸可以不同,根据生成的重叠矩形框可以crop出相应的图像区域。主要难点在于函数不假设图像的尺寸大小。
实现
以下是重叠矩形框的生成函数,是根据右下角的坐标来确定左上角的坐标,如果右下角的点超过了图像边缘,则让矩形的右下角等于边缘值。循环会让右下角的坐标往右和往下多走一个stride,这样可以将边缘部分的图像也包含进来。
#encoding=utf-8
def get_fixed_windows(image_size, wind_size, overlap_size):
'''
This function can generate overlapped windows given various image size
params:
image_size (w, h): the image width and height
wind_size (w, h): the window width and height
overlap (overlap_w, overlap_h): the overlap size contains x-axis and y-axis
return:
rects [(xmin, ymin, xmax, ymax)]: the windows in a list of rectangles
'''
rects = set()
assert overlap_size[0] < wind_size[0]
assert overlap_size[1] < wind_size[1]
im_w = wind_size[0] if image_size[0] < wind_size[0] else image_size[0]
im_h = wind_size[1] if image_size[1] < wind_size[1] else image_size[1]
stride_w = wind_size[0] - overlap_size[0]
stride_h = wind_size[1] - overlap_size[1]
for j in range(wind_size[1]-1, im_h + stride_h, stride_h):
for i in range(wind_size[0]-1, im_w + stride_w, stride_w):
right, down = i+1, j+1
right = right if right < im_w else im_w
down = down if down < im_h else im_h
left = right - wind_size[0]
up = down - wind_size[1]
rects.add((left, up, right, down))
return list(rects)
if __name__ == "__main__":
image_size = (1780, 532)
wind_size = (800, 600)
overlap_size = (300, 200)
rets = get_fixed_windows(image_size, wind_size, overlap_size)
for rect in rets:
print(rect)
'''
# output
(0, 0, 800, 600)
(500, 0, 1300, 600)
(980, 0, 1780, 600)
'''
效果
总结
实在不知道写什么了,把之前项目里的一个图像预处理代码po出来。嗯🤔,还是要坚持定时写点东西。