% Issac D. Gerg / Pushkar U. Durve
% CSE 485 - Digital Image Processing I 
% Project 2
% Group Number # 4
% Use the MATLAB function IMADJUST to perform the contrast stretch.


%-------------------------------------------------------------------------------
% Part 1 - bi-cubic interpolation. 
saltandpepper_filename = 'saltAndPepper.gif';
saltandpepper_image = imread(saltandpepper_filename);

% Use the IMRESIZE function to interpolate by a factor of 4 using bi-cubic interpolation. 
picture_saltandpepper_scale = imresize(saltandpepper_image, [60*4 59*4], 'bicubic');
imwrite(picture_saltandpepper_scale, 'saltandpepper_bicubic.jpg', 'jpg');  

%-------------------------------------------------------------------------------
% Part 2(a) - Contrast Stretching
saltandpepper_imadjust = imadjust(picture_saltandpepper_scale, [0 (180/255)], []);
imwrite(saltandpepper_imadjust , 'saltandpepper_imadjust.jpg', 'jpg');  

% Part 2(b) - Mean Gray Level 
saltandpepper_mean = mean2(picture_saltandpepper_scale)
saltandpepper_std = std2(picture_saltandpepper_scale)
low_in = (saltandpepper_mean - (saltandpepper_std) / 2) / 256
high_in = (saltandpepper_mean + (saltandpepper_std) / 2) / 256
saltandpepper_mean = imadjust(picture_saltandpepper_scale, [low_in high_in], []);
imwrite(saltandpepper_mean , 'saltandpepper_mean.jpg', 'jpg');

% Part 2(c) - Stretchlim
low_high = stretchlim(picture_saltandpepper_scale, 0)
saltandpepper_stretchlim = imadjust(picture_saltandpepper_scale, low_high, []);
imwrite(saltandpepper_stretchlim , 'saltandpepper_stretchlim.jpg', 'jpg');  

%-------------------------------------------------------------------------------
% Part 3 - Histogram Equalization 
imhist(picture_saltandpepper_scale);
saltandpepper_EqualizedImage = histeq(picture_saltandpepper_scale);
imwrite(saltandpepper_EqualizedImage, 'saltandpepper_EqualizedImage.jpg', 'jpg');  

%-------------------------------------------------------------------------------
% Part 4 - Subplot
colormap('gray');
subplot(2,2,1);
image(picture_saltandpepper_scale);
title('Original Image');
subplot(2,2,2);
image(saltandpepper_mean);
title('Mean and Std Image');
subplot(2,2,3);
image(saltandpepper_stretchlim);
title('StretchLim Image');
subplot(2,2,4);
image(saltandpepper_EqualizedImage);
title('Equalized Image');

figure;
subplot(2,2,1);
imhist(picture_saltandpepper_scale);
title('Original Image');
subplot(2,2,2);
imhist(saltandpepper_mean);
title('Mean and Std Image');
subplot(2,2,3);
imhist(saltandpepper_stretchlim);
title('StretchLim Image');
subplot(2,2,4);
imhist(saltandpepper_EqualizedImage);
title('Equalized Image');


%-------------------------------------------------------------------------------
% Part 5(a)- FSPECIAL
noise_gaussian = imnoise(saltandpepper_EqualizedImage,'gaussian',0,0.05); 
noise_saltAndpepper = imnoise(saltandpepper_EqualizedImage,'salt & pepper', 0.02);  

figure;
colormap('gray');
subplot(1,3,1);
imagesc(saltandpepper_EqualizedImage);
title('Equalized Image');
subplot(1,3,2);
imagesc(noise_gaussian);
title('Noise Gaussian');
subplot(1,3,3);
imagesc(noise_saltAndpepper);
title('Noise Salt and Pepper');

% Part 5(b)- Spatial Filtering
average_filter_3x3 = fspecial('average', [3 3])
average_filter_5x5 = fspecial('average', [5 5])
average_filter_7x7 = fspecial('average', [7 7])


% Part 5(c)- Imfilter
average_gaussian_3x3 = imfilter (noise_gaussian, average_filter_3x3);
average_gaussian_5x5 = imfilter (noise_gaussian, average_filter_5x5);
average_gaussian_7x7 = imfilter (noise_gaussian, average_filter_7x7);

figure;
colormap('gray');
subplot(2,2,1);
imagesc(noise_gaussian);
title('Noise Gaussian');
subplot(2,2,2);
imagesc(average_gaussian_3x3);
title('Average Gaussian 3x3');
subplot(2,2,3);
imagesc(average_gaussian_5x5);
title('Average Gaussian 5x5');
subplot(2,2,4);
imagesc(average_gaussian_7x7);
title('Average Gaussian 7x7');

% Part 5(d)- Imfilter
average_saltAndpepper_3x3 = medfilt2 (noise_saltAndpepper, [3 3]);
average_saltAndpepper_5x5 = medfilt2 (noise_saltAndpepper, [5 5]);
average_saltAndpepper_7x7 = medfilt2 (noise_saltAndpepper, [7 7]);

figure;
colormap('gray');
subplot(2,2,1);
imagesc(noise_saltAndpepper);
title('Noise saltAndpepper');
subplot(2,2,2);
imagesc(average_saltAndpepper_3x3);
title('Median saltAndpepper 3x3');
subplot(2,2,3);
imagesc(average_saltAndpepper_5x5);
title('Median saltAndpepper 5x5');
subplot(2,2,4);
imagesc(average_saltAndpepper_7x7);
title('Median saltAndpepper 7x7');

%-------------------------------------------------------------------------------
% Part 6(a) Edge Detection
wheel_filename = 'wheel.gif';
wheel_image = imread(wheel_filename);

BW1 = edge(wheel_image,'prewitt');
BW2 = edge(wheel_image,'canny');
BW3 = edge(wheel_image,'sobel');
BW4 = edge(wheel_image,'roberts');

figure;
colormap('gray');
subplot(2,2,1);
imagesc(BW1);
title('Prewitt');
subplot(2,2,2);
imagesc(BW2);
title('Canny');
subplot(2,2,3);
imagesc(BW3);
title('Sobel');
subplot(2,2,4);
imagesc(BW4);
title('Roberts');


% Part 6(b) Motion Blur
motionfilter = fspecial('motion',50,45);
wheel_filter = imfilter (wheel_image, motionfilter);

BW1 = edge(wheel_filter,'prewitt');
BW2 = edge(wheel_filter,'canny');
BW3 = edge(wheel_filter,'sobel');
BW4 = edge(wheel_filter,'roberts');

figure;
colormap('gray');
subplot(2,2,1);
imagesc(BW1);
title('Prewitt');
subplot(2,2,2);
imagesc(BW2);
title('Canny');
subplot(2,2,3);
imagesc(BW3);
title('Sobel');
subplot(2,2,4);
imagesc(BW4);
title('Roberts');


% Part 6(c) Gaussian Noise
gaussian_filter = imnoise (wheel_image, 'gaussian',0,0.001);

BW1 = edge(gaussian_filter,'prewitt');
BW2 = edge(gaussian_filter,'canny');
BW3 = edge(gaussian_filter,'sobel');
BW4 = edge(gaussian_filter,'roberts');

figure;
colormap('gray');
subplot(2,2,1);
imagesc(BW1);
title('Prewitt');
subplot(2,2,2);
imagesc(BW2);
title('Canny');
subplot(2,2,3);
imagesc(BW3);
title('Sobel');
subplot(2,2,4);
imagesc(BW4);
title('Roberts');

% Part 6(d) Filtered Gaussian Noise
avg_gaussian_filter = imfilter (gaussian_filter,average_filter_3x3);

BW1 = edge(avg_gaussian_filter,'prewitt');
BW2 = edge(avg_gaussian_filter,'canny');
BW3 = edge(avg_gaussian_filter,'sobel');
BW4 = edge(avg_gaussian_filter,'roberts');

figure;
colormap('gray');
subplot(2,2,1);
imagesc(BW1);
title('Prewitt');
subplot(2,2,2);
imagesc(BW2);
title('Canny');
subplot(2,2,3);
imagesc(BW3);
title('Sobel');
subplot(2,2,4);
imagesc(BW4);
title('Roberts');
%-------------------------------------------------------------------------------