最近邻插值是一种简单的插值方法,它选择离目标点最近的已知点的值作为新插入点的值。当源图像大小为
S
r
c
W
i
d
t
h
SrcWidth
SrcWidth和
S
r
c
H
e
i
g
h
t
SrcHeight
SrcHeight,目标图像大小为
D
s
t
W
i
d
t
h
DstWidth
DstWidth和
D
s
t
H
e
i
g
h
t
DstHeight
DstHeight,或者涨缩比例为
S
x
S_x
Sx和
S
y
S_y
Sy,则计算目标图像任意像素横纵坐标
d
s
t
x
dst_x
dstx和
d
s
t
y
dst_y
dsty如下:
如果
S
x
S_x
Sx和
S
y
S_y
Sy均为0,则先计算
S
x
S_x
Sx和
S
y
S_y
Sy
S
x
=
r
o
u
n
d
(
D
s
t
W
i
d
t
h
/
S
r
c
W
i
d
t
h
)
S_{x}=round(DstWidth/SrcWidth)
Sx=round(DstWidth/SrcWidth) //水平涨缩比例
S
y
=
r
o
u
n
d
(
D
s
t
H
e
i
g
h
t
/
S
r
c
H
e
i
g
h
t
)
S_{y}=round(DstHeight/SrcHeight)
Sy=round(DstHeight/SrcHeight) //竖直涨缩比例
计算目标图像->源图像的涨缩比例
S
x
i
n
v
=
1.
/
S
x
S_{xinv}=1./S_x
Sxinv=1./Sx //反水平涨缩比例
S
y
i
n
v
=
1.
/
S
y
S_{yinv}=1./S_y
Syinv=1./Sy //反竖直涨缩比例
计算目标像素在源图像中的坐标,注意边界条件
d
s
t
x
=
r
o
u
n
d
(
s
r
c
x
∗
S
x
i
n
v
)
dst_x=round(src_x*S_{xinv})
dstx=round(srcx∗Sxinv) //水平坐标
d
s
t
y
=
r
o
u
n
d
(
s
r
c
y
∗
S
y
i
n
v
)
dst_y=round(src_y*S_{yinv})
dsty=round(srcy∗Syinv) //竖直坐标
计算目标图像像素值
I
D
s
t
(
x
,
y
)
=
I
S
r
c
(
d
s
t
x
,
d
s
t
y
)
I_{Dst}(x,y)=I_{Src}(dst_x,dst_y)
IDst(x,y)=ISrc(dstx,dsty)
计算坐标偏差 点P坐标
(
x
,
y
)
(x,y)
(x,y),
t
,
u
t,u
t,u为小数部分坐标,
t
=
x
−
x
0
,
u
=
y
−
y
0
t=x-x0,u=y-y0
t=x−x0,u=y−y0
先计算水平方向 Q1,Q2线性插值:
I
Q
11
=
I
(
x
0
,
y
0
)
∗
(
1
−
t
)
+
I
(
x
1
,
y
0
)
∗
t
I_{Q11} = I(x_0,y_0) * (1-t) + I(x_1, y_0) * t
IQ11=I(x0,y0)∗(1−t)+I(x1,y0)∗t Q3,Q4线性插值:
I
Q
22
=
I
(
x
0
,
y
1
)
∗
(
1
−
t
)
+
I
(
x
1
,
y
1
)
∗
t
I_{Q22} = I(x_0, y_1) * (1-t) + I(x_1,y_1) *t
IQ22=I(x0,y1)∗(1−t)+I(x1,y1)∗t
利用Q11, Q22的值,进行垂直方向权重计算得出计算采样点值
I
D
s
t
(
x
,
y
)
=
I
Q
11
∗
(
1
−
u
)
+
I
Q
22
∗
u
I_{Dst}(x,y) = I_{Q11}*(1-u) + I_{Q22} * u
IDst(x,y)=IQ11∗(1−u)+IQ22∗u