% 16-311 Homework 2 Framework
% Written by Humphrey Hu
% humhu@cmu.edu
% 1/16/2014

% Note: Use this file with waldo.png in your active directory
function [] = WheresWaldo(image_path, output_path)
% WHERESWALDO - Finds Waldo in an image!
% Usage:
% WheresWaldo(image_path, output_path) - Finds waldo in an image specified by
% image_path and saves to a log specified by output_path.

%% 1. Image Basics
% Images are just matrices of numbers. Let's make a blank white image.
% Grayscale images are scaled from 0.0 to 1.0, with 0.0 black.
image = ones(64, 48);

% There are two different image display methods. imshow() operates on
% matrices of type uint_8 (0 to 255), or type double (0 to 1.0).
figure;
imshow(image); % Display the white image
title('Our blank image');

% Image files can be read with imread(). Here we read Waldo in PNG format
% to get a RGB (N X M X 3) matrix, where the 3rd dimensions are the RGB
% channels, respectively
waldo_rgb = imread(image_path);
fprintf('Loaded Waldo as a %d by %d by %d matrix.\n', ...
    size(waldo_rgb,1), size(waldo_rgb,2), size(waldo_rgb,3));
figure;
imshow(waldo_rgb);
title('RGB Waldo');

% Convert the RGB matrix to grayscale, with values 0 to 255
% Note: imread and rgb2gray return matrices of uint_8
% Calling double() converts these to doubles, which are easier to use
waldo_gray = double(rgb2gray(waldo_rgb));

% imagesc() shows any 2D matrix as a colorized image. You can also
% normalize the image to be on the range (0, 1.0) and use imshow()
figure;
imagesc(waldo_gray); 
colorbar;
title('Waldo with imagesc()');

waldo_gray = waldo_gray/255;
figure;
imshow(waldo_gray);
title('Normalized Waldo with imshow()');

% Since images are just matrices, we can paste Waldo into the image by
% assigning him to a block. Note that matrices are indexed (y,x) with (1,1) at
% the upper left hand corner. This is the same convention as for images.
H = size(waldo_gray, 1); W = size(waldo_gray, 2); % Get his height and width
image(10:10+H-1, 20:20+W-1) = waldo_gray;
figure;
imshow(image);
title('Our blank image with Waldo pasted in.');

% Note that we pasted Waldo in so that his upper left corner is at (20, 10)
% in x-y coordinates. Remember that the homework requests you to locate
% his center pixel!

%% 2. Find Waldo!

% This part is up to you!

%% 3. File I/O

% First we open a file and get a file ID # with fopen(). 'w' specifies we
% want to write.
file_id = fopen(output_path, 'w');
if file_id == -1    % -1 is an invalid file ID and signals failure
    fprintf('File creation failed!\n');
    return;
end

% Now we can write to the file using fprintf (formatted print to file)
% fprintf uses a format specifier string, where %d represents an integer
% and %f represents a floating point number. You can then give fprintf the 
% value you want to substitute into the string as additional arguments.
%'\n' is the special character for newline (or the enter key), and '/t' 
% is the tab character.
a = 1;
b = 2.5;
fprintf('a = %d,\t b = %f\n', a, b);

% If the file ID # is given to fprintf, it will write into the file instead
% of the terminal.
fprintf(file_id, 'Hello world!\n');

% Finally when you're done, you should close the file with fclose()
fclose(file_id);
