利用双线性插值实现
zoomNum为缩放系数
height和width的命名可能反了,不要在意这些细节…
一开始用subplot输出两幅图片,明明已经得到一个更大的矩阵了但就是死活看不出效果,后来求助于助教才知道subplot会让它们输出大小一致(吐血)…
I=imread('tissue.png');
figure(1);
imshow(I)
zoomNum=0.5;
[fWidth fHeight]=size(I);
fHeight=fHeight/3;
cWidth=floor(fWidth*zoomNum);
cHeight=floor(fHeight*zoomNum);
res=zeros(cWidth,cHeight,3);
%四个顶点
res(1,1,:)=I(1,1,:);
res(1,cHeight,:)=I(1,fHeight,:);
res(cWidth,1,:)=I(fWidth,1,:);
res(cWidth,cHeight,:)=I(fWidth,fHeight,:);
%四条边界
for dstX=2:cWidth-1
srcX=dstX/zoomNum;
i=floor(srcX);u=srcX-i;
res(dstX,1,:)=u*I(i,1,:)+(1-u)*I(i+1,1,:);
res(dstX,cHeight,:)=u*I(i,fHeight,:)+(1-u)*I(i+1,fHeight,:);
end
for dstY=2:cHeight-1
srcY=dstY/zoomNum;
i=floor(srcY);u=srcY-i;
res(cWidth,dstY,:)=u*I(fWidth,i,:)+(1-u)*I(fWidth,i+1,:);
res(1,dstY,:)=u*I(1,i,:)+(1-u)*I(1,i+1,:);
end
%中间
for dstX=2:cWidth-1
for dstY=2:cHeight-1
srcX=dstX/zoomNum;
srcY=dstY/zoomNum;
i=floor(srcX);u=srcX-i;
j=floor(srcY);v=srcY-j;
res(dstX,dstY,:)=(1-u)*(1-v)*I(i,j,:)+(1-u)*v*I(i,j+1,:)+u*(1-v)*I(i+1,j,:)+u*v*I(i+1,j+1,:);
end
end
figure(2);
imshow(uint8(res));