0% found this document useful (0 votes)
17 views11 pages

Assign 2

Uploaded by

Kanish Mahato
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views11 pages

Assign 2

Uploaded by

Kanish Mahato
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Assignment 2:

Digital Image Processing

Submitted by

Kanish Mahato
21103070
CSE

1/11
1. Read 2 different images and add the following noises in the image.
(a) Gaussian Noise
(b) Erlang Noise
(c) Exponential Noise
(d) Uniform Noise
(e) Salt and Pepper Noise

Code :-

img1 = imread('image1.tiff');
img2 = imread('image2.tiff');

gaussianimg1 = imnoise(img1, 'gaussian');


gaussianimg2 = imnoise(img2, 'gaussian');

shape = 2;
scale = 1;
erlangimg1 = img1 + uint8(gamrnd(shape, scale, size(img1)) * 255);
erlangimg2 = img2 + uint8(gamrnd(shape, scale, size(img2)) * 255);

lambda = 1;
expimg1 = exprnd(1/lambda, size(img1));
expimg2 = exprnd(1/lambda, size(img2));
expimg1 = uint8(double(img1) + expimg1 * 255);
expimg2 = uint8(double(img2) + expimg2 * 255);

uniformimg1 = imnoise(img1, 'salt & pepper', 0.1);


uniformimg2 = imnoise(img2, 'salt & pepper', 0.1);

saltPepperimg1 = imnoise(img1, 'salt & pepper');


saltPepperimg2 = imnoise(img2, 'salt & pepper');

figure;
subplot(3, 4, 1), imshow(img1), title('Original Image 1');
subplot(3, 4, 2), imshow(img2), title('Original Image 2');
subplot(3, 4, 3), imshow(gaussianimg1), title('Gaussian Noise 1');
subplot(3, 4, 4), imshow(gaussianimg2), title('Gaussian Noise 2');

2/11
subplot(3, 4, 5), imshow(erlangimg1), title('Erlang Noise 1');
subplot(3, 4, 6), imshow(erlangimg2), title('Erlang Noise 2');

subplot(3, 4, 7), imshow(expimg1), title('Exponential Noise 1');


subplot(3, 4, 8), imshow(expimg2), title('Exponential Noise 2');

subplot(3, 4, 9), imshow(uniformimg1), title('Uniform Noise 1');


subplot(3, 4, 10), imshow(uniformimg2), title('Uniform Noise 2');

subplot(3, 4, 11), imshow(saltPepperimg1), title('Salt & Pepper Noise


1');
subplot(3, 4, 12), imshow(saltPepperimg2), title('Salt & Pepper Noise
2');

Output :-

3/11
2. Remove the noise from the noisy images obtained in Ques 1
using the following filters:
(a) Arithmetic mean filter
(b) Geometric mean filter
(c) Contraharmonic filter
(d) Median Filter
(e) Weiner Filter

Code :-
Contraharmonic filter code as separate file:-
function output = contraharmonicFilter(img, m, n, Q)

paddedImage = padarray(double(img), [floor(m/2), floor(n/2)],


'symmetric');
[rows, cols] = size(img);
output = zeros(rows, cols);

for i = 1:rows
for j = 1:cols
region = paddedImage(i:i+m-1, j:j+n-1);
numerator = sum(sum(region.^(Q + 1)));
denominator = sum(sum(region.^Q));
output(i,j) = numerator / denominator;
end
end

output = uint8(output);
end

img1 = imread('skull.jpg');

gaussianimg1 = imnoise(img1, 'gaussian');


h = fspecial('average', [3 3]);
arithmeticmeanfilteredimg_gaussian = imfilter(gaussianimg1, h);
geometricmeanfilteredimg_gaussian =
exp(imfilter(log(double(gaussianimg1) + 1), h, 'replicate')) - 1;
geometricmeanfilteredimg_gaussian =
uint8(geometricmeanfilteredimg_gaussian);
q = 1.5;
contraharmonicfilteredimg_gaussian =
contraharmonicFilter(gaussianimg1, 3, 3, q);
medianfilteredimg_gaussian = medfilt2(gaussianimg1, [3 3]);

4/11
weinerfilteredimg_gaussian = wiener2(gaussianimg1, [5 5]);

lambda = 1;
erlang_noise = -1/lambda * log(rand(size(img1)));
erlangimg1 = uint8(double(img1) + erlang_noise);
arithmeticmeanfilteredimg_erlang = imfilter(erlangimg1, h);
geometricmeanfilteredimg_erlang =
exp(imfilter(log(double(erlangimg1) + 1), h, 'replicate')) - 1;
geometricmeanfilteredimg_erlang =
uint8(geometricmeanfilteredimg_erlang);
contraharmonicfilteredimg_erlang =
contraharmonicFilter(erlangimg1, 3, 3, q);
medianfilteredimg_erlang = medfilt2(erlangimg1, [3 3]);
weinerfilteredimg_erlang = wiener2(erlangimg1, [5 5]);

exp_noise = -1/lambda * log(rand(size(img1)));


exponentialimg1 = uint8(double(img1) + exp_noise);
arithmeticmeanfilteredimg_exp = imfilter(exponentialimg1, h);
geometricmeanfilteredimg_exp =
exp(imfilter(log(double(exponentialimg1) + 1), h, 'replicate')) -
1;
geometricmeanfilteredimg_exp =
uint8(geometricmeanfilteredimg_exp);
contraharmonicfilteredimg_exp =
contraharmonicFilter(exponentialimg1, 3, 3, q);
medianfilteredimg_exp = medfilt2(exponentialimg1, [3 3]);
weinerfilteredimg_exp = wiener2(exponentialimg1, [5 5]);

uniform_noise = rand(size(img1)) * 0.1;


uniformimg1 = uint8(double(img1) + uniform_noise);
arithmeticmeanfilteredimg_uniform = imfilter(uniformimg1, h);
geometricmeanfilteredimg_uniform =
exp(imfilter(log(double(uniformimg1) + 1), h, 'replicate')) - 1;
geometricmeanfilteredimg_uniform =
uint8(geometricmeanfilteredimg_uniform);
contraharmonicfilteredimg_uniform =
contraharmonicFilter(uniformimg1, 3, 3, q);
medianfilteredimg_uniform = medfilt2(uniformimg1, [3 3]);
weinerfilteredimg_uniform = wiener2(uniformimg1, [5 5]);

saltpepperimg1 = imnoise(img1, 'salt & pepper', 0.02);


arithmeticmeanfilteredimg_sp = imfilter(saltpepperimg1, h);

5/11
geometricmeanfilteredimg_sp =
exp(imfilter(log(double(saltpepperimg1) + 1), h, 'replicate')) -
1;
geometricmeanfilteredimg_sp = uint8(geometricmeanfilteredimg_sp);
contraharmonicfilteredimg_sp =
contraharmonicFilter(saltpepperimg1, 3, 3, q);
medianfilteredimg_sp = medfilt2(saltpepperimg1, [3 3]);
weinerfilteredimg_sp = wiener2(saltpepperimg1, [5 5]);

figure;
subplot(5, 3, 1), imshow(gaussianimg1), title('Gaussian Noise');
subplot(5, 3, 2), imshow(arithmeticmeanfilteredimg_gaussian),
title('AM Filter (Gaussian)');
subplot(5, 3, 3), imshow(geometricmeanfilteredimg_gaussian),
title('GM Filter (Gaussian)');
subplot(5, 3, 4), imshow(erlangimg1), title('Erlang Noise');
subplot(5, 3, 5), imshow(arithmeticmeanfilteredimg_erlang),
title('AM Filter (Erlang)');
subplot(5, 3, 6), imshow(geometricmeanfilteredimg_erlang),
title('GM Filter (Erlang)');
subplot(5, 3, 7), imshow(exponentialimg1), title('Exponential
Noise');
subplot(5, 3, 8), imshow(arithmeticmeanfilteredimg_exp),
title('AM Filter (Exponential)');
subplot(5, 3, 9), imshow(geometricmeanfilteredimg_exp), title('GM
Filter (Exponential)');
subplot(5, 3, 10), imshow(uniformimg1), title('Uniform Noise');
subplot(5, 3, 11), imshow(arithmeticmeanfilteredimg_uniform),
title('AM Filter (Uniform)');
subplot(5, 3, 12), imshow(geometricmeanfilteredimg_uniform),
title('GM Filter (Uniform)');
subplot(5, 3, 13), imshow(saltpepperimg1), title('Salt & Pepper
Noise');
subplot(5, 3, 14), imshow(arithmeticmeanfilteredimg_sp),
title('AM Filter (Salt & Pepper)');
subplot(5, 3, 15), imshow(medianfilteredimg_sp), title('Median
Filter (Salt & Pepper)');

figure;
subplot(5, 2, 1), imshow(contraharmonicfilteredimg_gaussian),
title('Contraharmonic (Gaussian)');
subplot(5, 2, 2), imshow(weinerfilteredimg_gaussian),
title('Wiener Filter (Gaussian)');

6/11
subplot(5, 2, 3), imshow(contraharmonicfilteredimg_erlang),
title('Contraharmonic (Erlang)');
subplot(5, 2, 4), imshow(weinerfilteredimg_erlang), title('Wiener
Filter (Erlang)');
subplot(5, 2, 5), imshow(contraharmonicfilteredimg_exp),
title('Contraharmonic (Exponential)');
subplot(5, 2, 6), imshow(weinerfilteredimg_exp), title('Wiener
Filter (Exponential)');
subplot(5, 2, 7), imshow(contraharmonicfilteredimg_uniform),
title('Contraharmonic (Uniform)');
subplot(5, 2, 8), imshow(weinerfilteredimg_uniform),
title('Wiener Filter (Uniform)');
subplot(5, 2, 9), imshow(contraharmonicfilteredimg_sp),
title('Contraharmonic (Salt & Pepper)');
subplot(5, 2, 10), imshow(weinerfilteredimg_sp), title('Wiener
Filter (Salt & Pepper)');

Output :-

7/11
3. Read a color image in RGB format and display it.
Change the color space of the image to the following:
(a) CMY
(b) HIS
(c) Lab

Code :-
img_rgb = imread('image1.tiff');
figure;

8/11
subplot(2, 2, 1), imshow(img_rgb), title('RGB Image');

img_cmy = 255 - img_rgb;


subplot(2, 2, 2), imshow(img_cmy), title('CMY Image');

img_hsv = rgb2hsv(img_rgb);
subplot(2, 2, 3), imshow(img_hsv), title('HSV Image');

img_lab = rgb2lab(img_rgb);
subplot(2, 2, 4), imshow(lab2rgb(img_lab)), title('Lab Image');

Output :-

4.Read a color image and smooth it using the following


filters:
Averaging filter
Gaussian filter

9/11
Code :-
img_rgb = imread('image1.tiff');
figure;
subplot(1, 3, 1), imshow(img_rgb), title('Original Image');

h_avg = fspecial('average', [3 3]);


img_avg = imfilter(img_rgb, h_avg);
subplot(1, 3, 2), imshow(img_avg), title('Averaging Filter');

h_gaussian = fspecial('gaussian', [3 3], 0.5);


img_gaussian = imfilter(img_rgb, h_gaussian);
subplot(1, 3, 3), imshow(img_gaussian), title('Gaussian Filter');

Output :-

5. Read a color image and sharpen it using Laplacian filter

10/11
Code:-
img_rgb = imread('image1.tiff');
figure;
subplot(1, 3, 1), imshow(img_rgb), title('Original Image');

h_laplacian = fspecial('laplacian', 0.2);


img_laplacian = imfilter(img_rgb, h_laplacian);
subplot(1, 3, 2), imshow(img_laplacian), title('Laplacian Image');

img_sharpened = uint8(double(img_rgb) - double(img_laplacian));


subplot(1, 3, 3), imshow(img_sharpened), title('Sharpened Image');
Output :-

11/11

You might also like