%% Lesson 06

%% Types of variables
clear
close all

% We know about numbers.  Easy to store them as variables, arrays, and
% matrices

a = 7
b = 55
c = 1:7


% If you just create a number, it is a double, a floating point that will
% take up 8 bytes per number
whos

% Not all numbers are created equal
u1 = uint8(9)
whos

% Uint8 -> unsigned integer.  Can go up to 2^8 = 255.  Has to be an
% integer.  Takes up 8 bits of memory
u2 = u1/2

% uint16 -> unsigned integer.  Can go up to 2^16 = 65535.  Also has to be
% an integer.  Takes up 16 bits of memory (2 bytes).


% Possible to convert back to double
u3 = double(u2)
u4 = u3/2


% We can also add characters.  These are obviously letters.  Strings are
% defined by using single apostrophe

d = 'hello world'
e = 'Quantitative optical imaging rocks'

% Character arrays are accessed exactly the same way as numerical arrays
d(1)
d(7)
e(1:7)
e([1 3 4 5 7])


% Structures are a class that can hold different types of data, with each
% one having a name

students.name = 'Johnny Appleseed'
students.age  = 24
students.grade = 'A'

students(2).name = 'Ralphie Buffalo'
students(2).age = 7
% If you look at variable editor, you can see all these inserted into a
% matrix
students(2).grade = 'C';

students.name
students.grade


%% Adjusting image contrast
clear
close all

% wpeppers.jpg is a stock matlab image
img = imread('wpeppers.jpg');
figure
imshow(img)

% Convert to grayscale image
imgGray = rgb2gray(img);

% UINT8 takes up less room, but it doesn't allow for non-integer values.
% To process the image, we should convert it to a double
imgGrayDouble = im2double(imgGray);

% Note the size difference between the integer and double images

% Adjusting contrast linearly is OK.  If you do a gamma correction, then
% you have to say it.

% We can also adjust the contrast by giving a maximum and minimum value
cMin = .1;
cMax = 120;

figure
imshow(imgGray,[cMin cMax])

cMin = .1;
cMax = .5;
figure
imshow(imgGrayDouble,[cMin cMax])


% We can look at the spread of intensities on each pixel.
figure
imhist(imgGrayDouble)

% Matlab will automatically correct it by spacing out across the entire
% range
imgGrayEq = histeq(imgGrayDouble);

figure
imshow(imgGrayEq)

figure
imhist(imgGrayEq)

% We can use imshow to show two images next to each other.  Useful to
% display different steps of processing algorithm.  The square brackets
% concantenate the two, just like making an array

figure
imshow([imgGrayDouble imgGrayEq])


% Matlab also has a built in image contrast tool
figure
imshow(imgGrayDouble)
imcontrast(gca)


%% Adjust contrast if just 1 image
% First make a new variable with the pepper images
img2 = img;

% The first matrix is the red matrix
img2(:,:,1) = img2(:,:,1)*1.5;

figure
imshow([img img2])

img2(:,:,3) = img2(:,:,3)*.7;

figure
imshow([img img2])

% How do we assemble a new color image from gray components. Lets start by
% defining three gray scale images for the red, green and blue

imgR = img(:,:,1);
imgG = img(:,:,2);
imgB = img(:,:,3);

figure
imshow([imgR imgG])

[ysize,xsize] = size(imgR)

% Now we can adjust the contrast of these images...
imgG = 1.7*imgG; % more green
imgR = .7*imgR; % Less red
imgB = 1.7*imgB; % more blue

imgColor = zeros(ysize,xsize,3);
imgColor(:,:,1) = imgG;
imgColor(:,:,2) = imgR;
imgColor(:,:,3) = imgB;

figure
imshow([img imgColor])

