Digital Image Processing Lab File ECS-752: Shree Bankey Bihari Institute of Technology (SB-BIT) - Meerut

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 39

DIGITAL IMAGE PROCESSING

LAB FILE
ECS-752

Prepared
By
Jay Kant Pratap RAJESH KUMAR

Yadav

Shree Bankey Bihari Institute of Technology (SB-BIT) -


Meerut
Digital Image Processing Lab

Digital Image Processing Lab Index

S. NAME OF THE PROGRAM DATE SIGN REMARK


No.
1 Implement image enhancement in spatial
domain
1.a Contrast manipulation
1.b Histogram equalization
1.c Threshold operation
1.d Spatial domain filtering
1.e Histogram of original & flipped image
2 Implement image enhancement in frequency
domain
2.a Image addition
2.b Image subtraction
2.c Blending of two images
3 Implement image segmentation in MATLAB
3.a Compute the edges in the image
4 Implement morphological image processing in
MATLAB
4.a Dilation & erosion
4.b Boundary detector
5 Implement colour image processing in MATLAB
5.a To extract red, green & blue component
5.b To remove the RGB plane
5.c Histogram Equalization of coloured image
5.d Perform the median filtering of colour image
5.f To separate an object from its background

Page 2
Digital Image Processing Lab

PROGRAM NO. 1

Implement Image Enhancement in Spatial Domain

1a) CONTRAST MANIPULATION

clc

clear all

close all

a=imread('onion.png')

b=a*0.4

c=a*2

subplot(1,3,1),imshow(a),title('Original Image')

subplot(1,3,2),imshow(b),title('Decrease in Contrast')

subplot(1,3,3),imshow(c),title('Increase in Contrast')

Page 3
OUTPUT :
1b) HISTOGRAM EQUALIZATION

clc

clear all

close all

a=imread('coins.png')

b=histeq(a)

subplot(2,2,1),imshow(a),title('Original Image')

subplot(2,2,2),imshow(b),title('after histogram equalization')

subplot(2,2,3),imhist(a),title('Original Image')

subplot(2,2,4),imhist(b),title('after histogram equalization')


OUTPUT :
1c) THRESHOLD OPERATION

clc

clear all

close all

a=imread('coins.png')

[m,n]=size(a)

t=input('enter the value of threshold parameter')

for i=1:m

for j=1:n

if a(i,j)<t

b(i,j)=0;

else

b(i,j)=255;

end

end

end

figure,imshow(a),title('Original image'),pixval on

figure,imshow(b),title('threshold image'),pixval on
OUTPUT :
1d) SPATIAL DOMAIN FILTERING

clc

clear all

close all

a=imread('coins.png')

h1=1/9*ones(3,3)

h2=1/25*ones(5,5)

b1=conv2(a,h1);

b2=conv2(a,h2);

imshow(a),title('original image')

imshow(uint8(b1)),title('output using 3*3 mask')

imshow(uint8(b2)),title('output using 5*5 mask')


OUTPUT :
1e) HISTOGRAM OF ORIGINAL AND FLIPPED IMAGE

clc

clear all

close all

a=imread('coins.png')

b=fliplr(a)

subplot(2,2,1),imshow(a),title('original image')

subplot(2,2,2),imshow(b),title('flipped image')

subplot(2,2,3),imhist(a),title('original image')

subplot(2,2,4),imhist(b),title('flipped image')
OUTPUT :
PROGRAM NO. 2

Implement Image Enhancement in Frequency Domain.

2a) IMAGE ADDITION


clc

clear all

close all

a=imread('circles.png')

b=imread('cameraman.tif')

c=double(a)+double(b)

subplot(2,2,1),imshow(a),title('Addition of two image')

subplot(2,2,2),imshow(b),title('Addition of two image')

subplot(2,2,3),imshow(uint8(c)),title('Addition of two image')


OUTPUT :
2b) Image Subtraction:

clc

clear all

close all

a=imread('circles.png')

b=imread('cameraman.tif')

c=double(a)-double(b)

subplot(2,2,1),imshow(a),title('substraction of two image')

subplot(2,2,2),imshow(b),title('substraction of two image')

subplot(2,2,3),imshow(uint8(c)),title('substraction of two image')


OUTPUT :
2c) BLENDING OF TWO IMAGES

clc

clear all

close all

a=imread('cameraman.tif');
b=imread('coins.png');
b=imresize(b,[256 256]);

[m n]=size(a);

alpha1=input('enter the value of alpha :');


alpha2=input('enter the value of alpha :');
alpha3=input('enter the value of alpha :');
alpha4=input('enter the value of alpha :');

for i=1:m
for j=1:n
c1(i,j)=(1-alpha1)*a(i,j)+alpha1*b(i,j);
c2(i,j)=(1-alpha2)*a(i,j)+alpha2*b(i,j);
c3(i,j)=(1-alpha3)*a(i,j)+alpha3*b(i,j);
c4(i,j)=(1-alpha4)*a(i,j)+alpha4*b(i,j);
end
end

subplot(2,2,1),imshow(c1),title('blended image')
xlabel(sprintf('alpha value is %g',alpha1))

subplot(2,2,2),imshow(c2),title('blended image')
xlabel(sprintf('alpha value is %g',alpha2))

subplot(2,2,3),imshow(c3),title('blended image')
xlabel(sprintf('alpha value is %g',alpha3))

subplot(2,2,4),imshow(c4),title('blended image')
xlabel(sprintf('alpha value is %g',alpha4))
OUTPUT :
PROGRAM NO. 3

Implement Image Segmentation in MAT Lab.

COMPUTE THE EDGES IN THE IMAGE.

clc

clear all

close all

a=imread('onion.png')

a=rgb2gray(a)

b=edge(a,'roberts');

c=edge(a,'sobel');

d=edge(a,'prewitt');

e=edge(a,'log');

f=edge(a,'canny');

figure,imshow(a),title('original image')

figure,imshow(b),title('robert')

figure,imshow(c),title('sobel')

figure,imshow(d),title('prewitt')

figure,imshow(e),title('log')

figure,imshow(f),title('canny')
OUTPUT :
PROGRAM NO. 4

Implement morphological image processing in MAT Lab.

4a) DILATION AND EROSION

clc

clear all

close all

a=imread('text.png')

b=[1 1 1;1 1 1;1 1 1]

a1=imdilate(a,b)

a2=imerode(a,b)

figure,imshow(a),title('original image')

figure,imshow(a1),title('dilute image')

figure,imshow(a2),title('eroded image')
OUTPUT :
4b) BOUNDARY DETECTOR

clc

clear all

a=imread(‘text.png’)

b=[0 1 0;1 1 1;0 1 0]

a1=imdilate(a,b)

a2=imerode(a,b)

a3=a1-a2

a4=a1-a

a5=a1-a2

figure, imshow(a), title(‘Original Image’)

figure, imshow(a1), title(‘Dilated Image’)

figure, imshow(a2), title(‘Eroded Image’)

figure, imshow(a3), title(‘First Property’)

figure, imshow(a4), title(‘Second Property’)

figure, imshow(a5), title(‘Third Property’)

OUTPUT :
PROGRAM NO. 5
Implement Colour Image Processing in MAT Lab.

5a) TO EXTRACT RED, GREEN, AND BLUE COMPONENT

clc

clear all

close all

rgb=imread('onion.png')

r=rgb

g=rgb

b=rgb

r( : , : , 2 )=0

r( : , : , 2 )=0

b( : , : , 1 )=0

b( : , : , 3 )=0

g( : , : , 1 )=0

g( : , : , 2 )=0

imshow(rgb),title)'original image')

figure,imshow(r),title('red component')

figure,imshow(g),title('green component')

figure,imshow(b),title('blue component')
OUTPUT :
5b) TO REMOVE THE RGB PLANE

clc

clear all

close all

a=imread('onion.png')

a1=a

b1=a

c1=a

a1(:,:,1)=0

b1(:,:,2)=0

c1(:,:,3)=0

imshow(a),title('original image')

figure,imshow(a1),title('gb sector')

figure,imshow(b1),title('br sector')

figure,imshow(c1),title('rg sector')
OUTPUT :
5c) HISTOGRAM EQUALIZATION OF COLOURED IMAGE

clc

clear all

a=imread('onion.png')

b=rgb2ntsc(a);

b(: , : , 1)=histeq(b(: , : , 1))

c=ntsc2rgb(b);

imshow(a),title('original image')

figure,imshow(c),title('Histogram equalized image')


OUTPUT :

Original Image

Histogr

am equalized image
5d) PERFORM THE MEDIAN FILTERING OF COLOUR
IMAGE

clc

clear all

close all

a=imread('onion.png')

b=imnoise(a,'salt & pepper',0.2)

c( : , : , 1 )=medfilt2(b( : , : , 1 ))

c( : , : , 2 )=medfilt2(b( : , : , 2 ))

c( : , : , 3 )=medfilt2(b(: , : , 3 ))

imshow(a),title('original image')

figure,imshow(b),title('corrupted image')

figure,imshow(c),title('median filtered image')


OUTPUT :
5e) TO SEPARATE AN OBJECT FROM ITS BACKGROUND

clc

clear all

a=imread(‘onion.png’)

b=rgb2ycbr(a)

mask=b( : , : , 2)>120

imshow(a),title(‘Original image’)

figure,imshow(mask),title(‘Segmented Image’)
OUTPUT :

Original Image

Segmented image

You might also like