%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% POCS Image Reconstruction
% -------------------------
% AUTHOR: Stephen Rose, Maher Khoury
% DATE: March 1, 1999
% PURPOSE: Generates SR frame using the POCS method
% Notes:
% -init.m contains the affine transformation parameters
% -Assuming a gaussian PSF
% -u,v are affine transformation vectors for (x,y)
% -mcX,mcY are transformed coordines in SR frame
%
% Variables:
% -ref = LR reference frame
% -upref = HR reference frame
% -NumberOfFrames = Number of pixel frames to consider
% -frame = LR frame currently being examined
% -weights = weights based on Gaussian PSF
%%% Initialization
%init;
NumberOfFrames = 5;
k = zeros(1,4);
%%% Create the high-resolution reference frame 对应算法:(1)选择一个参考帧k
ref = imread('C:\Users\John\Desktop\22.jpg');
[m1,n1]=size(ref)
figure;
imshow(ref);
colormap(gray);
drawnow;%刷新屏幕,有延时作用。
ref =double(ref);%(1:size(ref,1)行./2,1:size(ref,2)./2)列);
%%%Interpolate values at inbetween points 对应算法(2)将低分辨图像插值到高分辨网格上
[x, y] = meshgrid(1:size(ref,2), 1:size(ref,1));
[X, Y] = meshgrid(1:2.*size(ref,2), 1:2.*size(ref,1));
upref = interp2(x,y,double(ref),X./2,Y./2,'linear');
%zi = interp2(x,y,z,xi,yi, 'method')对二维网格(X,Y)上的数据Z进行插值
%对一组点(x,y,z)?按method指定的插值算法进行二维插值,并计算插值点(xi,yi)的函数值zi。
%其中(x,y,z)?是已给的数据点,(xi,yi)是插值点坐标
upref(isnan(upref)) = 0;
figure;
ShowIm(upref);
[m2,n2]=size(upref)
colormap(gray);
drawnow;
%%% Iterate the entire process
for iter=1:10,
disp(iter);
%%% Iterate over the frames
for num = 2:NumberOfFrames,
%%% Read in the frame
if (num < 10);
frame = pgmread(strcat('E:\SR\POCS\POCS\taxi\taxi0',num2str(num),'.pgm')); %把数值转换成字符串
else
frame = pgmread(strcat('E:\SR\POCS\POCS\taxi\taxi0',num2str(num),'.pgm'));
end
frame = double(frame);%(1:size(frame,1)./2,1:size(frame,2)./2));
%%%Calculate the affine motion parameters for this frame
k = affine(frame,ref); % Calculates affine motion parameters 基于光流法的图像配准?
u = k(1).*X + k(2).*Y + 2.*k(3); %运动矢量
v = -k(2).*X + k(1).*Y + 2.*k(4);
%k3 and k4 describe the translational motion of the image while k1 and k2 describe its rotational motion.
%%% Calculate the coordinates of the motion compensated pixels
mcX = X + u;
mcY = Y + v;
%%% Loop over entire (low-res) frame
for m2 = 1:size(frame,2),
for m1 = 1:size(frame,1),
%%% Get high-resolution coordinates
n1 = 2*m1;
n2 = 2*m2;
%%% Get coordinates of the motion compensated pixel
N2 = mcX(n1,n2);
N1 = mcY(n1,n2);
%%% If not a border pixel
if ( N1>3 & N1<size(upref,1)-2 & N2>3 & N2<size(upref,2)-2 )
%%% Find center of the window where the PSF will be applied
rN1 = round(N1);
rN2 = round(N2);
%%% Calculate the effective window
windowX = Y(rN1-2:rN1+2,rN2-2:rN2+2);
windowY = X(rN1-2:rN1+2,rN2-2:rN2+2);
%%% Find the value of the gaussian at these points and normalize
weights = exp(-((N1-windowX).^2+(N2-windowY).^2)./2);
weights = weights./sum(sum(weights));
%%% Calculate the value of the estimate Ihat
Ihat = sum(sum(weights.*upref(rN1-2:rN1+2,rN2-2:rN2+2)));
%%% Calculate the residual 计算残余项r
R = frame(m1,m2) - Ihat;
temp = 0;
%%% Calculate new values for the reference frame
if (R>1)
convertedR=double(R);%残余项
%投影公式
upref(rN1-2:rN1+2,rN2-2:rN2+2) = upref(rN1-2:rN1+2,rN2-2:rN2+2) + ...
(weights.*(convertedR-1))./sum(sum(weights.^2));
elseif (R<-1)
convertedR=double(R);
upref(rN1-2:rN1+2,rN2-2:rN2+2) = upref(rN1-2:rN1+2,rN2-2:rN2+2) + ...
(weights.*(convertedR+1))./sum(sum(weights.^2));
end
end
end
end
upref(upref<0) = 0;
upref(upref>255) = 255;
end
end
%%% Display the image %%%
pgmwrite(upref,'SRframe.pgm');
figure;
imshow(upref);
[m3,n3]=size(upref)
colormap(gray);
drawnow;