www.gusucode.com > 《图像配准技术及其Matlab编程实现》--源码程序 > 《图像配准技术及其Matlab编程实现》/match/NearestInterpolation.m

    function [newImage]=NearestInterpolation(oldImage,w,z)
%参数:oldImage:待插值图像
%      w:反射变换参数x0(与oldImage同维数组)
%      z:反射变换参数y0(与oldImage同维数组)
%      newImage:插值后图像
%功能:利用最近邻插值法根据几何变换参数进行灰度级插值
[width,height]=size(oldImage);
newImage=uint8(zeros(width,height));
for i=1:width
    for j=1:height
        source_x=w(i,j);
        source_y=z(i,j);
        if(source_x>=width-1 || source_y>=height-1 || double(uint16(source_x))<=0 || double(uint16(source_y))<=0)
            newImage(i,j)=0;
        else
            if(source_x/double(uint16(source_x))==1.0) & (source_y/double(uint16(source_y))==1.0)
                newImage(i,j)=oldImage(int16(source_x),int16(source_y));
            else
                a=double(round(source_x));
                b=double(round(source_y));
                newImage(i,j)=oldImage(a,b);
            end
        end
    end
end
figure,imshow(newImage);

%RunInterpolation('ct.bmp',1);
%RunInterpolation('lena.bmp',1);