
%How to create a gradient tensor matrix and compute the eigenvalues

%inputImage must be the input image

[N M] = size(inputImage);

%Convolve with Sobel operator to get the gradient in x and y direction
sobelx = [1 0 -1; 2 0 -2; 1 0 -1];
sobely = [1 2 1; 0 0 0; -1 -2 -1];
fx = conv2(inputImage,sobelx);
fy = conv2(inputImage, sobely);


%For the tensor we need the terms fx^2, fxy, and fy^2
%Create images containing these
fx2 = fx.^2; %Does this for all pixels in the image in compact notation
fxy = fx.*fy;
fy2 = fy.^2;

% The gradient should be smoothed in a local window before the tensor is
% created. This will make it less sensitive to noise
iscale = 11;
gausfilt = fspecial('gaussian', iscale, iscale/5);
gx2 = conv2(fx2,gausfilt,'same');
gxy = conv2(fxy,gausfilt,'same');
gy2 = conv2(fxy,gausfilt,'same');

largest_eigenvalue = zeros(size(inputImage));
smallest_eigenvalue = zeros(size(inputImage));
%Now compute the eigenvalues for every pixel in the image 
%We need a for-loop for this
for i=1:N
    for j=1:M
        T = [gx2(i,j) gxy(i,j); gxy(i,j) gy2(i,j)];
        [V D] = svd(T);
        e = diag(D);
        largest_eigenvalue(i,j) = e(1);
        smallest_eigenvalue(i,j) = e(2);
    end
end

%Now display the eigenvalue images and the fx, fy, gxy, gx2 and gy2.
%In which areas in the image is the smallest
%eigenvalue/(largest_eigenvalue+smallest_eigenvalue) different from zero?

