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
if (key0==3)
f = spline_coeff(g);
fs = g;
end;
f = g;
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;
l1= 1;
l2= 1;
l3= 1;
l4= 1;
k = 3;
k1 =1.5;
s =1/2;
net_ip = zeros(2 ,2000);
if (key0==2)||(key0==3)
net_ip = zeros(4,2000);
end
net_op = zeros(1,2000);
counter = 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
tt = l2*f(i,j)* (1-s)+l3*f(i,j+1)*s;% bilinear
net_ip(:,counter)=[f(i,j);f(i,j+1)];
case 2
tt = 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
net_ip(:,counter)=[f(i,j-1);f(i,j);f(i,j+1);f(i,j+2)];
case 3
tt = 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
net_ip(:,counter)=[f(i,j-1);f(i,j);f(i,j+1);f(i,j+2)];
end;
net_op(:,counter) = tt;
counter = counter +1;
end;
end;
net_ip = net_ip(:,1: counter-1);
net_op = net_op(:,1: counter-1);
net = newff(minmax(net_ip),[2,1],{'logsig' 'logsig'},'trainscg');
net = init(net);
net.performFcn = 'mse';
net.trainparam.epochs = 20000;
net.trainparam. show = 10;
net.trainparam.goal = le-10;
net.trainparam.min_grad =le-7;
net = train (net, net_ip,net_op);
for i = 1:M
for j = 2:N +1
switch key0
case 1
y = [f(i,j);f(i,j +1)];
case 2
Y= [f(i,j -1);f(i,j);f(i,j +1);f(i,j +2)];
case 3
Y= [f(i,j-1);f(i,j);f(i,j+2)];
end
ff(i,j -1) = sim(net, y);
end;
end;
M;
N;
ff = ff (:,1:N);
fff(1:M,1:2:2*N) = f(1:M,2:N+1);
if (key0==3)
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)
f = spline_coeff(g);
fs = g;
end;
ss = f;
clear ff;
clear 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;
s = 1/2;
k = 3;
k1 = 1.5;
l1= 1;
l2= 1;
l3= 1;
l4= 1;
N;
net_ip = zeros(2,2000);
if (key0==2)||(key==3)
net_ip = zeros(4,2000);
end
net_op = zeros(1,2000);
counter = 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
tt = l2*f(i,j)*(1-s)+l3*f(i,j+1)*s;% bilinear
net_ip(:,counter) = [f(i,j);f(i,j+1)];
case 2
tt = l1*f(i,j-1)*(a*s^3-2*a*s^2+a*s)+l2*f(i,j)*((a+2)*s^3-(3+a)*3^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
net_ip(:,counter)=[f(i,j-1);f(i,j);f(i,j+1);f(i,j + 2)];
case 3
tt = 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
net_ip(:,counter) = [f(i,j-1);f(i,j);f(i,j+1);f(i,j+2)];
end;
net_op(:,counter) = tt;
counter = counter +1;
end;
end;
net_ip = net_ip(:,1:counter-1);
net_op = net_op (:,1:counter -1);
net = newff(minmax(net_ip),[2 1],{'logsig''logsig'},'trainscg');
net = init(net);
net .performFcn = 'mse';
net.trainparam.epochs =20000;
net.trainparam.show = 10;
net .trainparam.goal =le-10;
net .trainparam.min_grad = le-7 ;
net = train (net,net_ip,net_op);
for i = 1:M
for j = 2 :N+1
switch key0
case 1
Y= [f(i,j);f(i,j+1)];
case 2
Y= [f(i,j-1);f(i,j);f(i,j+1);f(i,j+2)];
case 3
Y= [f(i,j-1);f(i,j);f(i,j+1);f(i,j+2)];
end;
ff(i,j-1) = sim(net,y);
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)
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);
size( fff')
size(f1)
error = fff'-f1;
[Ml,M2] = size(f1);
MSE = sum(sum(error(3:Ml-2,3:M2-2),2))*255^2/((Ml -4)*(M2-4));
PSNR = 10*log(sum(sum(ones(size(error))))/sum( sum(error.^2 )))/log(10);
imshow(fff');
figure
imshow(1-error);