Image Processing TP2
Image Processing TP2
image = imread('image3.jpg');
imshow(image)
title('Original Image')
image2 = im2gray(image);
imshow(image2)
title('Image in Gray Scale')
1
D = [0.05 0.1 0.2 0.3 0.4 0.5] ; %Noise Density
M = 0 ;%Mean
V = 0.06; %Variance
J = imnoise(image2,'gaussian',M,V);
figure;
imshow(J)
title('Gaussian Noise Image');
2
figure;
for i=1:size(D,2)
K = imnoise(image2,'salt & pepper',D(i));
subplot(3,2,i)
imshow(K)
title( 'Salt and Pepper Noise Img with '+ string(D(i))+ ' density');
end
3
Exercise 2: Comparison of spatial low pass filters
%Gaussian filter
n = [3 7 11];
len_n = size(n,2);
sigma = (n-1)/6;
for i=1:len_n
img_blur_gauss = imgaussfilt(J,sigma(i));
figure;
montage({J,img_blur_gauss})
title('Noisy Image (Left) Vs. Gaussian Filtered Image (Right)')
SNR_gauss = 10*log10((sum(sum(image2.^2)))/(sum(sum((img_blur_gauss-image2).^2))))
end
4
SNR_gauss = 3.5652
SNR_gauss = 4.3679
SNR_gauss = 4.5959
5
%Average filter
avg3 = ones(3)/9;
avg7 = ones(7)/49;
avg11 = ones(11)/121;
img_blur_avg = imfilter(J, avg3, 'symmetric');
figure;
montage({J,img_blur_avg})
title('Noisy Image (Left) Vs. Average Filtered Image (Right)')
SNR_avg3 = 10*log10((sum(sum(image2.^2)))/(sum(sum((img_blur_avg-image2).^2))))
SNR_avg3 = 4.2240
SNR_avg7 = 10*log10((sum(sum(image2.^2)))/(sum(sum((img_blur_avg-image2).^2))))
6
SNR_avg7 = 4.5900
SNR_avg11 = 10*log10((sum(sum(image2.^2)))/(sum(sum((img_blur_avg-image2).^2))))
SNR_avg11 = 4.5818
%Median filter
for i=1:len_n
img_blur_med = medfilt2(J,[n(i) n(i)]);
figure;
montage({J,img_blur_med})
title('Noisy Image (Left) Vs. Median Filtered Image (Right)')
SNR_med = 10*log10((sum(sum(image2.^2)))/(sum(sum((img_blur_med-image2).^2))))
end
7
SNR_med = 4.5614
SNR_med = 6.4125
SNR_med = 7.4114
8
Exercise 3: Spatial High pass filters
1. Calculate the modulus is the orientation of the gradient using a Sobel filter on the image of your choice.
Comment.
BW = edge(image2,"sobel");
figure;
imshowpair(image2,BW,'montage')
title('Original Image (Left) Vs. Sobel Filtered Image(Right)')
laplacian_filter = fspecial('laplacian');
conv_image = conv2(im2double(image2),laplacian_filter);
imshowpair(image2, conv_image,'montage')
title('Original Image (Left) Vs. Laplacian Filtered Image (Right)')
9
zerocross_image = edge(conv_image,"zerocross");
imshowpair(image2, zerocross_image,'montage')
title('Original Image (Left) Vs. Zerocross Filtered Image (Right)')
3. Convolve the image with a LOG then a DOG of size 5 × 5 suitably generated. Plot the profiles of the two
filters. Determine zero crossings.
%Log filter
log = fspecial('log',5,2);
log_image = conv2(image2,log,'same');
%dog filter
H1 = fspecial('gaussian',5,15);
H2 = fspecial('gaussian',5,20);
dog = H1 - H2;
dog_image = conv2(image2,dog,'same');
imshowpair(log_image, dog_image,'montage')
title('LoG Image (Left) Vs. DoG Image (Right)')
10
zerocross_log_image = edge(log_image,"zerocross");
zerocross_dog_image = edge(dog_image,"zerocross");
imshowpair(zerocross_log_image, zerocross_dog_image,'montage')
title('Zero crossings LoG Image (Left) Vs. Zero crossings DoG Image (Right)')
Exercise 4
1. Load the images: cameraman, kids, pout and flowers (imread then imshow). Change the colormaps using
matlab colormaps Matlab (PINK, HSV, GRAY, HOT, COOL, BONE, COPPER , FLAG).
img1 = imread('cameraman.tif');
img2 = imread('kids.tif');
img3 = imread('pout.tif');
img4 = imread('flowers.jpg')
11
img4(:,:,1) =
95 101 99 81 72 64 49 47 31 30 31 22 21 24 18 22 24 23 21
imshow(img1)
imshow(img2)
imshow(img3)
12
img4 = im2gray(img4);
13
% Change the colormap to HSV
figure;
imshow(img4, hsv);
title('HSV Colormap');
14
% Change the colormap to gray
figure;
imshow(img4, gray);
title('Gray Colormap');
15
% Change the colormap to hot
figure;
imshow(img4, hot);
title('Hot Colormap');
16
% Change the colormap to cool
figure;
imshow(img4, cool);
title('Cool Colormap');
17
% Change the colormap to bone
figure;
imshow(img4, bone);
title('Bone Colormap');
18
% Change the colormap to copper
figure;
imshow(img4, copper);
title('Copper Colormap');
19
% Change the colormap to flag
figure;
imshow(img4, flag);
title('Flag Colormap');
20
21