%-------------------------------------------------------------------------------
% Project 2
% Group 8
% Isaac Gerg (idg101)
% CSE 486
%-------------------------------------------------------------------------------

clc;
%colormap('default');

%-------------------------------------------------------------------------------
% Part 1 - Edge Detection
	imgPepper = imread('pepper.gif', 'gif');
	
    % Note: the imdetectedges function uses the magnitude approximation of |G| = |G-x| + |G_y|
    % Roberts
    imwrite(imdetectedges(imgPepper, 'roberts', 0.1119), 'RobertsEdgeDetection.jpg', 'jpg');
    
	% Sobel
    imwrite(imdetectedges(imgPepper, 'sobel', 0.1429), 'SobelEdgeDetection.jpg', 'jpg');
	
	% Prewitt
    imwrite(imdetectedges(imgPepper, 'prewitt', 0.1111), 'PrewittEdgeDetection.jpg', 'jpg');
	
    % LoG
	imwrite(imdetectedges(imgPepper, 'log', 0.1), 'LogEdgeDetection.jpg', 'jpg');
	
	% Canny
	imwrite(imdetectedges(imgPepper, 'canny', 0), 'CannyEdgeDetection.jpg', 'jpg');

%-------------------------------------------------------------------------------
% Part 2 - Noisy Images

	% Salt and Pepper
	imgSaltAndPepperNoise = imnoise(imgPepper, 'salt & pepper', 0.1);
	imwrite(imgSaltAndPepperNoise, 'SaltAndPepperNoise.jpg', 'jpg');
	
	% Gaussian
	imgGaussianNoise = imnoise(imgPepper, 'gaussian', 0, 0.098);
    % 25/255 = 0.098
	imwrite(imgGaussianNoise, 'GaussianNoise.jpg', 'jpg');
    
    % Uniform noise
    imgUniformNoise = imgPepper;
    for X = 1:256,
        for Y = 1:256,
            intOffset = round(30*rand(1) - 15);
            % Convert the imgPepper Image to double here in order to use + operator. 
            imgUniformNoise(X,Y) = double(imgPepper(X,Y)) + intOffset;
        end
    end
    imwrite(imgUniformNoise, 'UniformNoise.jpg', 'jpg');
    
%-------------------------------------------------------------------------------
% Part 3 - Image Filtering
	iMeanFilter3_3 = [(1/9) (1/9) (1/9);
                      (1/9) (1/9) (1/9);
                      (1/9) (1/9) (1/9)];
	iMeanFilter5_5 = [(1/25) (1/25) (1/25);
                      (1/25) (1/25) (1/25);
                      (1/25) (1/25) (1/25)];
	iGaussianFilter5_5 = [1  4  7  4 1;
                          4 16 28 16 4;
                          7 28 49 28 7;
                          4 16 28 16 4;
                          1  4  7  4 1];
                  
	% Edge Detection with Salt and Pepper Noise
		imgMean3_3 = conv2(imgSaltAndPepperNoise, iMeanFilter3_3);
		imgMean5_5 = conv2(imgSaltAndPepperNoise, iMeanFilter5_5);
		imgGaussian5_5 = conv2(imgSaltAndPepperNoise, iGaussianFilter5_5);
		imgMedian3_3 = medfilt2(imgSaltAndPepperNoise, [3 3]);
		imgMedian5_5 = medfilt2(imgSaltAndPepperNoise, [5 5]);
        
        imwrite(imdetectedges(imgMean3_3, 'prewitt', 0.1111), 'S_P_Mean3_3_WithPrewitt.jpg', 'jpg');
        imwrite(imdetectedges(imgMean5_5, 'prewitt', 0.1111), 'S_P_Mean5_5_WithPrewitt.jpg', 'jpg');
        imwrite(imdetectedges(imgGaussian5_5, 'prewitt', 0.1111), 'S_P_Gaussian5_5_WithPrewitt.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian3_3, 'prewitt', 0.1111), 'S_P_Median3_3_WithPrewitt.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian5_5, 'prewitt', 0.1111), 'S_P_Median5_5_WithPrewitt.jpg', 'jpg');

        imwrite(imdetectedges(imgMean3_3, 'Roberts', 0.1119), 'S_P_Mean3_3_WithRoberts.jpg', 'jpg');
        imwrite(imdetectedges(imgMean5_5, 'Roberts', 0.1119), 'S_P_Mean5_5_WithRoberts.jpg', 'jpg');
        imwrite(imdetectedges(imgGaussian5_5, 'Roberts', 0.1119), 'S_P_Gaussian5_5_WithRoberts.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian3_3, 'Roberts', 0.1119), 'S_P_Median3_3_WithRoberts.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian5_5, 'Roberts', 0.1119), 'S_P_Median5_5_WithRoberts.jpg', 'jpg');

        imwrite(imdetectedges(imgMean3_3, 'Sobel', 0.1429), 'S_P_Mean3_3_WithSobel.jpg', 'jpg');
        imwrite(imdetectedges(imgMean5_5, 'Sobel', 0.1429), 'S_P_Mean5_5_WithSobel.jpg', 'jpg');
        imwrite(imdetectedges(imgGaussian5_5, 'Sobel', 0.1429), 'S_P_Gaussian5_5_WithSobel.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian3_3, 'Sobel', 0.1429), 'S_P_Median3_3_WithSobel.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian5_5, 'Sobel', 0.1429), 'S_P_Median5_5_WithSobel.jpg', 'jpg');
        
        imwrite(imdetectedges(imgMean3_3, 'Log', 0.1), 'S_P_Mean3_3_WithLog.jpg', 'jpg');
        imwrite(imdetectedges(imgMean5_5, 'Log', 0.1), 'S_P_Mean5_5_WithLog.jpg', 'jpg');
        imwrite(imdetectedges(imgGaussian5_5, 'Log', 0.1), 'S_P_Gaussian5_5_WithLog.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian3_3, 'Log', 0.1), 'S_P_Median3_3_WithLog.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian5_5, 'Log', 0.1), 'S_P_Median5_5_WithLog.jpg', 'jpg');
        
        imwrite(imdetectedges(imgMean3_3, 'Canny', 0), 'S_P_Mean3_3_WithCanny.jpg', 'jpg');
        imwrite(imdetectedges(imgMean5_5, 'Canny', 0), 'S_P_Mean5_5_WithCanny.jpg', 'jpg');
        imwrite(imdetectedges(imgGaussian5_5, 'Canny', 0), 'S_P_Gaussian5_5_WithCanny.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian3_3, 'Canny', 0), 'S_P_Median3_3_WithCanny.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian5_5, 'Canny', 0), 'S_P_Median5_5_WithCanny.jpg', 'jpg');
        
	% Edge Detection with Gaussian Noise
		imgMean3_3 = conv2(imgGaussianNoise, iMeanFilter3_3);
		imgMean5_5 = conv2(imgGaussianNoise, iMeanFilter5_5);
		imgGaussian5_5 = conv2(imgGaussianNoise, iGaussianFilter5_5);
		imgMedian3_3 = medfilt2(imgGaussianNoise, [3 3]);
		imgMedian5_5 = medfilt2(imgGaussianNoise, [5 5]);
        
        imwrite(imdetectedges(imgMean3_3, 'prewitt', 0.1111), 'Gaussian_Mean3_3_WithPrewitt.jpg', 'jpg');
        imwrite(imdetectedges(imgMean5_5, 'prewitt', 0.1111), 'Gaussian_Mean5_5_WithPrewitt.jpg', 'jpg');
        imwrite(imdetectedges(imgGaussian5_5, 'prewitt', 0.1111), 'Gaussian_Gaussian5_5_WithPrewitt.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian3_3, 'prewitt', 0.1111), 'Gaussian_Median3_3_WithPrewitt.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian5_5, 'prewitt', 0.1111), 'Gaussian_Median5_5_WithPrewitt.jpg', 'jpg');

        imwrite(imdetectedges(imgMean3_3, 'Roberts', 0.1119), 'Gaussian_Mean3_3_WithRoberts.jpg', 'jpg');
        imwrite(imdetectedges(imgMean5_5, 'Roberts', 0.1119), 'Gaussian_Mean5_5_WithRoberts.jpg', 'jpg');
        imwrite(imdetectedges(imgGaussian5_5, 'Roberts', 0.1119), 'Gaussian_Gaussian5_5_WithRoberts.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian3_3, 'Roberts', 0.1119), 'Gaussian_Median3_3_WithRoberts.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian5_5, 'Roberts', 0.1119), 'Gaussian_Median5_5_WithRoberts.jpg', 'jpg');

        imwrite(imdetectedges(imgMean3_3, 'Sobel', 0.1429), 'Gaussian_Mean3_3_WithSobel.jpg', 'jpg');
        imwrite(imdetectedges(imgMean5_5, 'Sobel', 0.1429), 'Gaussian_Mean5_5_WithSobel.jpg', 'jpg');
        imwrite(imdetectedges(imgGaussian5_5, 'Sobel', 0.1429), 'Gaussian_Gaussian5_5_WithSobel.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian3_3, 'Sobel', 0.1429), 'Gaussian_Median3_3_WithSobel.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian5_5, 'Sobel', 0.1429), 'Gaussian_Median5_5_WithSobel.jpg', 'jpg');
        
        imwrite(imdetectedges(imgMean3_3, 'Log', 0.1), 'Gaussian_Mean3_3_WithLog.jpg', 'jpg');
        imwrite(imdetectedges(imgMean5_5, 'Log', 0.1), 'Gaussian_Mean5_5_WithLog.jpg', 'jpg');
        imwrite(imdetectedges(imgGaussian5_5, 'Log', 0.1), 'Gaussian_Gaussian5_5_WithLog.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian3_3, 'Log', 0.1), 'Gaussian_Median3_3_WithLog.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian5_5, 'Log', 0.1), 'Gaussian_Median5_5_WithLog.jpg', 'jpg');
        
        imwrite(imdetectedges(imgMean3_3, 'Canny', 0), 'Gaussian_Mean3_3_WithCanny.jpg', 'jpg');
        imwrite(imdetectedges(imgMean5_5, 'Canny', 0), 'Gaussian_Mean5_5_WithCanny.jpg', 'jpg');
        imwrite(imdetectedges(imgGaussian5_5, 'Canny', 0), 'Gaussian_Gaussian5_5_WithCanny.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian3_3, 'Canny', 0), 'Gaussian_Median3_3_WithCanny.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian5_5, 'Canny', 0), 'Gaussian_Median5_5_WithCanny.jpg', 'jpg');
        
	% Edge Detection with Uniform Noise
		imgMean3_3 = conv2(imgUniformNoise, iMeanFilter3_3);
		imgMean5_5 = conv2(imgUniformNoise, iMeanFilter5_5);
		imgGaussian5_5 = conv2(imgUniformNoise, iGaussianFilter5_5);
		imgMedian3_3 = medfilt2(imgUniformNoise, [3 3]);
		imgMedian5_5 = medfilt2(imgUniformNoise, [5 5]);
        
        imwrite(imdetectedges(imgMean3_3, 'prewitt', 0.1111), 'Uniform_Mean3_3_WithPrewitt.jpg', 'jpg');
        imwrite(imdetectedges(imgMean5_5, 'prewitt', 0.1111), 'Uniform_Mean5_5_WithPrewitt.jpg', 'jpg');
        imwrite(imdetectedges(imgGaussian5_5, 'prewitt', 0.1111), 'Uniform_Gaussian5_5_WithPrewitt.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian3_3, 'prewitt', 0.1111), 'Uniform_Median3_3_WithPrewitt.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian5_5, 'prewitt', 0.1111), 'Uniform_Median5_5_WithPrewitt.jpg', 'jpg');

        imwrite(imdetectedges(imgMean3_3, 'Roberts', 0.1119), 'Uniform_Mean3_3_WithRoberts.jpg', 'jpg');
        imwrite(imdetectedges(imgMean5_5, 'Roberts', 0.1119), 'Uniform_Mean5_5_WithRoberts.jpg', 'jpg');
        imwrite(imdetectedges(imgGaussian5_5, 'Roberts', 0.1119), 'Uniform_Gaussian5_5_WithRoberts.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian3_3, 'Roberts', 0.1119), 'Uniform_Median3_3_WithRoberts.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian5_5, 'Roberts', 0.1119), 'Uniform_Median5_5_WithRoberts.jpg', 'jpg');

        imwrite(imdetectedges(imgMean3_3, 'Sobel', 0.1429), 'Uniform_Mean3_3_WithSobel.jpg', 'jpg');
        imwrite(imdetectedges(imgMean5_5, 'Sobel', 0.1429), 'Uniform_Mean5_5_WithSobel.jpg', 'jpg');
        imwrite(imdetectedges(imgGaussian5_5, 'Sobel', 0.1429), 'Uniform_Gaussian5_5_WithSobel.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian3_3, 'Sobel', 0.1429), 'Uniform_Median3_3_WithSobel.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian5_5, 'Sobel', 0.1429), 'Uniform_Median5_5_WithSobel.jpg', 'jpg');
        
        imwrite(imdetectedges(imgMean3_3, 'Log', 0.1), 'Uniform_Mean3_3_WithLog.jpg', 'jpg');
        imwrite(imdetectedges(imgMean5_5, 'Log', 0.1), 'Uniform_Mean5_5_WithLog.jpg', 'jpg');
        imwrite(imdetectedges(imgGaussian5_5, 'Log', 0.1), 'Uniform_Gaussian5_5_WithLog.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian3_3, 'Log', 0.1), 'Uniform_Median3_3_WithLog.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian5_5, 'Log', 0.1), 'Uniform_Median5_5_WithLog.jpg', 'jpg');
        
        imwrite(imdetectedges(imgMean3_3, 'Canny', 0), 'Uniform_Mean3_3_WithCanny.jpg', 'jpg');
        imwrite(imdetectedges(imgMean5_5, 'Canny', 0), 'Uniform_Mean5_5_WithCanny.jpg', 'jpg');
        imwrite(imdetectedges(imgGaussian5_5, 'Canny', 0), 'Uniform_Gaussian5_5_WithCanny.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian3_3, 'Canny', 0), 'Uniform_Median3_3_WithCanny.jpg', 'jpg');
        imwrite(imdetectedges(imgMedian5_5, 'Canny', 0), 'Uniform_Median5_5_WithCanny.jpg', 'jpg');