%% HW4 - 3101 Matlab -- (INSERT NAME and UNI HERE)
% Due 11:59pm on Tuesday, April 8th, 2008

%% Goals
% Working with graphics, simple animation, and images

%% Directions
% Please fill in your name at the top of this page and write your answers
% under their corresponding question.  When you are done, publish this file
% to html using File > Publish To HTML (or the command publish('hw1.m',
% 'html') and create a new zip file that is labeled with your UNI and
% homework number, in this format: bs2018_hw1.zip, that contains this
% original m file, and the html directory created from publishing the file.
% Make sure the html file adequately shows your work, (has images, etc.),
% and then email this file to cs3101@gmail.com, making sure to include your
% name, and uni in the subject.



%% Problem 1 - Conway's Game of Life
% <http://en.wikipedia.org/wiki/Conways_Game_of_Life Conway's Game of Life>
% is a cellular automation that operates on a 2D grid. Life is a wonderful
% example of how complex patterns can arise from very simple rules and
% initial conditions.

N = 20;

%% 1.1
% Using only one command.  Make a grid of size NxN where 80% of the entries
% are randomly set to 1 and the rest are zero.  Store the grid in the
% variable X.  Do not display X.



%% 1.1.2
% Change the colormap to black and white and display X in figure 1, make
% sure to change the axis so that X is displayed as square.


%% 1.1.3
% For each cell of the grid, count how many of its 8 neighbors are equal to
% 1. Store the number of neighbors for each cell in a new variable called
% neighbors. The grid should be connected left to right and top to bottom,
% so that it's continuous. Cells on the boundary on the right for example
% may have neighbors on the left boundary.  Hint: it's much simpler to do
% this with one big command, and it involves circularly shifting around and
% adding the original matrix X.



%% 1.1.4
% Display neighbors as a heatmap in figure 2 using the colormap jet




%% 1.2
% Implement and run conway's game of life on a 100x100 grid where initially
% 80% of the entries are randomly assigned to 1. Some notes:

%%
%
% * Only run 50 iterations of conways game of life and then stop
% * Neighbors are computed as stated above (the grid wraps around itself
% like a torus)
% * At each iteration plot the grid in figure 1, so you can see it animate,
% but plot white for 0s and black for 1s
% * Make the title for figure 1 show which iteration the simulation is at,
% and the percentage of cells which are equal to 1





%% 1.3
% Write a short piece of code that initializes an empty 50x50 grid, plots
% that grid in figure 1 and then prompts the user to click on the plot. The
% user is able to toggle the color of cells between black and white by
% clicking on them.  Changing the color of the cell changes the underlying
% values in the grid from 1 to 0 and vice versa.  The user should be able
% to terminate the loop by right-clicking anywhere without making a dot
% (the dots are made by left clicks). 

%%
% NOTE: before you publish your HW, you must comment out this code because
% it is interactive, but to prove that it works you will use this bit of
% code in the next question.




%% 1.4
% Use the piece of code you wrote above to draw
% <http://en.wikipedia.org/wiki/Image:Game_of_life_glider_gun.png Gosper's
% Glider Gun>.  When you have sucessfully done it once.  Save X (your grid)
% to a file called Xgosperglider.mat (only save the one variable X to this
% file).  Comment out the interactive code above, and simply load the .mat
% file below, and apply conways game of life to it.  Only run Life for 100
% iterations.



%% 1.4.1
% Figure 1 should currently contain your Gosper Glider Gun after it has
% been iterated with life 100 times.  Write this entire figure window to an
% image file called 'gliderFigure.png'.  Make sure you write the entire
% figure, with the title, etc.




%% 1.4.2
% Now simply write to an image file for the data which is contained inside
% the plot (the data in variable X).  This image should be 50x50 pixels,
% and does not contain the axes or the title. 




%% Problem 2 -- Image Filters
%


%% 2.1
% Download the image fruit.jpg from the class website, and load the image
% into a variable called X.



%% 2.1.1
% Display X in figure 1, make sure sure the axes are set so that pixels are
% square




%% 2.1.2
% What are the dimensions of X?




%% 2.2
% A grayscale image can be computed by averaging the red, green, and
% blue components for each pixel.  Convert X to grayscale and store that
% result in variable called Xbw. 



%% 2.2.1
% Display Xbw twice, once using the gray colormap,



%% 2.2.2
% Display Xbw using the hot colormap



%% 2.2.3
% Compute and show a new image called Xthresh, which is white wherever the
% intesity of Xbw is greater then 85 and black everywhere the intensity is
% less then 85. 




%% 2.2.4
% write the Xthresh image out as a png.  do not write the whole figure window,
% just the image.




%% 2.3
% The Sobel Operator is a way to do edge detection and is used in many
% computer vison applications. Print out the help for conv2, and read
% through the wikipedia article on the
% <http://en.wikipedia.org/wiki/Sobel_operator Sobel Operator>




%% 2.3.1
% Create a new variable A that is 10x10 and contains all zeros except for
% the middle 6x6 elemnts which contain ones.  Display A as a grayscale
% image.



%% 2.3.2
% Compute Gx by convolving the sobel kernel with the A matrix. Display Gx
% as a mesh in figure 4 with the colormap jet.




%% 2.3.3
% Compute Gx, Gy, and G for the fruit image by following how it is done in
% the wikipedia article. Display the result as an image.  Save the result
% to a png file.



%% Problem 3 - Where's Waldo with Template Matching

%% 3.1
% load the two images waldo.png, and findwaldobig.jpg.  



%% 3.2
% Display findwaldobig



%% 3.3
% Display waldo



%% 3.4
% Print out the help pages for normxcorr2



%% 3.5
% Convert the two images two grayscale, and run normxcorr2 on them to get a
% matrix of correlation values, the maximum of which, corresponds to the
% best place to put the smaller image so that it lines up with the big
% image.  Call this matrix of correlations W, and display it using the jet
% heat map.  Note: normxcorr2 could take a while to run.




%% 3.6
% Find the coordinates for the maximum of W (display them), and use the
% hold, and scatter commands to plot the grayscale findwaldobig image, and
% plot 4 red dots around where you found waldo.







