% Matlab Reading Image file
% Modified from demo version

% First, clear the MATLAB workspace of any variables and close open figure windows.

close all

% Read in an image by using imread command. pout.tif is a sample image

I = imread('pout.tif');

% use imshow to show the image

imshow(I)

%To see how the imread function stores the image data in the workspace, check the Workspace browser in 
%he MATLAB desktop. The Workspace browser displays information about all the variables you create 
%during a MATLAB session. The imread function returned the image data in the variable I, which is a 291-
%by-240 element array of uint8 data. MATLAB can store images as uint8, uint16, or double arrays.
%
%can do this by clicking on the file

whos

%pout.tif is a somewhat low contrast image. To see the distribution of intensities in pout.tif, you can create a 
%histogram by calling the imhist function. (Precede the call to imhist with the figure command so that the 
%histogram does not overwrite the display of the image I in the current figure window.)

figure, imhist(I)

%Notice how the intensity range is rather narrow. It does not cover the potential range of [0, 255], and is 
%missing the high and low values that would result in good contrast.
%
%The toolbox provides several ways to improve the contrast in an image. One way is to call the histeq 
%function to spread the intensity values over the full range of the image, a process called histogram %
%equalization.

I2 = histeq(I);

%Display the new equalized image, I2, in a new figure window.

figure, imshow(I2)

%Call imhist again to create a histogram of the equalized image I2. If you compare the two histograms, 
%the histogram of I2 is more spread out than the histogram of I1.

figure, imhist(I2)

%To write the newly adjusted image I2 to a disk file, use the imwrite function. If you include the filename 
%extension '.png', the imwrite function writes the image to a file in Portable Network Graphics (PNG) 
%format, but you can specify other formats.

imwrite (I2, 'pout2.png');
imwrite (I2, 'pout2.jpg');

%To see what imwrite wrote to the disk file, use the imfinfo function.

imfinfo('pout2.png')