简单来讲,计算机视觉需要将图片大小统一,这样才能输入到网络。
这个代码就是将2D或者3D的图片padding 到指定大小,边缘填充使用指定常数,
如果是3D,(512,512,3)最后一维不进行拓展
我加了注释,如果拓展的不能整除,那么左,上,也就是序号小的比序号大的差一维
rar = crop_to((5,5),np.array([[1,3,4,5],[2,3,3,3]]))
print(rar)
输出:
[[21 21 21 21]
[21 21 21 21]
[ 1 3 4 5]
[ 2 3 3 3]
[21 21 21 21]]
import numpy as np
def crop_to(tag_shape=(512,512), org_arr=np.array([0,0]),constant_value=21):
"""
Padding a 2D array org_arr to a arbitrary tag_shape with a constant_value .
If the horizon and vertical gap not divided by 2
,then left0>right0
and up0>right0
Only for 2D
Args:
tag_shape:1D array,int, target shape suppose bigger than origin array shape
org_arr: 2D array prepared to be pad with 0 to shape equals to org_arr