You are on page 1of 22

G.

VENKATASWAMY NAIDU COLLEGE (SFC), KOVILPATTI - 2


(Re-Accredited with ‘A’ Grade by NAAC)
Department of Computer Science

DIGITAL IMAGE PROCESSING LAB


INDEX
S.No Date Programs Signature
1. Simulation and Display of an Image, components of the
image
2. Display color Image, find its complement and convert
to gray scale

3. Implementation of Image Smoothening Filters(Mean and


Median filtering of an Image)

4. Implementation of Transformations of an Image Scaling


& Rotation

5. Contrast stretching of a low contrast image, Histogram,


and Histogram Equalization

6. Implementation of Non-Linear Filtering in Image

7. Implementation of image sharpening filters and Edge


Detection using operators

8. Implementation of 2D DCT and 2D DFT

9. Implementation of image in Filtering frequency domain

10. Conversion between color space

11. Implementation of Discrete Wavelet Transform

12. Canny edge detection Algorithm

1. Simulation and Display of an Image, components of the image,negative


% Red Blue and Green and Gray Components
i=imread('cancercell.jpg');
subplot(3,2,1); imshow(i); title('Original Image');

%Red Component
r=i(:,:,1);
subplot(3,2,2); imshow(r);title('Red Component');

%Green Component
g=i(:,:,2);
subplot(3,2,3); imshow(g); title('Green Component');

%Blue Component
b=i(:,:,3);
subplot(3,2,4); imshow(b); title('Blue Component');

%Color to Gray Image


rg=rgb2gray(i);
subplot(3,2,5); imshow(rg); title('Gray Image');

%Color to negative Image


Rg1=rgb2bw(i);
subplot(3,2,6); imshow(rg1); title('Gray Image');

2. Display color Image, find its complement and convert to gray scale
I=imread('cancercell.jpg');
subplot(2,2,1); imshow(I); subimage(I); title('Color Image');

c=imcomplement(I);
subplot(2,2,2); imshow(c); subimage(c); title('Complement of color Image');

r=rgb2gray(I);
subplot(2,2,3); imshow(r); subimage(r); title('Gray scale of color Image');

%Complement of Gray Image


b=imcomplement(r);
subplot(2,2,4); imshow(b); subimage(b); title('Complement of Gray Image');
%Simulation of an Image( Arithmetic & Logic Operation)
a=ones(40);
b=zeros(40);
c=[a b;b a];
d=[b b;a a];
A=10*(c+d);
M =c.*d;
S=c-d;
D=c/4;
figure;
subplot(3,2,1); imshow(c);
subplot(3,2,2); imshow(d);
subplot(3,2,3); imshow(A);
subplot(3,2,4); imshow(M);
subplot(3,2,5); imshow(S);
subplot(3,2,6); imshow(D);
3.Implementation of Image Smoothening Filters(Mean and Median filtering of an Image)
% Median Filters
I=imread('nuron.jpg');
K = rgb2gray(I);
J= imnoise(K ,'salt & pepper',0.05);
f= medfilt2(J,[3,3]);
f1=medfilt2(J,[10,10]);

subplot(3,2,1), imshow(I), title('Original Image');


subplot(3,2,2), imshow(K), title('Gray Image');
subplot(3,2,3), imshow(J), title('Noise added Image');
subplot(3,2,4), imshow(f), title('3x3 Image');
subplot(3,2,5), imshow(f1),title('10x10 Image');

%Mean Filter and Average Filter


figure; i=imread('nuron.jpg');
g=rgb2gray(i); g1=fspecial('average',
[3 3]); b1 = imfilter(g,g1);
subplot(2,2,1); imshow(i); title('Original Image');
subplot(2,2,2); imshow(g); title('Gray Image');
subplot(2,2,3); imshow(b1); title('3x3 Image'); g2=
fspecial('average',[10 10]); b2=imfilter(g,g2);
subplot(2,2,4); imshow(b2); title('10x10 Image');

%Implementation of filter using Convolution


figure;
I= imread('earcell.jpg');
I=I(:,:,1); subplot(2,2,1); imshow(I); title('Original Image');

a=[0.001 0.001 0.001; 0.001 0.001 0.001; 0.001 0.001 0.001];


R=conv2(a,I);
subplot(2,2,2); imshow(R); title('Filtered Image');

b=[0.005 0.005 0.005; 0.005 0.005 0.005; 0.005 0.005 0.005];


R1=conv2(b,I);
subplot(2,2,3); imshow(R1); title('Filtered Image 2');
4 Implementation of Transformations of an Image %Scaling & Rotation

% Scaling (Resize)
I=imread('earcell.jpg');
subplot(2,2,1); subimage(I); title('Original Image');

s=input('Enter Scaling Factor');


j=imresize(I,s);
subplot(2,2,2); subimage(j); title('Scaled Image');

% Rotation
K=imrotate(j,60);
subplot(2,2,3); imshow(K); title('Rotated Image 60deg');

R=imrotate(j,45);
subplot(2,2,4); imshow(R); title('Rotated Image 45deg');
5. Contrast stretching of a low contrast image, Histogram, and Histogram Equalization

% Image Enhancement
I=imread('cancercell.jpg');
subplot(4,2,1); imshow(I); title('Original Image');

g=rgb2gray(I);
subplot(4,2,5); imshow(g); title('Gray Image');

J=imadjust(g,[0.3 0.7],[]);
subplot(4,2,3); imshow(J); title('Enhanced Image');

D= imadjust(I,[0.2 0.3 0; 0.6 0.7 1],[]);


subplot(4,2,4);imshow(D);title('Enhanced Image 2');

% Histogram and Histogram Equalization


subplot(4,2,7); imhist(g); title('Histogram of Gray Image');

m=histeq(g);
subplot(4,2,6); imshow(m); title('Equalized Image');
subplot(4,2,8); imhist(m); title('Histogram of Equalized Image');
6.Implementation of Non-Linear Filtering in Image

i= imread(‘filt.jpg’)
% Create a normalized 5-by-5 pixel averaging filter

h=ones(5,5)/25;

imf=imfilter(i,h);
ims=stdfilt(i);
ime=entropyfilt(i);

i1= imread(‘filt1.jpg’);
imr=rangefilt(i1);

subplot(3,2,1), imshow(i), title(‘Original Image i’);


subplot(3,2,2), imshow(imf), title(‘Using imfilter’);

subplot(3,2,3), imshow(ims,[]), title(‘Using Standard’);


subplot(3,2,4), imshow(ime), title(‘Entropy’);

subplot(3,2,5), imshow(i1), title(‘Original Image i1’);


subplot(3,2,6), imshow(imr), title(‘Using Range’);
7.Implementation of image sharpening filters and Edge Detection using operators

i=imread('cancercell.jpg');
subplot(4,2,1); imshow(i);
title('Original Image');

g=rgb2gray(i);
subplot(4,2,2); imshow(g); title('Gray Image');

f=fspecial('laplacian',0.05);
im=imfilter(g,f);
subplot(4,2,3); imshow(im); title('Laplacian ');

s=edge(g, 'sobel');
subplot(4,2,4); imshow(s); title('Sobel');

p=edge(g, 'prewitt');
subplot(4,2,5); imshow(p); title('Prewitt');

r=edge(g, 'roberts');
subplot(4,2,6); imshow(r); title('Roberts');

[BW,thresh,gv,gh]=edge(g,'sobel',[],'horizontal');
[BW1,thresh1,gv1,gh1]=edge(g,'sobel',[],'vertical');

subplot(4,2,7); imshow(BW); title('Sobel Horizontal');


subplot(4,2,8);
imshow(BW); title('Sobel Vertical');
8. Implementation of 2D DCT and 2D DFT

% 2D DFT of Image

i=imread(‘cup.jpg’);

i1=fft2(i);
i2=fftshift(i1);
i3=ifft2(i2);

subplot(2,2,1), imshow(i), title(‘original image’);


subplot(2,2,2), imshow(i1), title(‘Fourier Transform’);
subplot(2,2,3), imshow(i2), title(‘Shifted Fourier Transform image’);
subplot(2,2,4), imshow(uint8(i3)),title(‘Inverse Fourier Transformed image’);
% 2D DCT of image

RGB = imread('autumn.tif');
I = rgb2gray(RGB);
J = dct2(I);
J(abs(J)<10) = 0;
K = idct2(J);
% figure, imshow(I)

subplot(2,2,1),imshow(RGB),title('Original image');
subplot(2,2,2),imshow(I),title('grayscale image');
subplot(2,2,3),imshow(log(abs(J)),[]), colormap(jet), colorbar,title('colorbar for discrete
cosine transform');
subplot(2,2,4),imshow(K,[0 255]),title('inverse discrete cosine');
9. Implementation of image in Filtering frequency domain

clc;
clear all;

%% Loading Image
im=double(rgb2gray(imread('ffilt.jpg')));
F_u_v=fft2(im);
F_u_v=(fftshift(F_u_v));
figure(1);
subplot(1,2,1);imshow(uint8(im));
title('Original Image');

temp=log(abs(F_u_v));
subplot(1,2,2);
imshow(temp,[]);
title('Fourier Spectra');

%% Idle Lowpass filter


% Creating Transfer function
[M,N]=size(im);
% Set up range of variables.
u = 0:(M - 1);
v = 0:(N - 1);

% Compute the indices for use in meshgrid.


idx = find(u > M/2);
u(idx) = u(idx) - M;
idy = find(v > N/2);
v(idy) = v(idy) - N;

% Compute the meshgrid arrays.


[V, U] = meshgrid(v, u);

% Compute the distances D(U, V).


D0=30;
D = sqrt(U.^2 + V.^2);
H =ifftshift( double(D <=D0));

%Applying the transfer function


g=real(ifft2(H.*F_u_v));

%Crop to original size.


%g=g(1:size(F_u_v,1),1:size(F_u_v,2));
figure(2);
subplot(1,2,1);imshow(uint8(abs(g)));
title('Filtered Image');
subplot(1,2,2);imshow(H,[]);
title('Idle Lowpass filter');

%% Gaussian Filter
Hg = ifftshift(exp(-(D.^2)./(2*(D0^2))));

% Applying Gaussian Filter


g=real(ifft2(Hg.*F_u_v));
figure(3);
subplot(1,2,1);imshow(uint8(abs(g)));
title('Filtered Image');
subplot(1,2,2);imshow(Hg,[]);
title('Gaussian Lowpass filter');

%% Idle Highpass filter


D0=10;
H =ifftshift( double(D <=D0));
Hp=1-H;

% Applying Highpass filter


g=real(ifft2(Hp.*F_u_v));
figure(4);
subplot(1,2,1);imshow(uint8(abs(g)));
title('Filtered Image');
subplot(1,2,2);imshow(Hp,[]);
title('Idle Highpass filter');

%% Gaussian Highpass filter


D0=10;
H= ifftshift(exp(-(D.^2)./(2*(D0^2))));
Hgh=1-H;
g=real(ifft2(Hgh.*F_u_v));
figure(5);
subplot(1,2,1);imshow(uint8(abs(g)));
title('Filtered Image');
subplot(1,2,2);imshow(Hgh,[]);
title('Gaussian Highpass filter');
10.Conversion between color space

i=imread(‘c2.jpg’);
i2=rgb2ycbcr(i)
i3=rgb2ntsc(i);
i4=rgb2hsv(i);

subplot(2,2,1), imshow(i), title(‘Original image’);


subplot(2,2,1), imshow(i2), title(‘Ycbcr image’);
subplot(2,2,1), imshow(i3), title(‘NTSC image’);
subplot(2,2,1), imshow(i2), title(‘HSV image’);
11. Implementation of Discrete Wavelet Transform

2D Discrete Wavelet
load woman
imagesc(X)
colormap(map)
[cA,cH,cV,cD] = dwt2(X,'sym4','mode','per');
imagesc(cV)
title('Vertical Detail Coefficients')
imagesc(cA)
title('Approximation Coefficients')
2D Discrete Wavelet using Filters
load sculpture
imagesc(X)
colormap gray
[LoD,HiD] = wfilters('haar','d');
[cA,cH,cV,cD] = dwt2(X,LoD,HiD,'mode','symh');
subplot(2,2,1)
imagesc(cA)
colormap gray
title('Approximation')
subplot(2,2,2)
imagesc(cH)
colormap gray
title('Horizontal')
subplot(2,2,3)
imagesc(cV)
colormap gray
title('Vertical')
subplot(2,2,4)
imagesc(cD)
colormap gray
title('Diagonal')
12.Canny edge detection Algorithm

i= imread('cancercell.jpg');
g=rgb2gray(i);
subplot(2,2,1); imshow(i); title('Original Image');
subplot(2,2,2); imshow(g); title('Gray Image');
c=edge(g,'canny');
subplot(2,2,3); imshow(c); title('Canny output');

You might also like