%% Basics - Run this first

clear all; close all; clc; %#ok<CLALL> 

% MATLAB has a few built-in images that you can load to test with
rgb_pears = imread('pears.png');
rgb_chips = imread('coloredChips.png');

% Different formats can be read by the same function, but the data type
% that results can vary. 
I_rice = imread('rice.png');
I_pout = imread('pout.tif'); 
% Note that imread takes a variety of different extensions, and the data type isn't necessarily the same. 

% make a double image from a uint8 image. This is necessary for certain
% operations. The 'natural' range in MATLAB for double images is [0,1]
d_rice = double(I_rice); d_rice=d_rice./max(d_rice(:));

%
%% Display functions

% First, note that MATLAB thinks of images as matrices, and so its
% coordinate system is a little different. Notice how this is measured from
% the top-left, with the Y axis as the first coordinate.
tiledlayout(2,2);
nexttile;
image(rgb_chips); title('image(RGB)'); axis equal tight;
nexttile;
image(rgb_chips(1:150,1:350,:)); title('image(RGB(1:150,1:350))'); axis equal tight;
nexttile;
image(rgb_chips); title('image(RGB), flipped axis'); 
set(gca,'YDir','normal'); 
nexttile;
image(flipud(rgb_chips)); title('image(RGB_{flipped})');

% Note that the flipped images look the same, but the pixel coordinates (left side
% of the images) are not the same. It is important to keep in mind that
% your data and your display are two separate considerations.





% For RGB (3-channel uint8), things are pretty much the same no matter
% which visualization function you use.
% -imshow does not show axis ticks (pixel number), but this can be
% accomplished by adding 'axis off' to any of the other display funcs.
tiledlayout(2,2);
nexttile;
image(rgb_chips); title('image(RGB)');
nexttile;
imagesc(rgb_chips); title('imagesc(RGB)')
nexttile;
imshow(rgb_chips); title('imshow(RGB)')
nexttile;
imagesc(rgb_chips); title('imagesc(RGB) + axis off'); axis off;


% The story is different for double-format or grayscale images.
% - 'image' naturally assumes 8-bit (256-level) images, and so the scale is
% off.
% - 'imagesc' visualizes using the min-max range of the data.
% - 'imshow' visualizes using the 'natural' range for the data - for double
% this is [0,1], and so using it for unscaled data can cause images to look
% blown out.
colormap gray;
tiledlayout(2,2);
nexttile;
image(d_rice); title('image(double)'); colorbar;
nexttile;
imagesc(d_rice); title('imagesc(double)'); colorbar;
nexttile;
imshow(d_rice); title('imshow(double)'); colorbar;
nexttile;
imshow(d_rice * 2); title('imshow(double * 2)'); colorbar;


%
%% Colormaps


% Using imagesc on a grayscale image gives you a colormap-indexed image, where each grayscale
% value is mapped to an RGB triplet. Run the following code and look at the
% values.
figure;
cmap_gray = colormap('gray');
cmap_parula = colormap('parula');
cmap_jet = colormap('jet');
cmap_spring = colormap('spring');

figure;
tiledlayout(1,4); 
nexttile; imshow(cmap_gray); axis on; daspect([1,6,6]); set(gca,'XTickLabels',{'R','G','B'}); title('gray');
nexttile; imshow(cmap_parula); daspect([1,6,6]);title('parula');
nexttile; imshow(cmap_jet); daspect([1,6,6]);title('jet');
nexttile; imshow(cmap_spring); daspect([1,6,6]);title('spring');

%

% Colormaps by default will apply to the entire figure window. The
% following will not color the individual images differently, and will
% color every image with the last colormap selected.

tiledlayout(2,2);
nexttile;
imagesc(d_rice); title('parula'); colormap(parula); colorbar; axis off
pause(1);  
nexttile;
imagesc(d_rice); title('jet'); colormap('jet'); colorbar; axis off;
pause(1);  
nexttile;
imagesc(d_rice); title('gray'); colormap('gray'); colorbar; axis off;
pause(1);  
nexttile;
imagesc(d_rice); title('spring'); colormap('spring'); colorbar; axis off

%

% To have different colormaps for each image, you must capture the handles
% of the image axes as the outputs from the nexttile function, then pass
% that to the colormap function. Note that nexttile is not necessary to
% use, but you will need to get an Axes object somehow. 
% 'gca' Gets the Current Axes, but this is an unreliable approach in more
% complex presentations and analyses. 

tiledlayout(2,2);
h1 = nexttile;
imagesc(d_rice); title('parula'); colormap(h1,parula); colorbar; axis off;
h2 = nexttile;
imagesc(d_rice); title('jet'); colormap(h2,'jet'); colorbar; axis off;
h3 = nexttile;
imagesc(d_rice); title('gray'); colormap(h3,'gray'); colorbar; axis off;
h4 = nexttile;
imagesc(d_rice); title('spring'); colormap(gca,'spring'); colorbar; axis off;  


%
%% Data adjustment vs display adjustment

colormap(gray) % Reset instance colormap if necessary.

% Some images do not have good contrast and are hard to see. This is
% because the distribution of intensity values is concentrated in one
% range, making it difficult to differentiate visual levels.


% You can automatically adjust contrast for grayscale images, but this DOES
% affect the data.
tiledlayout(2,2); 
nexttile;
imagesc(I_pout); colorbar; axis off; title('Raw image');

nexttile;
I_pout_eq = histeq(I_pout);
imagesc(I_pout_eq); colorbar; axis off; title('Equalized');

nexttile;
histogram(I_pout);  title('Raw histogram'); ylabel('Counts'); xlabel('Gray values');

nexttile;
histogram(I_pout_eq);  title('Eq. histogram'); xlabel('Gray values');

% It is also possible to change the levels of the display itself. Let's
% scale to the 2nd and 98th percentile of the value distribution.
new_limits = [prctile(I_pout(:),2) prctile(I_pout(:),98)]

tiledlayout(2,2); nexttile;
imagesc(I_pout); colorbar; axis off; title('Raw image');
nexttile;
imagesc(I_pout); set(gca,'CLim',new_limits); colorbar; axis off; title('Changed limits');
nexttile;
histogram(I_pout);  title('Raw histogram'); ylabel('Counts'); xlabel('Gray values');
nexttile;
histogram(I_pout); set(gca,'XLim',new_limits);  title('Changed limits histogram'); xlabel('Gray values');

%
%% Images can also be resized ...

% Notice that the sizes of the images as displayed do not change, but the
% extents in pixels do.
tiledlayout(2,2); nexttile;
imagesc(I_pout); title('Raw image');
nexttile;
imagesc(imresize(I_pout,2)); title('2x rescale');
nexttile;
imagesc(imresize(I_pout,0.5)); title('0.5x rescale');
nexttile;
imagesc(imresize(I_pout,[256,256])); title('Scale to [256 256]');

%
%% ... and cropped. Rectangles are defined as [xmin ymin width height]


rect_X0 = 30;
rect_Y0 = 30;
rect_width = 190;
rect_height = 90;
rect_spec = [rect_X0,rect_Y0,rect_width,rect_height];


tiledlayout(1,3); nexttile;
imagesc(I_pout); title('Raw image'); axis equal tight;

nexttile;
imagesc(I_pout); title(['[', num2str(rect_spec),']']); axis equal tight;
rectangle('Position',rect_spec,'EdgeColor','y');


nexttile;
imagesc(imcrop(I_pout,rect_spec)); title(['Cropped [' num2str(rect_spec),']']); axis equal tight;


%%% Uncomment this and run in the Command Window %%%
%%% vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv %%%

%


%% When interactive windows come up, draw a rectangle by
%% clicking-and-dragging and right click inside to crop the image.
% fig1=figure;
% I_pout_interactive_crop = imcrop(I_pout);
% fig2=figure;
% imagesc(I_pout_interactive_crop);

%


% fig3=figure;
% I_pout_interactive_crop_2 = imcrop(I_pout,colormap('autumn'));
% fig4=figure;
% imagesc(I_pout_interactive_crop_2);
