3

我一直在基于 matlab 中的 wiki 示例进行双线性插值。我按照 T 的示例进行操作,但是在比较我的函数和内置 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)。

4

1 回答 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)最简单的方法是分离出输入图像中输出像素 ( )的浮点坐标,(o_row,o_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
于 2018-11-14T14:29:32.117 回答