内容发布更新时间 : 2024/12/29 14:40:07星期一 下面是文章的全部内容请认真阅读。
% Define a function for shrinking and zooming images by bilinear interpolation % I is the original image and N is the factor for shrinking firstly and zooming later function interpolation_shrink_zoom_image(I, N
% First to shrink the original image % To measure the size of original image I [P,Q]=size(I;
% Rebuild the original image I to I_TEMP for bilinear interpolation I_TEMP=zeros(P+2,Q+2; I_TEMP(2:P+1,2:Q+1=I; I_TEMP(1,2:Q+1=I(1,:; I_TEMP(P+2,2:Q+1=I(P,:; I_TEMP(2:P+1,1=I(:,1; I_TEMP(2:P+1,Q+2=I(:,Q; I_TEMP(1,1=I(1,1; I_TEMP(1,P+2=I(1,P; I_TEMP(P+2,1=I(P,1; I_TEMP(P+2,Q+2=I(P,Q; I_TEMP=double(I_TEMP; % To fix the size of shrunken image
% R is the number of rows and C is the number of columns R=floor(P/N; C=floor(Q/N; % Define the shrunken image I_S=zeros(R,C;
% For bilinear interpolation f(x,y in 4 points f(x1,y1,f(x2,y1,f(x1,y2,f(x2,y2 % f(x,y = f(x1,y1*(x2-x*(y2-y/((x2-x1*(y2-y1
% + f(x2,y1*(x-x1*(y2-y/((x2-x1*(y2-y1 % + f(x1,y2*(x2-x*(y-y1/((x2-x1*(y2-y1 % + f(x2,y2*(x-x1*(y-y1/((x2-x1*(y2-y1
% For pixel matrix, x2=x1+1, y2=y1+1, x=x1+a, y=y1+b % Let x1=u, y1=v, then x2=u+1, y2=v+1, x=u+a, y=v+b % Then f(u+a, v+b = f(u,v*(1-a*(1-b + f(u+1,v*a*(1-b % + f(u,v+1*(1-a*b + f(u+1,v+1*a*b
% Method: from original image I get suitable gray value by surrounding 4 pixels, % then fill the gray value into certain location in shrunken image I_S. for i=1:R; for j=1:C;
% Step1: get x,y,u,v,a,b. The size of I to size of I_S ratio = N, % (x,y is pixel \x=i*N; y=j*N; u=floor(x; v=floor(y; a=x-u; b=y-v;
% Step2: considering the situation on bound of I, using I_TEMP to get suitable value of f(x,y by bilinear interpolation
I_S(i,j = I_TEMP(u+1,v+1*(1-a*(1-b + I_TEMP(u+2,v+1*a*(1-b +
I_TEMP(u+1,v+2*(1-a*b + I_TEMP(u+2,v+2*a*b; end ; end ;
% Display the original image I and the shrunken image I_S I_S=uint8(I_S;
figure; imshow(I,[]; title(['Original image: ',num2str(P,'*',num2str(Q]; figure; imshow(I_S,[]; title(['Interpolation shrinking image: ' ,num2str(R,'*',num2str(C];
% Second to zoom the shrunken image
% R*C is the size of shrunken image as shown before
% Rebuild the shrunken image I_S to I_TEMP for bilinear interpolation I_TEMP=zeros(R+2,C+2; I_TEMP(2:R+1,2:C+1=I_S; I_TEMP(1,2:C+1=I_S(1,:; I_TEMP(R+2,2:C+1=I_S(R,:; I_TEMP(2:R+1,1=I_S(:,1; I_TEMP(2:R+1,C+2=I_S(:,C; I_TEMP(1,1=I_S(1,1; I_TEMP(1,R+2=I_S(1,R; I_TEMP(R+2,1=I_S(R,1; I_TEMP(R+2,R+2=I_S(R,C; I_TEMP=double(I_TEMP; % Define the zoomed image I_Z=zeros(R*N,C*N;