function [neigh_image,biliner_image] = liner_interpt1( input_image,h1,w1,flag)
%输入变量是原始的图像,以及需要缩放的目标图像的尺寸
%判断图像是否为空
if ~exist('input_image','var') || isempty(input_image)
error('输入图像input_image 未定义或为空!')
end
if ~exist('w1','var') || ~exist('h1','var')|| isempty(w1)||isempty(h1)
error('缩放因子未定义')
end
I = input_image;
I = double(I);
[IH,IW,ID] = size(I);
scale_w = IW / w1;
scale_h = IH / h1;
scale_image = zeros(h1,w1,ID);
for i = 1:1:h1
for j = 1:1:w1
x = round(i*scale_h);
y = round(j*scale_w);
if x == 0|| y == 0
x = 1;
y = 1;
end
if x > IH || y > IW
x = IH;
y = IW;
end
scale_image(i,j,:) = I(x,y,:);
end
end
neigh_image = scale_image;
%双线性插值
%对映射之后的图像进行扩展,映射之后图像的大小为w1*h1
expend_image = zeros(IH+2,IW+2,ID);
expend_image(2:IH+1,2:IW+1,:) = I(:,:,:);
expend_image(1,2:IW+1,:) = I(1,:,:);
expend_image(IH+2,2:IW+1,:) = I(IH,:,:);
expend_image(2:IH+1,1,:) = I(:,1,:);
expend_image(2:IH+1,IW+2,:) = I(:,IW,:);
expend_image(1,1) = I(1,1);
expend_image(1,IW+2) = I(1,IW);
expend_image(IH+2,1) = I(IH,1);
expend_image(IH+2,IW+2) = I(IH,IW);
%开始插值
biliner_image = zeros(h1,w1,ID);
expend_image = double(expend_image);
for i = 1:1:h1
for j = 1:1:w1
xd = i*scale_h;
yd = j*scale_w;
u = xd - floor(xd);
v = yd - floor(yd);
x1 = floor(xd);
x2 = ceil(xd);
y1 = floor(yd);
y2 = ceil(yd);
if x1 == 0 || x2 == 0 || y1 ==0 || y2 == 0
x1 = 1;
x2 = 2;
y1 = 1;
y2 = 2;
end
biliner_image(i,j,:) = (1-u)*(1-v)*expend_image(x1,y1,:) + u*(1-v)*expend_image(x2,y1,:) + v*(1-u)*expend_image(x1,y2,:) + u*v*expend_image(x2,y2,:);
end
end
biliner_image = uint8(biliner_image);
if flag == 1
figure(1);
subplot(131);imshow(uint8(I));title('原图');
subplot(132);imshow(uint8(scale_image));title('最近邻插值');
subplot(133);imshow(uint8(biliner_image));title('双线性插值');
figure(2);
imshow(uint8(scale_image));title('最近邻插值');
figure(3);
imshow(uint8(expend_image));title('扩展后的图像');
figure(4);
imshow(uint8(biliner_image));title('双线性插值');
end
end