为什么我的双线性插值与内置的matlab函数有很大的不同?

为什么我的双线性插值与内置的matlab函数有很大的不同?,matlab,image-processing,bilinear-interpolation,Matlab,Image Processing,Bilinear Interpolation,我一直在研究基于matlab中wiki示例的双线性插值。我按照例子中的T,但是当比较我的函数和内置的matlab函数的输出时,结果有很大的不同,我不知道为什么或者如何发生 使用内置的matlab函数: 我的以下功能的结果: 传递给函数的参数是step4=双线性(Igray,16681836);(比例因子为3)。找到离要插值点最近的像素,然后找到该像素的4个相邻像素并在它们之间插值: o_row = ceil(row/hr); o_col = ceil(col/wr); Q12=X(o_row

我一直在研究基于matlab中wiki示例的双线性插值。我按照例子中的T,但是当比较我的函数和内置的matlab函数的输出时,结果有很大的不同,我不知道为什么或者如何发生

使用内置的matlab函数:

我的以下功能的结果:


传递给函数的参数是step4=双线性(Igray,16681836);(比例因子为3)。

找到离要插值点最近的像素,然后找到该像素的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);
计算距离时,需要使用相同像素的坐标。最简单的方法是分离出输入图像
(o_行,o_列)
中输出像素(
(行,列)
)的浮点坐标,以及输入图像
(o_行,列)
中最近像素的位置。然后,距离就是
d_row=o_row-fo_row
1-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

另外,
uint8(零(h,w))
创建一个双数组并将其与uint8协调。最好使用
零(h,w,'uint8')
。删除相关代码通常被视为故意破坏。请不要这样做。通过在Stack Exchange(SE)网络上发布,您已根据授予SE分发该内容的不可撤销权利(即,无论您未来的选择如何)。根据SE政策,该帖子的非故意破坏版本是发布的版本。因此,任何故意破坏行为都将恢复原状。如果您想了解有关删除帖子的更多信息,请参阅:@Vocaloidas,因为这是错误的。您使用4个相邻像素进行插值,而不是相邻像素。@Vocaloidas:我已经更新了答案,并建议如何正确计算距离。我还包括了对您的代码的修改,以确保我没有告诉您什么错误。:)
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