% singlePointDistort.m
% 
% Zach Gildersleeve
% Januaru 26, 2007
%
% This script displays an image, and allows the user to select starting
% point and a landmark point.  The image is transformed to warp the
% starting point to the landmark point using a gaussian radial basis
% function.


% Read in the image
myImage = imread('../images/photo1-2.png');

% Convert the image to double between 0 and 1
myImage = (double(myImage)/255);

% Define the cordinate axis of the image
% For simplicity assume that the image Domain is [-1 1]
[X,Y] = meshgrid(linspace(-1,1,512),linspace(-1,1,512));

% Define sigma
sigma = 0.5;

% Create the figure and draw the original image
figure('Name', 'Single Point Warp', 'Position', [0 0 512 512]);
imagesc(linspace(-1, 1, 512), linspace(-1, 1, 512), myImage);
colormap('gray');
axis('square');

% Get two coordinates from the user's mouse
[L1, L2] = ginput(2);

% Compute alpha
alpha1 = (L1(2) - L1(1));
alpha2 = (L2(2) - L2(1));

% Calculate the transform [U1, U2]
U1 = alpha1 .* exp(-((X - L1(2)).^2 + (Y - L2(2)).^2) / sigma^2);
U2 = alpha2 .* exp(-((X - L1(2)).^2 + (Y - L2(2)).^2) / sigma^2);

% Interpolate the transformed image
% We use X - U1, Y - U2 to transform in correct direction
distortedImage = interp2(X, Y, myImage, X - U1, Y - U2, 'spline');
imagesc(distortedImage);

colormap('gray');
axis('square');

% Draw line from landmark point location to translated point
if 1
    newL1 = L1 * 256 + 256;
    newL2 = L2 * 256 + 256;
    hold on;
    plot(newL1, newL2, 'r--x');
end