一、最邻近差值(nearest)
含义: 选取离目标点最近的点的值作为新的插入点的值。
两幅图坐标值变换关系:
(代码未验证)
for i=1:size(dist,1)
x = round(i* (size(src,1)/size(dist,1))); %dst横坐标变换到src坐标系,最临近点
for j=1:size(dist,2)
y = round(j* (size(src,2)/size(dist,2))); %dst纵坐标变换到src坐标系,最临近点
dist(i,j)=src(x,y); % 最临近点赋值
end
end
二、线性插值 (linear)
含义: 根据两点建立线性关系,以距离作为权重进行插值。
三、双线性插值(bilinear)
含义: 分别从两个方向上各进行一次线性插值。
对于p点的灰度值,可以通过p点周围Q11 = (x1, y1)、Q12 = (x1, y2), Q21 = (x2, y1) 以及 Q22 = (x2, y2) 四个点来获得。
在横坐标方向上进行两次线性插值:
根据Q11、Q21:
f
(
R
1
)
≈
x
2
−
x
x
2
−
x
1
f
(
Q
11
)
+
x
−
x
1
x
2
−
x
1
f
(
Q
21
)
where
R
1
=
(
x
,
y
1
)
f\left(R_{1}\right) \approx \frac{x_{2}-x}{x_{2}-x_{1}} f\left(Q_{11}\right)+\frac{x-x_{1}}{x_{2}-x_{1}} f\left(Q_{21}\right) \quad \text { where } R_{1}=\left(x, y_{1}\right)
f(R1)≈x2−x1x2−xf(Q11)+x2−x1x−x1f(Q21) where R1=(x,y1)
根据Q21、Q22:
f
(
R
2
)
≈
x
2
−
x
x
2
−
x
1
f
(
Q
12
)
+
x
−
x
1
x
2
−
x
1
f
(
Q
22
)
where
R
2
=
(
x
,
y
2
)
f\left(R_{2}\right) \approx \frac{x_{2}-x}{x_{2}-x_{1}} f\left(Q_{12}\right)+\frac{x-x_{1}}{x_{2}-x_{1}} f\left(Q_{22}\right) \quad \text { where } R_{2}=\left(x, y_{2}\right)
f(R2)≈x2−x1x2−xf(Q12)+x2−x1x−x1f(Q22) where R2=(x,y2)
在纵坐标方向上进行一次线性插值:
根据R1和R2: f ( P ) ≈ y 2 − y y 2 − y 1 f ( R 1 ) + y − y 1 y 2 − y 1 f ( R 2 ) f(P) \approx \frac{y_{2}-y}{y_{2}-y_{1}} f\left(R_{1}\right)+\frac{y-y_{1}}{y_{2}-y_{1}} f\left(R_{2}\right) f(P)≈y2−y1y2−yf(R1)+y2−y1y−y1f(R2)
整理得:
f
(
P
)
≈
(
y
2
−
y
)
(
x
2
−
x
)
(
y
2
−
y
1
)
(
x
2
−
x
1
)
f
(
Q
11
)
+
(
y
2
−
y
)
(
x
−
x
1
)
(
y
2
−
y
1
)
(
x
2
−
x
1
)
f
(
Q
21
)
+
(
y
−
y
1
)
(
x
2
−
x
)
(
y
2
−
y
1
)
(
x
2
−
x
1
)
f
(
Q
12
)
+
(
y
−
y
1
)
(
x
−
x
1
)
(
y
2
−
y
1
)
(
x
2
−
x
1
)
f
(
Q
22
)
\begin{aligned} f(P) & \approx \frac{\left(y_{2}-y\right)\left(x_{2}-x\right)}{\left(y_{2}-y_{1}\right)\left(x_{2}-x_{1}\right)} f\left(Q_{11}\right)+\frac{\left(y_{2}-y\right)\left(x-x_{1}\right)}{\left(y_{2}-y_{1}\right)\left(x_{2}-x_{1}\right)} f\left(Q_{21}\right) \\ &+\frac{\left(y-y_{1}\right)\left(x_{2}-x\right)}{\left(y_{2}-y_{1}\right)\left(x_{2}-x_{1}\right)} f\left(Q_{12}\right)+\frac{\left(y-y_{1}\right)\left(x-x_{1}\right)}{\left(y_{2}-y_{1}\right)\left(x_{2}-x_{1}\right)} f\left(Q_{22}\right) \end{aligned}
f(P)≈(y2−y1)(x2−x1)(y2−y)(x2−x)f(Q11)+(y2−y1)(x2−x1)(y2−y)(x−x1)f(Q21)+(y2−y1)(x2−x1)(y−y1)(x2−x)f(Q12)+(y2−y1)(x2−x1)(y−y1)(x−x1)f(Q22)
由于图像双线性插值只使用相邻4个点,因此上式分母都为1。整理公式得到:
f(i+u,j+v) = (1-u)(1-v)f(i,j) + (1-u)vf(i,j+1) + u(1-v)f(i+1,j) + uvf(i+1,j+1)
图示:
代码示例(未验证):
for i=1:size(dist,1)
x = double(i* (size(src,1)/size(dist,1)));
u = x-floor(x); %向下取整,求差值
for j=1:size(dist,2)
y = double(j* (size(src,2)/size(dist,2)));
v = y-floor(y); %向下取整,求差值
dist(i,j)=(1-u)*(1-v)*src(i,j) + (1-u)*v*src(i,j+1) + u*(1-v)*src(i+1,j) + u*v*src(i+1,j+1);
end
end
四、双三次插值(cubic)
含义: 对于某点进行插值时,考虑该点周围16个像素的影响。首先构造一个cubic函数,根据该点不同位置的cubic函数值作为权值,对该点进行插值赋值。
对待插值的像素点
(
x
,
y
)
(x,y)
(x,y)(x和y可以为浮点数),取其附近的4x4邻域点
(
x
i
,
y
j
)
,
i
,
j
=
0
,
1
,
2
,
3
(x_i,y_j), i,j = 0,1,2,3
(xi,yj),i,j=0,1,2,3。按如下公式进行插值计算:
f
(
x
,
y
)
=
∑
i
=
0
3
∑
j
=
0
3
f
(
x
i
,
y
j
)
W
(
x
−
x
i
)
W
(
y
−
y
j
)
f(x, y)=\sum_{i=0}^{3} \sum_{j=0}^{3} f\left(x_{i}, y_{j}\right) W\left(x-x_{i}\right) W\left(y-y_{j}\right)
f(x,y)=i=0∑3j=0∑3f(xi,yj)W(x−xi)W(y−yj)
代码示例(未验证):
% 构造cubic函数,取a=-1
function A=sw(w1)
w=abs(w1);
if w<1&&w>=0
A=1-2*w^2+w^3;
elseif w>=1&&w<2
A=4-8*w+5*w^2-w^3;
else
A=0;
end
for i=1:size(dist,1)
x = double(i* (size(src,1)/size(dist,1)));
u = x-floor(x); %向下取整,求差值
A=[sw(1+u) sw(u) sw(1-u) sw(2-u)];
for j=1:size(dist,2)
y = double(j* (size(src,2)/size(dist,2)));
v = y-floor(y); %向下取整,求差值
C=[sw(1+v);sw(v);sw(1-v);sw(2-v)];
B=[src(i-1,j-1) src(i-1,j) src(i-1,j+1) src(i-1,j+2)
src(i,j-1) src(i,j) src(i,j+1) src(i,j+2)
src(i+1,j-1) src(i+1,j) src(i+1,j+1) src(i+1,j+2)
src(i+2,j-1) src(i+2,j) src(i+2,j+1) src(i+2,j+2)];
dis(i,j)=(A*B*C);
end
end
备注:所有代码是为了方便加深理解算法过程写的matlab伪代码片段,未在编译器上运行测试
参考文章:
【图像插值算法总结】https://www.cnblogs.com/laozhanghahaha/p/12580822.html
【图像缩放之双三次插值法】https://blog.csdn.net/qq_29058565/article/details/52769497
【双三次插值算法详解 含python实现】https://www.cnblogs.com/wojianxin/p/12516762.html