圖像内插
假設一幅大小為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 >= 0 && x < width && y >= 0 && y < height)
return 1;
else
return 0;
}
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 = 0; i < out_height; i++){
for (int j = 0; j < out_width; j++){
x = j / w_times;
y = i / h_times;
x1 = (short)(x - 1);
x2 = (short)(x + 1);
y1 = (short)(y + 1);
y2 = (short)(y - 1);
f11 = is_in_array(x1, y1, height, width) ? in_array[y1][x1] : 0;
f12 = is_in_array(x1, y2, height, width) ? in_array[y2][x1] : 0;
f21 = is_in_array(x2, y1, height, width) ? in_array[y1][x2] : 0;
f22 = is_in_array(x2, y2, height, width) ? in_array[y2][x2] : 0;
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)));
}
}
}