为什么我的双线性插值与Matlab内置函数差别巨大?

3
我在Matlab中根据维基百科的示例开发了基于双线性插值的程序。我完全按照这个示例的要求进行操作,但是与内置的Matlab函数相比较时,输出结果差距很大,我无法弄清楚为什么或者如何出现这种情况。
使用内置的Matlab函数得到的结果如下图所示: 无标题 而我的函数得到的结果如下图所示,图像模糊不清: 无标题
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)。

1
另外,uint8(zeros(h,w))会创建一个双精度数组并将其转换为uint8。最好使用zeros(h,w,'uint8') - Cris Luengo
删除相关的代码通常被认为是破坏行为,请不要这样做。通过在 Stack Exchange(SE)网络上发布内容,您已经授予了一个不可撤销的权利,在CC BY-SA 3.0许可证下,使SE可以分发该内容(即使您将来改变选择)。根据SE政策,非破坏性版本的帖子是分发的版本。因此,任何破坏行为都将被撤销。如果您想了解更多有关删除帖子的信息,请参见:删除如何工作?... - Makyen
1个回答

4

您需要找到最接近要插值的点的像素,然后找到该像素的4个邻居并在它们之间进行插值:

o_row = ceil(row/hr);
o_col = ceil(col/wr);
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);

相反,找到距离你想要插值的点最近的4个像素:

o_row = ceil(row/hr);
o_col = ceil(col/wr);
Q12=X(o_row,o_col-1);
Q22=X(o_row,o_col);
Q11=X(o_row-1,o_col-1);
Q21=X(o_row-1,o_col);

同一个像素的坐标需要在计算距离时使用。最简单的方式是将输出像素((row,col))的浮点坐标与输入图像中最近的像素位置(fo_row,fo_col)分开。然后,距离可以简单地计算为d_row = o_row - fo_row1-d_row等等。
以下是我会编写这个函数的方式:
function T = bilinear(X,h,w)
% Pre-allocating the output size
T = zeros(h,w,'uint8');            % Create the matrix in the right type, rather than cast !!
% Calculating dimension ratios 
hr = h/size(X,1);                  % Not with the padded sizes!!
wr = w/size(X,2);
% Padding the original image with 0 so I don't go out of bounds
pad = 2;
X = padarray(X,[pad,pad],'both');
% Loop
for col = 1:w                      % Looping over the row in the inner loop is faster!!
   for row = 1:h
      % For calculating equivalent position on the original image
      o_row = row/hr;
      o_col = col/wr;
      fo_row = floor(o_row);       % Code is simpler when using floor here !!
      fo_col = floor(o_col);
      % Getting the intensity values from horizontal neighbors
      Q11 = double(X(fo_row  +pad, fo_col  +pad));  % Indexing taking padding into account !!
      Q21 = double(X(fo_row+1+pad, fo_col  +pad));  % Casting to double might not be necessary, but MATLAB does weird things with integer computation !!
      Q12 = double(X(fo_row  +pad, fo_col+1+pad));
      Q22 = double(X(fo_row+1+pad, fo_col+1+pad));
      % Calculating the relative positions to the enlarged image
      d_row = o_row - fo_row;
      d_col = o_col - fo_col;
      % Interpolating on 2 first axis and the result between them
      R1 = (1-d_row)*Q11 + d_row*Q21;
      R2 = (1-d_row)*Q12 + d_row*Q22;
      T(row,col) = round((1-d_col)*R1 + d_col*R2);
   end
end
end

@Vocaloidas,那是错误的。你需要使用4个相邻像素进行插值,而不是邻居。 - Ander Biguri
@Vocaloidas:我已经更新了答案,并提出了如何正确计算距离的建议。我还包括了我对你的代码所做的修改,只是为了确保我没有告诉你错误的东西。 :) - Cris Luengo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接