1.最邻近法,在进行采样后的原图像的每个坐标变为浮点型,先进行四舍五入寻找整数值然后利用公式寻找原图像对应的像素,采样后的(i,j)对应原图像中的((i/l),(j/l))取整。
row=image.shape[0]
col=image.shape[1]
channels=image.shape[2]
l=2.0#放大缩小系数
'''
重采样三种方法
最邻近法
双线性内插法
三次卷积内插法
'''
def NearestNeighbour(image,l):
newrow=round(row*l)
newcol=round(col*l)
img=np.zeros((newrow,newcol,channels),np.uint8)
for i in range(newrow):
for j in range(newcol):
x=int(i/l)
y=int(j/l)
img[i,j]=image[x,y]
return img
img=NearestNeighbour(image,l)
cv2.imshow('NearestNeighbour',img)
cv2.waitKey(0)
2.双线性内插法,选择重采样后的大小然后找到变化因子,寻找之前的原图像的坐标应该是浮点数,然后向下取整,找到权重,对其周围四个区域分不同的权重相乘
def bilinear(src_img, dst_shape):
"""
双线性插值法,来调整图片尺寸
:param org_img: 原始图片
:param dst_shape: 调整后的目标图片的尺寸
:return: 返回调整尺寸后的图片矩阵信息
"""
dst_img = np.zeros((dst_shape[0], dst_shape[1], 3), np.uint8)
dst_h, dst_w = dst_shape
src_h = src_img.shape[0]
src_w = src_img.shape[1]
# i:纵坐标y,j:横坐标x
# 缩放因子,dw,dh
scale_w = src_w / dst_w
scale_h = src_h / dst_h
for i in range(dst_h):
for j in range(dst_w):
src_x = float((j + 0.5) * scale_w - 0.5)
src_y = float((i + 0.5) * scale_h - 0.5)
# 向下取整,代表靠近源点的左上角的那一点的行列号
src_x_int = math.floor(src_x)
src_y_int = math.floor(src_y)
# 取出小数部分,用于构造权值
src_x_float = src_x - src_x_int
src_y_float = src_y - src_y_int
if src_x_int + 1 == src_w or src_y_int + 1 == src_h:
dst_img[i, j, :] = src_img[src_y_int, src_x_int, :]
continue
dst_img[i, j, :] = (1. - src_y_float) * (1. - src_x_float) * src_img[src_y_int, src_x_int, :] + \
(1. - src_y_float) * src_x_float * src_img[src_y_int, src_x_int + 1, :] + \
src_y_float * (1. - src_x_float) * src_img[src_y_int + 1, src_x_int, :] + \
src_y_float * src_x_float * src_img[src_y_int + 1, src_x_int + 1, :]
return dst_img
3.三次卷积内插
有点复杂