为什么我的双线性插值与内置的matlab函数有很大不同? [英] Why is my bilinear interpolation vastly different from the in-built matlab function?

查看:401
本文介绍了为什么我的双线性插值与内置的matlab函数有很大不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在基于Matlab中的Wiki示例进行双线性插值.我将示例跟在T后面,但是将我的函数和内置的matlab函数的输出进行比较时,结果却大相径庭,我不知道为什么会发生这种情况或如何发生这种情况.

I've been working on bilinear interpolation based on wiki example in matlab. I followed the example to the T, but when comparing the outputs from my function and the in-built matlab function, the results are vastly different and I can't figure out why or how that happens.

使用内置的matlab函数:

Using inbuilt matlab function:

下面我的函数结果:

Result of my function below:

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).

The arguments passed to the function are step4 = bilinear(Igray,1668,1836); (scale factor of 3).

推荐答案

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

You are finding the pixel nearest to the point you want to interpolate, then find 4 of this pixel’s neighbors and interpolate between them:

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个像素:

Instead, find the 4 pixels nearest the point you want to interpolate:

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_row,o_col)中输出像素((row,col))的浮点坐标和输入图像(fo_row,fo_col)中最近像素的位置.然后,距离就是d_row = o_row - fo_row1-d_row

The same pixel’s coordinates then need to be used when computing distances. The easiest way to do that is to separate out the floating-point coordinates of the output pixel ((row,col)) in the input image (o_row,o_col), and the location of the nearest pixel in the input image (fo_row,fo_col). Then, the distances are simply d_row = o_row - fo_row and 1-d_row, etc.

这就是我编写此函数的方式:

This is how I would write this function:

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

这篇关于为什么我的双线性插值与内置的matlab函数有很大不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆