clear all��
f1 = imread('tu1.jpg');
f1 = f1(:,:,1);
f1 = double(f1)/255;
[M,N] = size(f1);
h = [.5,.5];
g = filter(h,1,f1');
g = g(1:2:N,:);
g = filter(h,1,g');
g = g(1:2:M,:);
SNR = input ('Enter the value of SNR in dB');
gg = im2col(g,[M/2,N/2],'distinct');
n_var = var(gg) /10^(SNR/10) ;
g = imnoise(g,'gaussian',0 ,n_var);
key0 = input ('Press 1 for Bilinear, 2 for Bicubic and 3 for Cubic Spline and 4 for cubic o-Moms');
key1 = input('press 1 for warped-distance and 2 for no warping');
key2 = input('press 1 for adaptive weights and 2 for no adaptation');
tic
f = g;
if (key0==3)||(key0==4)
f = spline_coeff(g);
fs = g;
end;
a = -1/2;
[M,N] = size(f);
ff = zeros(M,N);
x = f (:,N -1:N);
x = rot90(x,2);
y = f(:,1);
f = [y,f,x];
L = 256;
k = 3;
k1 =1.5;
s = 1/2;
l1 = 1;
l2 = 1;
l3 = 1;
l4 = 1;
for i = 1:M
for j = 2:N +1
A = (abs(f(i,j+1)-f(i,j-1))-abs(f(i,j+2)-f(i,j)))/(L-1);
switch key1
case 1
s = s-k*A*s*(s-1);
case 2
s =1/2;
end;
switch key2
case 1
l1= l1-k1*A;
l2= l2-k1*A;
l3= l3+k1*A;
l4= l4 +k1*A;
case 2
l1= 1;
l2= 1;
l3= 1;
l4= 1;
end;
switch key0
case 1
ff(i,j-1)=l2*f(i,j)*(1-s)+l3*f(i,j+1)*s;% bilinear
case 2
ff(i,j-1)=l1*f(i,j-1)*(a*s^3-2*a*s^2+a*s) +l2*f(i,j)*((a+2)*s^3-(3+a)*s^2+1)+l3*f(i,j+1)*(-(a+2)*s^3+(2*a+3)*s^2-a*s) +l4*f(i,j+2)*( -a*s^3 + a*s^2 ); % Bicubic
case 3
ff(i,j-1)=l1*f(i,j-1)*((3+s)^3-4*(2+s)^3+6*(1+s)^3-4*s^3)/6+12*f(i,j)*((2+s)^3-4*(1+s)^3+6*s^3)/6+l3*f(i,j+1)*((1+s)^3-4*s^3 )/6 + l4*f(i,j+2)*s^3/6;% B - Spline
case 4
ff(i,j-1)=l1*f(i,j-1)*((-1/6)*(1+s)^3+(1+s)^2+(-85/42)*(1+s)+(29/21))+l2*f(i,j)*(0.5*s^3-s^2+(1/14)*s+l3/21)+13*f(i,j+1)*(0.5*(1-s)^3-(1-s)^2+(1/14)*s+13/21)+l4*f(i,j+2)*((-1/6)*(2-s)^3+(2-s)^2-(85/42)*(2-s)+29/21);% Cubic o - Moms
end;
end;
end;
ff = ff(:,1:N);
fff(1:M,1:2:2*N) = f(1:M,2:N+1);
if (key0==3)||(key0==4)
fff(1:M,1:2:2 *N) = fs(1:M,1:N);
end;
fff(1:M,2:2:2*N) = ff(1:M,1:N);
fff = (fff>=0).*fff;
f = fff';
imshow(f)
figure
if (key0==3)||(key0==4)
fs = f;
f = spline_coeff(f);
end;
ss = f;
clear ff,fff;
a = -1/2;
[M,N] = size(f);
ff = zeros(M,N);
x = f(:,N-1:N);
x = rot90(x,2);
y = f(:,1);
f = [y,f,x];
L = 256;
k = 3;
k1 =1.5;
s =1/2;
l1=1;
l2=1;
l3=1;
l4=1;
for i = 1:M
for j = 2:N+1;
A=(abs(f(i,j+1)-f(i,j-1))-abs(f(i,j+2)-f(i,j)))/(L-1);
switch key1
case 1
s=s-k*A*s*(s-1);
case 2
s=1/2;
end;
switch key2
case 1
l1=l1-k1*A;
l2=l2-k1*A;
l3=l3+k1*A;
l4=l4+k1*A;
case 2
l1=1;
l2=1;
l3=1;
l4=1;
end;
switch key0
case 1
ff(i,j-1) = l2*f(i,j)*(1-s)+l3*f(i,j+1)*s;% bilinear
case 2
ff(i,j-1) = l1*f(i,j-1)*(a*s^3-2*a*s^2+a*s) +l2*f(i,j)*((a+2) *s^3-(3+a)*s^2 +1) +l3*f(i,j+1)*(-(a+2)*s^3 +(2*a+3)*s^2-a*s)+l4*f(i,j+2)*(-a*s^3+a*s^2 ); % Bicubic
case 3
ff(i,j-1) = l1*f(i,j-1)*((3+s)^3-4*(2+s)^3+6*(1+s)^3-4*s^3)/6+l2*f(i,j)*((2+s)^3-4*(1+s)^3+6*s^3)/6 +l3*f(i,j+1)*((1+s)^3-4*s^3 )/6+l4*f(i,j+2)*s^3/6;% B - Spline
case 4
ff(i,j-1) = l1*f(i,j-1)*((-1/6)*(1+s)^3+(1+s)^2+(-85/42)*(1+s)+(29/21))+l2*f(i,j)*(0.5*s^3-s^2+(1/14)*s+13/21) +l3*f(i,j+1)*(0.5*(1-s)^3-(1-s)^2+(1/14)*s+13/21)+l4*f(i,j+2)*((-1/6)*(2-s)^3+(2-s)^2-(85/42)*(2-s) +29/21);% Cubic o - Momend��
end;
end;
end;
ss=ff;
clear fff;
ff = ff(:,1:N);
fff(1:M,1:2:2*N) = f(1:M,2:N+1);
if (key0==3)||(key0==4)
fff(1:M,1:2:2*N)=fs(1:M,1:N);
end;
fff(1:M,2:2:2*N)=ff(1:M,1:N);
toc
fff = (fff>=0).*fff;
fff = min(fff,1);
error = fff'- f1;
[M1,M2]=size(f1);
MSE=sum(sum(error(3:M1-2,3:M2-2).^2))*255^2/((M1-4)*(M2-4));
PSNR2=10*log(sum(sum(ones(size(error))))/sum(sum(error.^2)))/log(10);
imshow(fff')
ct2 = fff';
save datct2 ct2
评论0