%% 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 color in a color 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;

% Note: I'm using the square brackets to create a new array.  In this case,
% I'm creating an array of 2 images.
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])

