% Isaac D. Gerg / Pushkar U. Durve
% CSE 485 - Digital Image Processing I 
% Project 5
% Group Number # 4

clc;
close all;

Disp('Running...');
%-------------------------------------------------------------------------------
% JPEG Compression Analysis

    % Part 2
	setdefaults;               % Set the default quantizer, etc.
	x = getpgm('lena.pgm');    % Get a graymap image
	Tx = dct(x);               % Do the 8x8 cosine transform
	QTx = quant(Tx);           % Quantize, using standard 8x8 quantizer
	Ty = dequant(QTx);         % Dequantize
	y = invdct(Ty);            % Recover the image
	imgError = imabsdiff(x,y);
	
	figure('Name', '8x8 DCT', 'NumberTitle', 'off', 'MenuBar', 'none');
	subplot(2,2,2);
	imagesc(y);
	title('Quantized Image');
	subplot(2,2,1);
	imagesc(x);
	title('Original Image');
	subplot(2,2,3);
	imagesc(imgError);
	title('Error Matrix');
	colormap('gray');
	
	% Part 3
	r = [];
	for i = 1:10,
        setdefaults;               % Set the default quantizer, etc.
        x = getpgm('lena.pgm');    % Get a graymap image
      stdQ = ...
        [16 11 10 16 24 40 51 61;
        12 12 14 19 26 58 60 55;
        14 13 16 24 40 57 69 56;
        14 17 22 29 51 87 80 62;
        18 22 37 56 68 109 103 77;
        24 35 55 64 81 104 113 92;
        49 64 78 87 103 121 120 101;
        72 92 95 98 112 100 103 99];
        QuatizationMatrix = stdQ .* i;
        y = zeros(size(x));
        [y, c] = jpeg(x, QuatizationMatrix);
        r = [r , c];
        figure('Name', sprintf('Iteration: %i', i), 'NumberTitle', 'off', 'MenuBar', 'none');
        subplot(1,1,1);
        imagesc(y);
        %gtext(sprintf('Compression Ratio: %6.4f', c));
        colormap(gray);
	end
	
	figure('Name', 'Compression Ratio vs. Interation', 'NumberTitle', 'off', 'MenuBar', 'none');
	plot(r);
	xlabel('Iteration');
	ylabel('Compression Ration (percentage)');

%-------------------------------------------------------------------------------
% Part 4
    % a - Block Diagram
    % b - Scripting
%-------------------------------------------------------------------------------
% Part 5 - 7 - MPEG Analysis
	
	load 'mri';
	E = [];
	E = reshape(D, size(D,1), size(D,2),size(D,4));
	M = moviein(size(D,4));
	figure('Name', 'MRI', 'NumberTitle', 'off', 'MenuBar', 'none');
	
	for i = 1:size(E,3),
        I = E(:,:,i);
        %pause(1);
        imshow(I);
        M(:,i) = getframe;
	end
	
	[X,MAP] = GRAY2IND(I,256);
	mpgwrite(M, MAP, 'Mri.mpeg');

%-------------------------------------------------------------------------------
% Part 8
	M = mpgread('Mri.mpeg', 1:siz(3));
	figure('Name', 'Matlab Playback', 'NumberTitle', 'off', 'MenuBar', 'none');
    colormap(gray);
    movie(M);

%-------------------------------------------------------------------------------
% Part 9
    % a - Block Diagram
    % b - Scripting
%-------------------------------------------------------------------------------
% Part 10 - Mathematical Morphology
    
    % Algorithm 1
    I = imread('cell.tif');
        %figure, imshow(I), title('original image');
    BWs = edge(I, 'sobel', (graythresh(I) * .1));
        %figure, imshow(BWs), title('binary gradient mask');
    se90 = strel('line', 3, 90);
    se0 = strel('line', 3, 0);
    BWsdil = imdilate(BWs, [se90 se0]);
    BWdfill = imfill(BWsdil, 'holes');
        %figure, imshow(BWdfill);
        %title('binary image with filled holes');
    BWnobord = imclearborder(BWdfill, 4);
        %figure, imshow(BWnobord), title('cleared border image');
    seD = strel('diamond',1);
    BWfinal = imerode(BWnobord,seD);
    BWfinal = imerode(BWfinal,seD);
        %figure, imshow(BWfinal), title('segmented image');
    BWoutline = bwperim(BWfinal);
    Segout = I; 
    Segout(BWoutline) = 255; 
    figure('Name', 'Outline of Cell', 'NumberTitle', 'off', 'MenuBar', 'none');
    imshow(Segout), title('Outlined Original Image');

    % Algorithm 2
    I = imread('cell.tif');
    I = imrotate(I, 90, 'bilinear');
        BWs = edge(I, 'sobel', (graythresh(I) * .1));
        %figure, imshow(BWs), title('binary gradient mask');
    se90 = strel('line', 3, 90);
    se0 = strel('line', 3, 0);
    BWsdil = imdilate(BWs, [se90 se0]);
    BWdfill = imfill(BWsdil, 'holes');
        %figure, imshow(BWdfill);
        %title('binary image with filled holes');
    BWnobord = imclearborder(BWdfill, 4);
        %figure, imshow(BWnobord), title('cleared border image');
    seD = strel('diamond',1);
    BWfinal = imerode(BWnobord,seD);
    BWfinal = imerode(BWfinal,seD);
        %figure, imshow(BWfinal), title('segmented image');
    BWoutline = bwperim(BWfinal);
    Segout = I; 
    Segout(BWoutline) = 255; 
    figure('Name', 'Outline of Cell', 'NumberTitle', 'off', 'MenuBar', 'none');
    imshow(Segout), title('Outlined Original Image');

    % Algorithm 3
    I = imread('cell.tif');
    I = imrotate(I, 45, 'bilinear');
%         BWs = edge(I, 'sobel', (graythresh(I) * .1));
%         figure, imshow(BWs), title('binary gradient mask');
    se90 = strel('line', 3, 90);
    se0 = strel('line', 3, 0);
    BWsdil = imdilate(BWs, [se90 se0]);
    figure, imshow(BWsdil), title('dilated gradient mask');
    BWdfill = imfill(BWsdil, 'holes');
%         figure, imshow(BWdfill);
%         title('binary image with filled holes');
    BWnobord = imclearborder(BWdfill, 4);
%         figure, imshow(BWnobord), title('cleared border image');
    seD = strel('diamond',1);
    BWfinal = imerode(BWnobord,seD);
    BWfinal = imerode(BWfinal,seD);
%         figure, imshow(BWfinal), title('segmented image');
    BWoutline = bwperim(BWfinal);
    Segout = I; 
    Segout(BWoutline) = 255; 
    figure('Name', 'Outline of Cell', 'NumberTitle', 'off', 'MenuBar', 'none');
    imshow(Segout), title('Outlined Original Image');
%-------------------------------------------------------------------------------
Disp('Done.');
    
%-------------------------------------------------------------------------------