我在Matlab中根据维基百科的示例开发了基于双线性插值的程序。我完全按照这个示例的要求进行操作,但是与内置的Matlab函数相比较时,输出结果差距很大,我无法弄清楚为什么或者如何出现这种情况。
使用内置的Matlab函数得到的结果如下图所示:
而我的函数得到的结果如下图所示,图像模糊不清:
函数接收的参数是 step4 = bilinear(Igray,1668,1836); (比例因子为3)。
使用内置的Matlab函数得到的结果如下图所示:
![无标题](https://istack.dev59.com/hCAFj.webp)
![无标题](https://istack.dev59.com/4Ng7Y.webp)
function T = bilinear(X,h,w)
%pre-allocating the output size
T = uint8(zeros(h,w));
%padding the original image with 0 so i don't go out of bounds
X = padarray(X,[2,2],'both');
%calculating dimension ratios
hr = h/size(X,1);
wr = w/size(X,2);
for row = 3:h-3
for col = 3:w-3
%for calculating equivalent position on the original image
o_row = ceil(row/hr);
o_col = ceil(col/wr);
%getting the intensity values from horizontal neighbors
Q12=X(o_row+1,o_col-1);
Q22=X(o_row+1,o_col+1);
Q11=X(o_row-1,o_col-1);
Q21=X(o_row-1,o_col+1);
%calculating the relative positions to the enlarged image
y2=round((o_row-1)*hr);
y=round(o_row*hr);
y1=round((o_row+1)*hr);
x1=round((o_col-1)*wr);
x=round(o_col*wr);
x2=round((o_col+1)*wr);
%interpolating on 2 first axis and the result between them
R1=((x2-x)/(x2-x1))*Q11+((x-x1)/(x2-x1))*Q21;
R2=((x2-x)/(x2-x1))*Q12+((x-x1)/(x2-x1))*Q22;
P=round(((y2-y)/(y2-y1))*R1+((y-y1)/(y2-y1))*R2);
T(row,col) = P;
T = uint8(T);
end
end
end
函数接收的参数是 step4 = bilinear(Igray,1668,1836); (比例因子为3)。
uint8(zeros(h,w))
会创建一个双精度数组并将其转换为uint8。最好使用zeros(h,w,'uint8')
。 - Cris Luengo