Você está na página 1de 7

% Affine Parameter Calculation

% ---------------------------% AUTHOR: Maher Khoury


%
DATE: March 1, 1999
% PURPOSE:
%
Calculates affine motion parameters
%
% Notes on called subroutines:
%
-warp.m warps frame2 back onto frame1 using the calculated parameters
%
-grad.m calculates the image spatio-temporal gradients
%
-Parametric.m calculates the values of the parameters
%
% Variables:
%
-pyrIter
= level of the pyramid
%
-step
= sampling step
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function k = affine(image1,image2);
%% Initialize values of k to be zero %%
k(1:4) = 0;
%%%%% Hierarchical loop %%%%%
for pyrIter=2:-1:0,
step=2.^pyrIter;
%% Double the value of the transitional parameters %%
k = [k(1) k(2) 2.*k(3) 2.*k(4)];
im1 = corrDn(image1, step);
im2 = corrDn(image2, step);
for l=1:3
%%% Motion compensate frame 2 %%%
warped = warp(im2,k);
%%% Get image gradients %%%
[Ix Iy It] = grad(im1,warped);
%%% Find residual k's %%%
resid_k = Parametric(Ix,Iy,It);
%%% Update the k values %%%
k = k + resid_k;
end
end

%%%%%%%% Read in and store

clear;
p = 2;

images %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

addpath('../')
load face_adyoron_1_GS.mat
frame = face_adyoron_1_GS;
M = size(frame(:,:,1),1);
N = size(frame(:,:,1),2);
[m,n] = meshgrid(1:M,1:N);
%%%%%%%%% Find Translation Parameters %%%%%%%%%%%%%%%%%%%%%
K(1,:) = zeros(1,4);
for num= 2:p,
K(num,:) = affine(frame(:,:,num),frame(:,:,1));
end
deltaK = K(:,3:4);
%%%%%%%%%% Initialization %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Lu = 1;
Lv = 1;
Tx = 2;
Ty = 2;
wsx = 1./Tx;
wsy = 1./Ty;
F1 = fft2(frame(:,:,1));
F2 = fft2(frame(:,:,2));
F = zeros(2*Lu*M,2*Lv*N);
%%%%%%%%%%% Calculating values in matrix F
for n = 1:N,
disp(n);
for m = 1:M,
Gmn = [F1(m,n);F2(m,n)];

%%%%%%%%%%%%%%%%%%

for k=1:p,
for r = 1:4
PHImn(k,r) = exp(j*2*pi.*(deltaK(k,1)*(m./M*Tx)-(Lu/Tx)
+deltaK(k,2)*(n./N*Ty)-(Lv/Ty)))*...
exp(j*2*pi.*((deltaK(k,1)./Tx)*rem(r-1,2*Lu)+(deltaK(k,2)*floor((r1)./2*Lv))));
end
end
Fmn = pinv(PHImn)*Gmn;

F(m,n) = Fmn(4,1);
F(m+M,n) = Fmn(2,1);
F(m,n+N) = Fmn(3,1);
F(m+M,n+N) = Fmn(1,1);
end
end

%%%%%%%%%%% Get f from F and display


image = abs(ifft2(fftshift(F)));
figure;
%showIm(image);
imagesc(image);
colormap('gray')

2
3
4
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8

%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%
% warp.m subroutine for Affine Parameter Calculation
% ------------------------------------------------% AUTHOR: Maher Khoury
%
DATE: March 1, 1999
% PURPOSE:
%
Motion Compensates frame2 using input u and v vectors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function warped = warp(im, k);
[x, y] = meshgrid(1:size(im,2), 1:size(im,1));
u = k(1).*x + k(2).*y + k(3);
v = -k(2).*x + k(1).*y + k(4);
x_warped = x + u;
y_warped = y + v;
warped = interp2(x, y, im, x_warped, y_warped, 'linear');
warped(isnan(warped)) = 0;

1
9
2
0
2
1
2
2
2
3

%
%
%
%
%
%
%
%
%
%

% RES = RCONV2(MTX1, MTX2, CTR)


Convolution of two matrices, with boundaries handled via reflection
about the edge pixels. Result will be of size of LARGER matrix.
The origin of the smaller matrix is assumed to be its center.
For even dimensions, the origin is determined by the CTR (optional)
argument:
CTR
origin
0
DIM/2
(default)
1
(DIM/2)+1

% Eero Simoncelli, 6/96.


function c = rconv2(a,b,ctr)
if (exist('ctr') ~= 1)
ctr = 0;
end
if (( size(a,1) >= size(b,1) ) & ( size(a,2) >= size(b,2) ))
large = a; small = b;
elseif (( size(a,1) <= size(b,1) ) & ( size(a,2) <= size(b,2) ))
large = b; small = a;
else
error('one arg must be larger than the other in both dimensions!');
end
ly = size(large,1);
lx = size(large,2);

sy = size(small,1);
sx = size(small,2);
%% These values are one less than the index of the small mtx that falls on
%% the border pixel of the large matrix when computing the first
%% convolution response sample:
sy2 = floor((sy+ctr-1)/2);
sx2 = floor((sx+ctr-1)/2);
% pad with reflected copies
clarge = [
large(sy-sy2:-1:2,sx-sx2:-1:2), large(sy-sy2:-1:2,:), ...
large(sy-sy2:-1:2,lx-1:-1:lx-sx2); ...
large(:,sx-sx2:-1:2),
large,
large(:,lx-1:-1:lx-sx2); ...
large(ly-1:-1:ly-sy2,sx-sx2:-1:2), ...
large(ly-1:-1:ly-sy2,:), ...
large(ly-1:-1:ly-sy2,lx-1:-1:lx-sx2) ];
c = conv2(clarge,small,'valid');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% parametric.m subroutine for Affine Parameter Calculation
% -------------------------------------------------------% AUTHOR: Maher Khoury
%
DATE: March 1, 1999
% PURPOSE:
%
Caluclates the affine parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function k = Parametric(Ix,Iy,It);
k(1:4) = 0;
[x, y] = meshgrid(1:size(Ix,2), 1:size(Ix,1));
%%% Initialize the values in the A matrix
A = zeros(4);
A(1,1) = sum(sum((x.*Ix+y.*Iy).^2));
A(1,2) = sum(sum((x.*Ix+y.*Iy).*(y.*Ix-x.*Iy)));
A(2,1) = A(1,2);
A(1,3) = sum(sum(Ix.*(x.*Ix+y.*Iy)));
A(3,1) = A(1,3);
A(1,4) = sum(sum(Iy.*(x.*Ix+y.*Iy)));
A(4,1) = A(1,4);
A(2,2) = sum(sum((y.*Ix-x.*Iy).^2));
A(2,3) = sum(sum(Ix.*(y.*Ix-x.*Iy)));
A(3,2) = A(2,3);
A(2,4) = sum(sum(Iy.*(y.*Ix-x.*Iy)));
A(4,2) = A(2,4);
A(3,3) = sum(sum(Ix.^2));
A(3,4) = sum(sum(Ix.*Iy));

A(4,3) = A(3,4);
A(4,4) = sum(sum(Iy.^2));
%%% Initialize the values in the B matrix
B = zeros(4,1);
B(1,1) = sum(sum(It.*(x.*Ix+y.*Iy)));
B(2,1) = sum(sum(It.*(y.*Ix-x.*Iy)));
B(3,1) = sum(sum(It.*Ix));
B(4,1) = sum(sum(It.*Iy));
B = -B;
%%% Calculate the k parameters
C = inv(A)*B;
k = C';

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% grad.m subroutine for Affine Parameter Calculation
% -------------------------------------------------% AUTHOR: Maher Khoury
%
DATE: March 1, 1999
% PURPOSE:
%
Caluclates the image gradients in 3 dimensions given
%
two input frames
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Ix, Iy, It] = grad(im1, im2);
%%%% Defime the size of the gradients
sizey = size(im1,1);
sizex = size(im1,2);
%%%% Get neighbouring pixels %%%%
im1_right = im1(1:sizey-1, 2:sizex);
im1_down = im1(2:sizey,1:sizex-1);
im1_down_right = im1(2:sizey,2:sizex);
im2_right = im2(1:sizey-1, 2:sizex);
im2_down = im2(2:sizey,1:sizex-1);
im2_down_right = im2(2:sizey,2:sizex);
%%%% Calculate Ix %%%%
Ix = zeros(sizey,sizex);
Ix(1:sizey-1,1:1:sizex-1) = 0.25.*(im1_right + im2_right + im1_down_right +
im2_down_right - ...
(im1(1:sizey-1, 1:sizex-1) + im2(1:sizey-1,1:sizex-1) + im1_down +
im2_down));
Ix(sizey,:) = 0;
Ix(:,sizex) = 0;
%%%% Calculate Iy %%%%
Iy = zeros(sizey,sizex);

Iy(1:sizey-1,1:1:sizex-1) = 0.25.*(im1_down + im2_down + im1_down_right +


im2_down_right - ...
(im1(1:sizey-1, 1:sizex-1) + im2(1:sizey-1, 1:sizex-1) + im1_right +
im2_right));
Iy(sizey,:) = 0;
Iy(:,sizex) = 0;
%%%% Calculate It %%%%
It = zeros(sizey,sizex);
It(1:sizey-1,1:1:sizex-1) = 0.25.*(im2(1:sizey-1,1:sizex-1) + im2_right +
im2_down + im2_down_right - ...
(im1(1:sizey-1,1:sizex-1) + im1_right + im1_down + im1_down_right));
It(sizey,:) = 0;
It(:,sizex) = 0;

Você também pode gostar