% Jake Bobowski
% August 16, 2016
% Created using MATLAB R2014a

% This tutorial shows how to import images into MATLAB and then do some 
% basic processing.
clearvars

% Use imread to read the image into MATLAB.
img = imread('30min.jpg');

% Display the image using imshow.
imshow(img)

% Our particular image is of the interference pattern that results when a
% laser is passed through a Michelson interferometer.  Let's rotate the
% image such that the interference pattern is approximately vertical.  Use
% imrotate and supply the rotation angle in degrees.
imgRotate = imrotate(img, 18);
figure();
imshow(imgRotate)

% We can crop the image to keep only the part that is showing the 
% interference patern. First, use size to get the height and width of the image.
[height, width, dim] = size(imgRotate)
% The way that we've called size produces three outputs: the height, width,
% and dimension.  For colour images, the colour is determine by relative 
% levels of red green and blue (RGB), therefore all the information about 
% image is actually encoded in a 3-dimensional matrix in MATLAB.  

% The image can now be cropped using imcrop.  If you type just
% 'imcrop(img)' you'll get an iteractive window that pops up which will
% allow you to draw a crop box on the image.  If you want to do the
% cropping all in code, then you have to supply imcrop with the dimensions
% for cropping in a vector.  The vector elements are: [xmin, ymin, width,
% height].
rect = [1600, 1800, 1900, 1000];
imgCrop = imcrop(imgRotate, rect);
figure();
imshow(imgCrop)

% Often, when analyzing an image, one may wish to convert the image colours 
% to a gray scale.  To do so, use "rgbtogray".  This command assigns a 
% value between 0 (black) and 255 (white) to each pixel in the image.
imgGray = rgb2gray(imgCrop);
figure();
imshow(imgGray)
max(max(imgGray))

% If you want, you can scale the matrix values using mat2gray.  Now black
% pixels are assigned a value of zero and white pixels a value of 1.
imgScaled = mat2gray(imgGray, [0, 255]);
figure();
imshow(imgScaled)
max(max(imgScaled))

% You can also use mat2gray to improve the contrast of your image. 
% mat2gray(imgGray, [100, 150]) will force all pixels with values less than
% 100 to be back and pixels with values greater than 150 to be white.
imgContrast = mat2gray(imgGray, [10, 50]);
figure();
imshow(imgContrast)

% Let's try something fun.  Let's search the first grayscale image for very
% bright pixels and set those to zero.  This should remove the noisy white
% specs.  Then, we can use mat2gray to enhance the contrast.
[height, width, dim] = size(imgGray);
cutoff = 80;
for i = 1:height
    for j = 1:width
        if imgGray(i, j) > cutoff
            imgCut(i, j) = 0;
        else
            imgCut(i, j) = imgGray(i, j);
        end
    end
end
figure();
imshow(imgCut)

% imgCut looks better.  Let's try applying mat2gray.
imgEnhanced = mat2gray(imgCut, [0, 80]);
figure();
imshow(imgEnhanced)

% To export the processed image, we use imwrite.  Here's an example where
% we export imgEnhanced as a jpg.
imwrite(imgEnhanced,'30min-enhanced.jpg','jpg')

% We can now do some other processing of the modified image.  For example,
% if, for some reason, we wanted we could calculate the average intensity 
% of the image.
tot = 0;
for i = 1:height
    for j = 1:width
        tot = tot + imgEnhanced(i, j);
    end
end
avgInt = tot/(height*width)

% Perhaps more interesting, we could try to find the average intensity of
% each column of pixels in the image.
for j = 1:width
    tot = 0;
    for i = 1:height
        tot = tot + imgEnhanced(i, j);
    end
    colAvg(j) = tot/height;
end
plot(1:width, colAvg, 'bo')
xlabel('Column')
ylabel('Average Intensity')
axis([0 1900 0 0.7])