图像预处理-⼤图切割-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
alertbehave是什么意思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 = t()
asrt overlap_size[0] < wind_size[0]
asrt overlap_size[1] < wind_size[1]
im_w = wind_size[0] if image_size[0] < wind_size[0] el image_size[0]
perfectgirls
im_h = wind_size[1] if image_size[1] < wind_size[1] el 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 el im_w彼嘉卸妆油
down = down if down < im_h el 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)
conveyance(980, 0, 1780, 600)
'''
词典在线效果moulding
总结
实在不知道写什么了,把之前项⽬⾥的⼀个图像预处理代码po出来。嗯 ,还是要坚持定时写点东西。