Escolar Documentos
Profissional Documentos
Cultura Documentos
Theory: In image processing, the first and foremast task is to import the image to platform where
processing has to be applied. In MATLAB, when the image is imported, it’s imported in form of
matrix. For example the imported image has matrix dimensions of 256x256x3 , here first value is
describing number of rows, second number of columns and 3 is describing it has three levels that
means the imported image is RGB image . The imported image matrix depicts the pixel’s intensity
level.
Description of inbuilt function:
1. imread: A=imread(filename,fmt) reads a grayscale or color image from the file specified
by the character vector or string scalar filename. Fmt specifies the format of the file by its
standard file extension .
2. imshow: imshow(A) will display the image.
3. rgb2gray: rgb2gray(A) converts RGB image or colormap to grayscale.
4. im2bw: im2bw(A,level) converts image to binary image by thresholding. The output
binary image has values of 1 (white) for all pixels in the input image with luminance
greater than level and 0 (black) for all other pixels.
5. imcrop: imcrop(A) displays the image I in a figure window and creates a cropping tool
associated with that image.
6. imresize: B = imresize(A, scale) returns an image that is scale times the size of A.
7. imsave: imsave is used to save the image.
Matlab Code:
%Clearing workspace and command window
clear all; clc;
%% To read an image
x = imread('pikachu1.jpg');
imshow(x);
Output:
Figure 1 Converting from RGB to Gray and black and white image.
2
801861003
Figure 2 Cropped image
3
801861003
Experiment-2
Aim: Perform the following operations using MATLAB.
a) Intensity enhancement of a gray image.
b) To quantize the gray image in four levels.
c) To divide the image in four unequal parts.
Matlab Code:
clc;
clear all;
Img=imread('pikachu.tif');
%% To enhance an image
Inew=Img+60;
figure;
imshow(Inew)
%% Quantizing an image
% The image is ceiled to upper value.
figure;
subplot(2,1,1);
imshow(Img);
I=imresize(Img,[256,256]);
quantized=I;
for i=1:256
for j=1:256
if I(i,j)<=64;
quantized(i,j)=64;
elseif I(i,j)<=128;
quantized(i,j)=128;
elseif I(i,j)<=192;
quantized(i,j)=192;
else I(i,j)<=256;
quantized(i,j)=256;
end
end
end
subplot(2,1,2)
imshow(quantized)
%% Segmentation
newImg= imresize(Img, [256 256]);
i1=newImg(1:128,1:256);
i2=newImg(129:256,1:128);
i3=newImg(129:192,129:256);
4
801861003
i4=newImg(193:256,129:256);
figure;
subplot(3,2,1)
imshow(newImg)
subplot(3,2,2)
imshow(i1)
subplot(3,2,3)
imshow(i2)
subplot(3,2,4)
imshow(i3)
subplot(3,2,5)
imshow(i4)
%concattining
i5=[i3;i4];
i6=[i2,i5];
i7=[i1;i6];
subplot(3,2,6)
imshow(i7)
Output:
5
801861003
Figure 6 Segmented Image
6
801861003
Experiment-3
Aim: To increase the contrast and brightness of an image using MATLAB.
Theory: To increase the contrast of image we multiply it with an integer and for increasing
brightness a constant value is added to the intensity levels. So we have to implement,
𝑔(𝑥 ) = 𝑎𝑓(𝑥 ) + 𝑏
Here a and b are constants . f(x) represents input image and g(x) represents output image.
Matlab Code:
clc;
clear all;
Img=imread('Charizard.png');
%% Increase contrast and brightness of Image
subplot(1,3,1)
imshow(Img);
title('original picture')
I1=2*Img;
subplot(1,3,2);
imshow(I1)
title('increased contrast')
I2=2*Img+50;
subplot(1,3,3);
imshow(I2)
title('increased contrast and brightness');
Output:
7
801861003
Experiment-4
Aim: To add different type of noises in an image.
Theory: There are many type of noises present in system which distorts the pixel’s intensity
value and information contained by that particular pixel is lost.
Description of inbuilt command:
imnoise: imnoise(I,TYPE,...) adds noise of a given TYPE to the intensity image I. TYPE
is a string or char vector that can have different values ex gaussian, poisson etc.
Matlab Code:
clc;
clear all;
Img=imread('pikachu.tif');
%% Adding noise
J1=imnoise(Img,'gaussian');
J2=imnoise(Img,'salt & pepper');
J3=imnoise(Img,'speckle');
J4=imnoise(Img,'poisson');
subplot(221);
imshow(J1);
title('gaussian noise');
subplot(222);
imshow(J2);
title('Salt & pepper noise');
subplot(223);
imshow(J3);
title('Speckle noise');
subplot(224);
imshow(J4);
title('poisson noise');
Output:
8
801861003
Experiment-5
Aim: To generate square wave using synthetic image.
Theory: A synthetic black and white image can be generated by using combinations of 0’s and
1’s. 0 level depicts black pixel and 1 depicts white level.
Description of in-built command:
improfile – improfile(I) computes the intensity values along a line or a multiline path in an
image I. It uses interpolation to find the intensity value for each point.
Matlab Code:
clc;
clear all;
%% Image synthesises
I1=zeros(10,50);
I2=ones(10,50);
I=[I1; I2; I1; I2;I1;I1;I2;I2;I1;I1];
figure;
imshow(I);
x=[25 25];
y=[0 100];
improfile(I,x,y);
%% Perfect square wave
I3=zeros(25,50);
I4=ones(25,50);
Inew=[I3;I4;I3;I4];
figure;
imshow(Inew);
improfile(Inew,x,y);
Output:
9
801861003
10
801861003
Experiment-6
Aim: To find an unknown object in an image.
Theory: To find an unknown object between two images , image subtraction may be performed
between two identical images. The pixels having similar intensities will yield zero and the pixels
whose values is different in two images will yield the object introduced/missing .
Matlab Code:
%% Finding an unknown object
P1=imread('MiniPresent.jpg');
P2=imread('Miniabsent.jpg');
Pg1=rgb2gray(P1);
Pg2=rgb2gray(P2);
P=Pg2-Pg1;
subplot(1,3,1)
imshow(Pg1)
title('first image');
subplot(1,3,2)
imshow(Pg2)
title('second image');
subplot(1,3,3)
imshow(P)
title('subtarcted image')
Output:
11
801861003
Experiment-7
Aim: To perform the following geometric transformations on an image
a) To translate an image.
b) To scale an image.
c) To shear an image.
d) To skew an image.
e) To rotate an image around
i. Y-axis
ii. X-axis
iii. Origin
iv. Line Y=X
v. Line X=Y
Theory: The geometric image on an image can be performed using element wise matrix
multiplication. For every transformation we have a pre-decided matrix whose values can be altered
to perform the required transformation. Some of the matrices are given below:
Translation Matrix Scaling Matrix Rotation Matrix
x′ 1 0 tx x x′ Cx 0 0 x x′ cos 𝜃 −sin θ t x x
′
[y ] = [0 1 t y ] [y ] ′
[y ] = [ 0 Cy 0] [ y ] ′
[y ] = [ sin θ cos 𝜃 t y ] [y ]
1 0 0 1 1 1 0 0 1 1 1 0 0 1 1
Vertical Shear Matrix Horizontal Shear Matrix Skew Matrix
x′ 1 S𝑣 0 x x′ 1 0 0 x x′ 1 0 0 x
[y ′ ] = [0 1 0] [ y ] [y ′ ] = [ S ℎ 1 0] [ y ] [y ′ ] = [tan ∅ 1 0] [ y ]
1 0 0 1 1 1 0 0 1 1 1 0 0 1 1
Matlab Code:
close all; clear all; clc;
x= imread('Charizard.png');
%% Translation
T=[1 0 30; 0 1 30; 0 0 1];
tform = maketform('affine',T');
J = imtransform(x,tform,'XData',[1 size(x,2)],'YData',[1 size(x,1)]);
12
801861003
figure;
subplot(121)
imshow(x);
title('Original image')
subplot(122)
imshow(J);
title('Translated image')
%% Scaling
S=[60 0 0; 0 60 0; 0 0 1];
tform = maketform('affine',S');
J1= imtransform(x,tform,'XYScale',1);
figure;
subplot(121)
imshow(x)
title('original image');
subplot(122)
imshow(J1)
title('scaled image');
%% Shearing
%in vertical
Sv=[1 0.8 0; 0 1 0; 0 0 1];
tform1 = maketform('affine',Sv');
J2= imtransform(x,tform1);
%in horizontal
Sh=[1 0 0; 0.8 1 0; 0 0 1];
tform1 = maketform('affine',Sh');
J3= imtransform(x,tform1);
figure;
subplot(131)
imshow(x)
title('original image');
subplot(132)
imshow(J2)
title('Sheared vertically');
subplot(133)
imshow(J3)
title('Sheared horizontally');
%% Skewing
S1=[1 0 0; 1 1 0; 0 0 1];
tform0 = maketform('affine',S1');
J6 = imtransform(x,tform0);
figure;
subplot(121);
imshow(x);
title('original image')
subplot(122)
imshow(J6);
title('Skewed image')
%% Rotation around
% y axis
R1=[-1 0 0; 0 1 0; 0 0 1];
tform1 = maketform('affine',R1');
M1= imtransform(x,tform1);
13
801861003
% x axis
R2=[1 0 0; 0 -1 0; 0 0 1];
tform2 = maketform('affine',R2');
M2= imtransform(x,tform2);
%origin
R3=[-1 0 0; 0 -1 0; 0 0 1];
tform3 = maketform('affine',R3');
M3= imtransform(x,tform3);
% y =x
R4=[0 1 0; 1 0 0; 0 0 1];
tform4 = maketform('affine',R4');
M4= imtransform(x,tform4);
% y =-x
R5=[0 -1 0;-1 0 0; 0 0 1];
tform5 = maketform('affine',R5');
M5= imtransform(x,tform5);
% Plotting
figure;
subplot(2,3,1)
imshow(x)
title('Original image');
subplot(232)
imshow(M1)
title('Around Y axis')
subplot(233)
imshow(M2)
title('Around X axis')
subplot(234)
imshow(M3)
title('Around origin')
subplot(235)
imshow(M4)
title('Around Y=X')
subplot(236)
imshow(M5)
title('Around Y=-X');
Output:
14
801861003
15
801861003
16
801861003
Experiment-8
Aim: a) To plot the histogram of an image.
b) To perform histogram equalization.
Theory: An image histogram is a chart that shows the distribution of intensities in an indexed or
grayscale image. Histogram equalization is a technique by which dynamic range of an image can
be increased. Histogram equalization assigns the intensity value of pixels in the input image such
that the output image contains a uniform distribution of intensity.
Description of in-built image:
imhist: imhist(I) displays a histogram for the intensity image I whose number of bins are
specified by the image type. If I is a grayscale image, imhist uses 256 bins as a default
value.
histeq: histeq enhances the contrast of images by transforming the values in an intensity
image, or the values in the colormap of an indexed image, so that the histogram of the
output image approximately matches a specified histogram.
Matlab Code:
clc;
clear all;
Img=imread('Charizard.png');
imshow(Img)
%% Plotting histogram
figure;
imhist(Img)
xlabel('gray scale vaues');
ylabel('no of pixels');
title('histogram of original image');
%% Histogram equalization
figure;
[B T]=histeq(Img);
imhist(B)
xlabel('gray scale vaues');
ylabel('no of pixels');
title('equalized histogram of original image');
figure;
imshow(B)
title('equalized image')
17
801861003
Output:
18
801861003
Experiment-9
Aim: To perform the following intensity transformations on the image
a) Log Transformation
b) Gamma Transformation
c) Image Negative
d) 𝑁 𝑡ℎ root
e) Contrast Stretching
Matlab Code:
%% Log transformation
clc;
clear all;
Img=imread('Charizard.png');
newIm=2*log(1+double(Img));
subplot(121)
imshow(Img)
title('original image')
subplot(122);
imshow(newIm);
title('Logarithmic transformation');
%% Image negative
figure;
I1=256-Img;
subplot(121)
imshow(Img)
title('original image')
subplot(122);
imshow(I1);
title('image negative');
19
801861003
%% Gamma Transformation
I2=Img.^2;
I3=Img.^3;
I4=Img.^4;
I5=Img.^5;
figure;
subplot(221)
imhist(I2);
title('Gamma=2');
subplot(222);
imhist(I3);
title('Gamma=3');
subplot(223);
imhist(I4);
title('Gamma=4');
subplot(224);
imhist(I5);
title('Gamma=5');
%% Nth root
Img1=double(Img)
I2=nthroot(Img1,3); %cube root
figure;
subplot(121)
imshow(Img)
title('Original Image');
subplot(122)
imshow(I2);
title('cuberoot of Image');
%% contrast strecting
I=imresize(Img,[256,256]);
for i=1:256
for j=1:256
if I(i,j)<=128;
In(i,j)=0;
else
In(i,j)=256;
end
end
end
figure;
subplot(121)
imshow(Img)
title('Original Image');
subplot(122)
imshow(In);
title('contrast strecting of image');
20
801861003
Output:
Logarithmic Transformation
Gamma Transformation
21
801861003
Gamma Transformation
Image Negative
22
801861003
23
801861003
Experiment-10
Aim: To perform the Fourier transformation on a given image.
Theory: The Fourier Transform is an important image processing tool which is used to
decompose an image into its sine and cosine components. In the Fourier domain image, each point
represents a particular frequency contained in the spatial domain image. The Fourier Transform is
used in a wide range of applications, such as image analysis, image filtering, image reconstruction
and image compression.
fft2: fft2 is two-dimensional discrete Fourier Transform. fft2(X) returns the two-
dimensional Fourier transform of matrix X. If X is a vector, the result will have the same
orientation. fft2(X,MROWS,NCOLS) pads matrix X with zeros to size MROWS-by-
NCOLS before transforming.
Matlab Code:
clc;
clear all;
I1=imread('Charizard1.png');
I=rgb2gray(I1);
In=imresize(I,[256 256]);
Id=im2double(In);
l=log(abs(fft2(Id)));
imshow(l)
Output:
24
801861003
Experiment-11
Aim: To find bit planes of an image and apply binary to gray coding
Theory: A multilevel image is decomposed into several bit plane to reduce the inter pixel
redundancy. The bits are decomposed and then gray coding is applied on them. The m-bit gray
code 𝑔𝑚−1 … 𝑔2 𝑔1 𝑔0 that corresponds to the polynomial can be computed from
𝑔𝑚−1 = 𝑎𝑚−1
Bitget: Get bit. C = bitget(A,BIT) returns the value of the bit at position BIT in A,
where A is a signed or unsigned integer array. BIT must be between 1 (least significant
bit) and the number of bits in the integer class of A
Matlab Code:
clc;
a=imread('Charizard.png');
[r c p]=size(a);
if (p==3)
error('Input image should be Grayscale')
else
[pl1,pl2,pl3,pl4,pl5,pl6,pl7,pl8]=bitplane_code(a);
end
figure;
subplot(3,3,1);
imshow(pl1);
title('1st plane');
subplot(3,3,2);
imshow(pl2);
title('2nd plane');
subplot(3,3,3);
imshow(pl3);
title('3rd plane');
subplot(3,3,4);
imshow(pl4);
title('4th plane');
subplot(3,3,5);
imshow(pl5);
title('5th plane');
subplot(3,3,6);
imshow(pl6);
title('6th plane');
subplot(3,3,7);
imshow(pl7);
25
801861003
title('7th plane');
subplot(3,3,8);
imshow(pl8);
title('8th plane')
rec=pl1+pl2*2+pl3*4+pl4*8+pl5*16+pl6*32+pl7*64+pl8*128;
subplot(3,3,9);
imshow(uint8(rec));
title('Original Image');
%%Function of Bitplane Slicing:
[row col]=size(img);
b=zeros(row,col,8);
for k=1:8
for i=1:row
for j=1:col
b(i,j,k)=bitget(img(i,j),k);
end
end
end
pl1=b(:,:,1);
pl2=b(:,:,2);
pl3=b(:,:,3);
pl4=b(:,:,4);
pl5=b(:,:,5);
pl6=b(:,:,6);
pl7=b(:,:,7);
pl8=b(:,:,8);
end
d1=pl1;
subplot(331)
imshow(logical(d1));
title('1st plane');
d2=xor(pl1,pl2);
subplot(332)
imshow(logical(d2));
title('2nd plane');
d3=xor(pl3,pl2);
subplot(333)
imshow(logical(d3));
title('3rd plane');
d4=xor(pl3,pl4);
subplot(334)
imshow(logical(d4));
title('4th plane');
d5=xor(pl4,pl5);
subplot(335)
imshow(logical(d5));
title('5th plane');
d6=xor(pl5,pl6);
26
801861003
subplot(336)
imshow(logical(d6));
title('6th plane');
d7=xor(pl6,pl7);
subplot(337)
imshow(logical(d7));
title('7th plane');
d8=xor(pl8,pl7);
subplot(338)
imshow(d8);
title('8th plane');
rec1=d1+d2*2+d3*4+d4*8+d5*16+d6*32+d7*64+d8*128;
subplot(339);
imshow(uint8(rec1));
title('Original Image');
Output:
Binary Bitplanes
27
801861003
Gray Bitplanes
28
801861003
Experiment-12
Theory: Huffman coding yields the smallest possible number of code symbols per source
symbols. Huffman code is applied by extracting the unique intensity values and then arranging
these symbols in descending order of probabilities. The last two probabilities are summed up and
entire process is repeated till we obtain unity. Symbols are given to each intensity level based on
their division. Each division yields a 0,1 pair.
Matlab Code:
clc;
clear all;
%Image
A=[ 21 21 21 95 169 243 243 243 ;
21 21 21 95 169 243 243 243 ;
21 21 21 95 169 243 243 243 ;
21 21 21 95 169 243 243 243 ];
[r c]= size(A); %getting rows and columns
n=r.*c;
B=unique(A); %find the unique elements
for j=1:1:4
y = sum(A == B(j)); %Compare each column of the matrix with the entries in
that column.
C(j)=sum(y);
p(j) = C(j)/n; % finding probabilities
end
dict = huffmandict(B,p’); %creating dictionary
C=[A(1,:), A(2,:),A(3,:),A(4,:)]; %changing the matrix into row vector
hcode = huffmanenco(C,dict); %Encoding the image using dictionary
dhsig = huffmandeco(hcode,dict);
29
801861003
Output
Dictionary
Encoded image
30
801861003
Experiment-13
Theory: Smoothing is often used to reduce noise within an image or to produce a less pixelated
image. Most smoothing methods are based on low pass filters. A kernel, convolution matrix,
or mask is a small matrix which is used for blurring, sharpening, embossing, edge detection, and
more. A pre-defined kernel and an image are convolved together to perform image smoothening.
Gaussian Kernel
Filter2: Two-dimensional digital filter. Y = filter2(B,X) filters the data in X with the 2-D
FIR filter in the matrix B. The result, Y, is computed using 2-D correlation and is the same
size as X.
Fspecial: fspecial Create predefined 2-D filters. H = fspecial(TYPE) creates a two-
dimensional filter H of the specified type.
Imgaussfilt: 2-D Gaussian filtering of images B = imgaussfilt(A) filters image A with a
2-D Gaussian smoothing kernel with standard deviation of 0.5. A can have any number of
dimensions.
Matlab Code:
clc;
clear all;
I2=imread('Charizard.png'); %reading image
I1=imresize(I2,[256 256]);
% I2=rgb2gray(I1);
subplot(121);
imshow(I1)
title('Original image');
K=[1 4 7 4 1; 4 16 26 16 4; 7 26 41 26 7; 4 16 26 16 4; 1 4 7 4 1];
R=(1/273)*K;
f=uint8(filter2(R,I2));
subplot(122);
imshow(f);
title('Smoothened image using gaussian kernel')
%% Box Kernel
31
801861003
figure
subplot(121);
imshow(I1);
title('Original image');
K2=ones(7,7);
R2=(1/49)*K2;
f1=uint8(filter2(R2,I2));
subplot(122);
imshow(f1);
title('Smoothened image using Box kernel');
%% Using inbuilt commands
figure;
subplot(121)
imshow(I1);
title('Original image');
h1=fspecial('gaussian',[5 5],1.5);
f=imfilter(I1,h1);
subplot(122);
imshow(f);
title('Smoothening using Gaussian Kernel(using inbuilt command)');
%%
M=imgaussfilt(I1,2);
figure;
subplot(121)
imshow(I1);
title('Original image');
subplot(122);
imshow(M);
title('Smoothening using Gaussian Kernel(using inbuilt command)');
Output
32
801861003
33
801861003
34
801861003
Experiment-14
Theory: Image sharpening refers to any enhancement technique that highlights edges and fine
details in an image . A kernel, convolution matrix, or mask is a small matrix which is used for
blurring, sharpening, embossing, edge detection, and more. A pre-defined kernel and an image are
convolved together to perform image sharpening. For edge detection following kernel can be used.
Filter2: Two-dimensional digital filter. Y = filter2(B,X) filters the data in X with the 2-D
FIR filter in the matrix B. The result, Y, is computed using 2-D correlation and is the same
size as X.
Fspecial: fspecial Create predefined 2-D filters. H = fspecial(TYPE) creates a two-
dimensional filter H of the specified type.
Imgaussfilt: 2-D Gaussian filtering of images B = imgaussfilt(A) filters image A with a
2-D Gaussian smoothing kernel with standard deviation of 0.5. A can have any number of
dimensions.
Matlab Code
clc;
clear all;
I2=imread('Charizard.png'); %reading image
I1=imresize(I2,[256 256]);
subplot(131);
imshow(I1);
title('original image')
L=[1 1 1; 1 -8 1; 1 1 1];
f=uint8(filter2(L,I2));
subplot(132);
imshow(f)
title('Edges of image')
r=imsubtract(I2,f);
subplot(133)
imshow(r)
title('Sharpened image')
Output
36
801861003