% Read in an image and make a surface plot of it with pseudocolors.

% Initialization steps.
clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
clear;  % Erase all existing variables. Or clearvars if you want.
workspace;  % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;

% [X,Y,Z] = peaks(256);
% I = double(imread('cameraman.tif'));
% figure;
% mesh(X, Y, Z, I);
% colormap jet;

% Read in and display the image.
grayImage = imread('coins.png'); % An actual image.
% grayImage = imread('eight.tif'); % An actual image.
% grayImage = peaks(256);  % Gives a nice looking demo of a smooth surface.

% Display original image in the upper left.
h1 = subplot(2, 2, 1);
imshow(grayImage);
title('coins.png', 'FontSize', fontSize);
hp = impixelinfo();	% Allow user to mouse over image and see the gray level.
% Get the dimensions of the image.  
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
drawnow;

% Convert to gray scale if needed.
if numberOfColorChannels > 1
	% It's not really gray scale like we expected - it's color.
	% Use weighted sum of ALL channels to create a gray scale image.
	grayImage = rgb2gray(grayImage); 
end

% Put pseudocolored version in the lower left.
h3 = subplot(2, 2, 3);
imshow(grayImage);
title('coins.png', 'FontSize', fontSize);
% Apply color map.
colormap(h3, jet(256));

% Get a list of x, y coordinates
[X, Y] = meshgrid(1:columns, 1:rows);
dblImage = double(grayImage); 
% Make the height be the gray level.
Z = dblImage;
% Smooth the image so it's not so spiky.
windowSize = 5; % Use an odd number
Z = conv2(Z, ones(windowSize)/windowSize^2, 'same');

h4 = subplot(1, 2, 2);
mesh(X, Y, Z, Z, 'EdgeColor', 'none', 'FaceColor', 'interp'); % Overlay image as texture

% Apply color map.
colormap(h4, jet(256));

% Label axes
xlabel('X, column', 'FontSize', fontSize);
ylabel('Y, Row', 'FontSize', fontSize);
zlabel('Intensity, Gray Level', 'FontSize', fontSize);
title('Click the rotation icon on the toolbar to turn the surface', 'FontSize', fontSize);

% Maximize the figure.
hFigure = gcf; % Get a handles to the figure.
hFigure.WindowState = 'maximized';  % R2018a??? and later.


