% This is the program for vitgnetting for ee362
% Title: vignetting.m
% written by Stephen Kaye
% Started on 3/04/06 
%
% This program takes in an image and makes a contour of
% the light intensity.  Since the image is large and has
% noise, two processing steps are done before the contour
% plot.  The image needs to be resized.  We were unhappy 
% with this function for images with fine details, but
% this function is fine for these puposes.
% We also needed to smooth the image since we were interested
% in the low frequency light intensity change and not the high
% (spatial) frequency noise.  After all that, the light contour
% of the image was plotted. 
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%


clear all;

filename = 's50 f-stop 5.6';
image = imread('IMG_6644.jpg');
%imshow(image);

db_image = double(image);
red_blue_ratio = db_image(:,:,1)./db_image(:,:,3);

Y = 0.30.*db_image(:,:,1) + 0.59.*db_image(:,:,2) + 0.11.*db_image(:,:,3);

figure(1);
imagesc(Y);
colormap(gray);
colorbar;
str = ['Luminance Falloff', filename];
title({'Luminance Falloff', filename});
%print -djpeg y71f2.jpg

%figure(2);
%imagesc(red_blue_ratio);
%colormap(gray);
%colorbar;
%title({'Red to Blue Ratio Falloff', filename});
%print -djpeg red_blue71f2.jpg

gray_im = Y;  % this is the intensity, use it 
gray_doub = double(gray_im);

clear 'Y'; 
% let's resize the image so that Matlab can compute the contour
im_size = size(gray_doub);
aspect_ratio = im_size(2)/im_size(1);
rebinned = imresize(gray_doub, [750 (750*aspect_ratio)]);

clear 'gray_doub'; 
%imshow(rebinned, gray(256));

% Now filter the image
% We're interested in low frequency light falloff, not pixel to pixel noise
filter_2d = fspecial('gaussian', [3 3], 20);
filtered_gray_im = imfilter(rebinned, filter_2d);

% This part normalizes the data so that we get precentage light falloff
% with respect to the peak light level
max_filt_gray_im = max(max(filtered_gray_im));
norm_filt_gray = filtered_gray_im./max_filt_gray_im;

% This is finally the figure of the light falloff in a contour plot
colormap('default');
% return colormap to default state
figure(3);
[C, h] = contour(norm_filt_gray, 7);
clabel(C, h);
title({'Contour Plot of Light Falloff', filename});
hold on;

%first_value = C(1, 1);

size_o_C = size(C,2);
next_value = 1;
i = 1;
while (next_value < size_o_C)
    num_values = C(2, next_value);
    contour_value(i) = C(1, next_value);
    next_value = next_value + num_values + 1;
    i=i+1;
end

max_contour = max(contour_value);
min_contour = min(contour_value);
mean_contour = mean(contour_value);

str(1) = {['Max Contour Value:',num2str(max_contour)]};
str(2) = {['Min Contour:',num2str(min_contour)]};
str(3) = {['Mean Contour:',num2str(mean_contour)]};
text(50, 60, str, 'BackgroundColor', [0.8 0.8 0.8]);
print -djpeg contours5056f20.jpg

% here's a noise plot of the data

% first filter the grey scale image

%filter_gray_full = imfilter(gray_doub, filter_2d); 
%im_noise = gray_doub - filter_gray_full; 

%diff_mean =mean(mean(im_noise)); 
% make matrix with the mean value 
%mean_matrix = zeros(size(im_noise));
%mean_matrix = diff_mean + mean_matrix;  

%std_dev_matrix = (im_noise - mean_matrix).^2/numel(im_noise); 
%grey_noise = 40960000.*std_dev_matrix; 

%figure(4);
%imshow(grey_noise);
%colormap(gray);
%colorbar;
%title({'Nonuniformity Noise Detail', filename});
%print -djpeg grey_noise71f2.jpg

