图像内插
假设一幅大小为500 * 500的图像扩大1.5倍到750 * 750,创建一个750 * 750 的网格,使其与原图像间隔相同,然后缩小至原图大小,在原图中寻找最接近的像素(或周围的像素)进行赋值,最后再将结果放大
最邻近内插法
寻找最近的像素赋值
双线性内插法
v(x,y) = ax + by + cxy + d
双线性内插法参数计算
已知Q11, Q12, Q21, Q22,要插值的点为P点,首先在x轴上,对R1,R2两个点进行插值
然后根据R1和R2对P点进行插值
化简得
对于边界值的处理,若x1 < 0 ,则直接令f(Q11), f(Q12) = 0
处理结果
原图
扩大为6000 * 4000
缩小为1000 * 500
下面为代码实现的主要部分
int is_in_array(short x, short y, short height, short width)
{
if (x >= && x < width && y >= && y < height)
return ;
else
return ;
} void bilinera_interpolation(short** in_array, short height, short width,
short** out_array, short out_height, short out_width)
{
double h_times = (double)out_height / (double)height,
w_times = (double)out_width / (double)width;
short x1, y1, x2, y2, f11, f12, f21, f22;
double x, y; for (int i = ; i < out_height; i++){
for (int j = ; j < out_width; j++){
x = j / w_times;
y = i / h_times;
x1 = (short)(x - );
x2 = (short)(x + );
y1 = (short)(y + );
y2 = (short)(y - );
f11 = is_in_array(x1, y1, height, width) ? in_array[y1][x1] : ;
f12 = is_in_array(x1, y2, height, width) ? in_array[y2][x1] : ;
f21 = is_in_array(x2, y1, height, width) ? in_array[y1][x2] : ;
f22 = is_in_array(x2, y2, height, width) ? in_array[y2][x2] : ;
out_array[i][j] = (short)(((f11 * (x2 - x) * (y2 - y)) +
(f21 * (x - x1) * (y2 - y)) +
(f12 * (x2 - x) * (y - y1)) +
(f22 * (x - x1) * (y - y1))) / ((x2 - x1) * (y2 - y1)));
}
}
}