%Track a feature in an image (Roppel, 1/30/2013)
%Use Eq. 2 from Paper L - "Good Features to Track"
%Jianbo Shi and Carlo Tomasi in CVPR’94, pp. 593–600, 1994
%
%Need to fix this code so that the image coordinates x are measured with
%respect to the window center.
%
%Note that when displaying arrays in MATLAB, the x coordinate
%is the row number and increases downward. The y-coordinate
%is the column number and increases to the right. Also, the origin
%is at (1,1) = upper left corner, since MATLAB array indexes start at
%one. Calling I(0,0) will give an error.
%
n=101; %size of original image containing feature to track
ms = 200; %maximum shift amount - add'l size for output image
close all
%Create a simple test image I of size n x n, n must be odd.

I = 255*.5*ones(n,n); %all gray n x n
ctr=(n+1)/2;
I(ctr,ctr) = 255; %bright spot in center
%bordered with black:
for r=1:n
I(r,[1 n])=0;
end
for c=1:n
I([1 n],c)=0;
end
imagesc(I); colormap(gray); %display input image
axis([0 n+ms 0 n+ms]); %same scale as output image

%Create an array J to contain the output image

J=zeros(n+ms,n+ms); %J bigger than I by ms

%Now create output image using affine transformation.

d = [10 10]'; %translation of feature window center (user input)
D = [0 0; 0 0]; %deformation matrix (user input - example: all 1's)
id2=eye(2); %2 x 2 identity matrix

A = id2+D; %affine matrix

for r=1:n
for c= 1:n
x=[r c]'; %pixel location in original image I
newx = A*x + d; %location to which x moves in new image J
newr=newx(1); %new row value
newc=newx(2); %new column value
J(newr, newc)=I(r,c); %move the pixel intensity

end
end

figure;
imagesc(J); colormap(gray); %display output image
axis([0 n+ms 0 n+ms])