Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

EXPERIMENT NO-1

OBJECTIVE: To perform thresholding operation on a gray scale image.

HARDWARE/SOFTWARE REQUIRED: MATLAB

THEORY: Thresholding is a widely used tool in image segmentation where one is interested in
identifying the different homogeneous components of the image it is useful in discriminating
objects from background in many scenes, such as organs in scinti- graphic studies, targets in
forward looking infrared (FLIR) studies. Edges can be detected as the boundaries of the different
segments. Thresholding, however, is of limited value for textured images, i.e., inhomogeneous
components. There are two kinds of thresholding: bi-level and multilevel. In the former, one
assumes that the image is composed of two components, foreground and background that have
distinctively different gray-level distribution. One then attempts to find the threshold value
between the two groups. Each pixel of gray level that is above the threshold is assigned to the
foreground(background) and that below is assigned to the background (foreground). In the
multilevel, it is assumed that there are several components (segments) in the image, each of a
homogeneous value of gray level. One then attempts to locate the values of the thresholds that
can separate the components. Obviously, the multilevel situation is an extension of the bi-level
one.

PROGRAM
% MATLAB CODE FOR THRESHOLDING

clc
clear all
close all
I=imread('JellyFish.jpg');
I_gray=rgb2gray(I);
[r,c]=size(I_gray);
I_temp=zeros(r,c);
for i=1:r
for j=1:c
if I_gray(i,j)>=75
I_temp(i,j)=255;
else
I_temp(i,j)=0;
end
end
end
subplot(1,2,1),imshow(I_gray),title('Original image')
subplot(1,2,2), imshow(I_temp);title('Final image after thresholding')
OUTPUT:
RESULT: Thresholding operation on grayscale image is successfully performed Using MATLAB.
EXPERIMENT NO-2

OBJECTIVE: To determine and plot the bit planes of gray scale image

HARDWARE/SOFTWARE REQUIRED: MATLAB

THEORY: Gray scale digital images can be thought of as a matrix of pixels with intensities
(values). In an 8-bit image, these values range from 0 to 255. The bit planes of an 8-bit gray scale
image can be thought of as the stack of 8 binary images, containing only white or black pixels. Each
pixels value is determined based upon the binary representation of its intensity. The first bit plane
contains a set of pixels whose least significant bits are on, and the 8th bit plane contains the set of
pixels whose most significant bits are on.

Instead of highlighting gray level images, highlighting the contribution made to total image
appearance by specific bits might be desired. Suppose that each pixel in an image is represented by
8 bits. Imagine the image is composed of 8, 1-bit planes ranging from bit plane1-0 (LSB) to bit
plane 7 (MSB).
In terms of 8-bits bytes, plane 0 contains all lowest order bits in the bytes comprising the pixels in
the image and plane 7 contains all high order bits.

Separating a digital image into its bit planes is useful for analyzing the relative importance played
by each bit of the image, implying, it determines the adequacy of numbers of bits used to quantize
each pixel, useful for image compression.
In terms of bit-plane extraction for a 8-bit image, it is seen that binary image for bit plane 7 is
obtained by proceeding the input image with a thresholding gray-level transformation function that
maps all levels between 0 and 127 to one level (e.g 0)and maps all levels from 129 to 253 to another
(eg. 255).
PROGRAM
% MATLAB CODE FOR EXTRACTING BITPLANE

clc
clear all
i=imread('Penguins.jpg');
i=rgb2gray(i);
i=double(i);
a=mod(i,2);
subplot(3,3,1),imshow(a),title('LSB Bit Plane');

b=mod(floor(i/2),2);
subplot(3,3,2),imshow(b),title('2nd Bit Plane');

c=mod(floor(i/4),2);
subplot(3,3,3),imshow(c),title('3rd Bit Plane');

d=mod(floor(i/8),2);
subplot(3,3,4),imshow(d),title('4th Bit Plane');

e=mod(floor(i/16),2);
subplot(3,3,5),imshow(e),title('5th Bit Plane');

f=mod(floor(i/32),2);
subplot(3,3,6),imshow(f),title('6th Bit Plane');

g=mod(floor(i/64),2);
subplot(3,3,7),imshow(g),title('7th Bit Plane');

h=mod(floor(i/128),2);
subplot(3,3,8),imshow(h),title('MSB Bit Plane');

r=(2^7)*h+(2^6)*g+(2^5)*f+(2^4)*e+(2^3)*d+(2^2)*c+2*b+a;
subplot(3,3,9),imshow(uint8(r)),title('Reconstructed image');
OUTPUT:

RESULT: Bit Level Splicing of grayscale image is successfully achieved using MATLAB.
EXPERIMENT NO-3

OBJECTIVE: To generate histogram of a grayscale image..

HARDWARE/SOFTWARE REQUIRED: MATLAB

THEORY: The histogram of a digital image with gray levels in the range [0, L-1] is a discrete
function h(rk)=nk, where rk is the kth gray level and nk is the number of pixels in the image having
gray level rk. It is common practice to normalize a histogram by dividing each of its values by the
total number of pixels in the image, denoted by n. Thus, a normalized histogram is given by
p(rk)=nk/n, for k=0, 1,p ,L-1. Loosely speaking, p(rk) gives an estimate of the probability of
occurrence of gray level rk. Note that the sum of all components of a normalized histogram is equal
to 1.

Histograms are the basis for numerous spatial domain processing techniques. Histogram
manipulation can be used effectively for image enhancement. In addition to providing useful image
statistics, the information inherent in histogram is also quite useful in other image processing
applications, such as image compression and segmentation.

Consider a discrete gray scale image {x} and let ni be the number of occurrences of gray level i. The
probability of an occurrence of a pixel of level i in the image is

L being the total number of gray levels in the image (typically 256), n being the total number of
pixels in the image, and being in fact the image's histogram for pixel value i, normalized to
[0,1].
PROGRAM
% MATLAB CODE FOR HISTOGRAM EQUALISATION

clc
clear all
close all
I=imread('Penguins.jpg');
I=rgb2gray(I);
subplot(2,1,1), imshow(I),title('Grayscale image');
subplot(2,1,2), imhist(I),title('Histogram')
OUTPUT:

RESULT: Histogram generation of a grayscale image is successfully achieved using MATLAB.


EXPERIMENT NO-4

OBJECTIVE: To perform histogram equalization of gray scale images.

HARDWARE/SOFTWARE REQUIRED: MATLAB

THEORY: The histogram of a digital image with gray levels in the range [0, L-1] is a discrete
function h(rk)=nk, where rk is the kth gray level and nk is the number of pixels in the image having
gray level rk. It is common practice to normalize a histogram by dividing each of its values by the
total number of pixels in the image, denoted by n. Thus, a normalized histogram is given by
p(rk)=nk/n, for k=0, 1,p ,L-1. Loosely speaking, p(rk) gives an estimate of the probability of
occurrence of gray level rk. Note that the sum of all components of a normalized histogram is equal
to 1.

Histograms are the basis for numerous spatial domain processing techniques. Histogram
manipulation can be used effectively for image enhancement. In addition to providing useful image
statistics, the information inherent in histogram is also quite useful in other image processing
applications, such as image compression and segmentation.

Consider a discrete gray scale image {x} and let ni be the number of occurrences of gray level i. The
probability of an occurrence of a pixel of level i in the image is

L being the total number of gray levels in the image (typically 256), n being the total number of
pixels in the image, and being in fact the image's histogram for pixel value i, normalized to
[0,1].
Let us also define the cumulative distribution function corresponding to px as

,
which is also the image's accumulated normalized histogram.
We would like to create a transformation of the form y = T(x) to produce a new image {y}, with a
flat histogram. Such an image would have a linearized cumulative distribution function (CDF)
across the value range, i.e.

for some constant K. The properties of the CDF allow us to perform such a transform, it is defined
as

Where k is in the range [0,L). Notice that T maps the levels into the range [0,1] , since we used a
normalized histogram of {x}. In order to map the values back into their original range, the
following simple transformation needs to be applied on the result:
PROGRAM
% MATLAB CODE FOR HISTOGRAM EQUALISATION

clc
clear all
close all
I=imread('Jellyfish.jpg');
I=rgb2gray(I);
[r,c]=size(I);
No_of_pixels=r*c;
subplot(2,2,1), imshow(I),title('Original image');
subplot(2,2,2), imhist(I),title('Original histogram')
H_Eq=uint8(zeros(r,c));
%H_Eq=zeros(r,c);
freq=zeros(256,1);
p_f=zeros(256,1);
p_c=zeros(256,1);
cum=zeros(256,1);
output=zeros(256,1);

for i=1:r
for j=1:c
value=I(i,j);
freq(value+1)=freq(value+1)+1;
p_f(value+1)=freq(value+1)/No_of_pixels;
end
end

sum=0;
for i=1:size(freq,1)
sum=sum+freq(i);
cum(i)=sum;
p_c(i)=cum(i)/No_of_pixels;
output(i)=round(p_c(i)*256);
end

for i=1:r
for j=1:c
H_Eq(i,j)=output(I(i,j)+1);
% display('hello');
end
end

subplot(2,2,3), imhist(H_Eq),title('Histogram Equalisation');


subplot(2,2,4), imshow(H_Eq),title('Equalised image')
OUTPUT:

RESULT: Histogram Equalisation of grayscale image is successfully achieved using MATLAB.


EXPERIMENT NO-5

OBJECTIVE:. To perform the effect of various noise model on gray scale image like Impulse,
Gaussian, salt and peppernoise.

HARDWARE/SOFTWARE REQUIRED: MATLAB

THEORY: Noise represents unwanted information which deteriorates image quality. Noise is
defined as a process (n) which affects the acquired image (f) and is not part of the scene (initial
signal s). Using the additive noise model, this process can be written as: f(i,j) = s(i,j) + n(i,j)

Digital image noise may come from various sources. The acquisition process for digital images
converts optical signals in to electrical signals and then into digital signals and is one processes by
which the noise is introduced in digital images. Each step in the conversion process experience
fluctuations, caused by natural phenomena, and each of these steps adds a random value to the
resulting intensity of a given pixel.

NOISE MODELING

Noise (n) may be modeled either by a histogram or a probability density function which is
superimposed on the probability density function of the original image (s). In the following, the
models for the most common types of noise will be presented: salt and pepper noise and Gaussian
noise.

THE SALT & PEPPER NOISE: In the salt & pepper noise model only two possible values are
possible, a and b, and the probability of obtaining each of them is less then 0.1 (otherwise, the noise
would vastly dominate the image). For an 8 bit/pixel image, the typical intensity value for pepper
noise is close to 0 and for salt noise is close to 255.

Fig.1 Probability density function for the salt & pepper noise model.

The salt & pepper noise is generally caused by malfunctioning cameras sensor cells, by memory
cell failure or by synchronization errors in the image digitizing or transmission.

GAUSSIAN NOISE : The Gaussian noise has a normal (Gaussian) probability density function.
Gaussian noise is useful for modeling natural processes which introduce noise (e.g. noise caused by
the discrete nature of radiation and the conversion of the optical signal into an electrical one
(detector/shot noise), the electrical noise during acquisition (sensor electrical signal amplification).
Fig.2 Probability density function for the gaussian noise model.

PROGRAM
% MATLAB CODE FOR SALT AND PEPPER NOISE MODEL
clc
clear all
I1=imread('Penguins.jpg');
I=rgb2gray(I1);
[m,n]=size(I);
rq=[0 200];
t=randint(m,n,rq);
I(t<=5)=0;
I(t>=200)=255;
subplot(2,1,1),imshow(I),title('Noisy Image')
I2=medfilt2(I);
subplot(2,1,2), imshow(I2),title('Filtered image')

PROGRAM
% MATLAB CODE FOR GAUSSIAN NOISE
clc
clear all
I1=imread('Jellyfish.jpg');
I=rgb2gray(I1);
[m,n]=size(I);
mean=0;
var=0.5;
subplot(1,3,1),imshow(I),title('Original Image')
t=rand(m,n);
q=var.*t;
s=q+double(I)/256;
subplot(1,3,2),imshow(s),title('Noisy image')
h=[1000 1000];
w=fspecial('gaussian',h,0.5);
k=imfilter(s,w);
subplot(1,3,3), imshow(k),title('Filtered image')
OUTPUT:

1. Salt and pepper noise

2. Gaussian Noise

RESULT: Removal of salt-pepper and Gaussian noise in a grayscale image is successfully


performed using MATLAB.
EXPERIMENT NO- 6

OBJECTIVE: Demonstrate Canny Edge Detection technique.

HARDWARE/SOFTWARE REQUIRED: MATLAB

THEORY:
Edge Detection:
Edges in an image are those points which show sudden change of intensity and with help of
derivatives (1st order, 2nd order) we can find this change in an image.

Canny Edge Detector:


Canny edge detector is a complex but accurate detector as compared to Marr-Hilderth detector.
And it is based on these 3 following objectives:

Low Error Rate: all edges should be found and these detected edges should be as much close to
true edges as possible

Edge points should be well localized : The distance between a point detected or marked as an edge
by canny detector and the centre of the true edge should be minimum.

3. Single Edge Point Response: The detector must output one edge point for each true edge point.

Algorithm:
Step 1: f(x,y) = input image and G(x,y) = Gaussian function: G(x,y)=e^-(x^2 + y^2/2 a^2) Form a
smoothed image fs(x,y) by fs(x,y) = G(x,y) * f(x,y)

Step 2: Compute the gradient magnitude and direction(angle): M(x,y) =gx^2 + gy^2 and
a(x,y)=arctan(gy/gx) where gx = fs/x and gy=fs/y

Step 3: The gradient M(x,y) typically contains wide range around local \maxima. Next step is to
thin those ridges.

Nonmaxima Suppression: Let d1,d2,d3, and d4 denote the four basic edge directions for a
33region: horizontal, -45degrees, vertical, +45degrees. Find the direction dk that is closest to
a(x,y). If the value of M(x,y) is less than at least one of its 2 neighbours along dk, gn(x,y)=0
Step 4: Final operation is to threshold gn(x,y) to reduce false edge points.

Hysteresis Thresholding: Mathematically its done like this: gnh(x,y) = gn(x,y)>=Th gnl(x,y) =
gn(x,y)>=Tl and
gnl(x,y) = gnl(x,y) gnh(x,y).

The Canny Edge Detection: Summary


Smooth the input image with a Gaussian filter
Compute the gradient magnitude and angle images
Apply non-maxima suppression to the gradient magnitude image
Use double thresholding and connectivity analysis to detect and link edges

PROGRAM
% MATLAB CODE FOR CANNY EDGE DETECTION

%CANNY EDGE DETECTOR

%CANNY EDGE DETECTOR

close all; clear all; clc;


im1=imread('Cameraman.tif');
%im1=rgb2gray(im1);

[x,y]=size(im1);
subplot(1,3,1);imshow(im1);title('Original Image')

%--------1. Gaussian filtering

g=fspecial('gaussian',[5 5],1.4); % 1.4 is the variance


G1=filter2(g,im1);

%--------2. Gradient

s = [-1 0 1; -2 0 2; -1 0 1]; % Sobel Mask in X-Direction


im_x = conv2(G1,s,'same');
im_y = conv2(G1,s','same'); im_s = sqrt(im_x.^2 + im_y.^2); im_s = im_s/255;

%--------3. Non Maxima Supression theta(x,y)= 0;

img3(x,y)= 0; theta2(x,y)= 0;
for r=2:x-1 for c=2:y-1
theta(r,c) = atand(im_y(r,c)/im_x(r,c));

if ((theta(r,c)>-22.5) && (theta(r,c)<=22.5) || (theta(r,c)>157.5) && (theta(r,c)<=-157.5))


theta2(r,c) = 0; end

if ((theta(r,c)>22.5) && (theta(r,c)<=67.5) || (theta(r,c)<=-112.5) && (theta(r,c)>-157.5))


theta2(r,c) = 45; end
if ((theta(r,c)>67.5) && (theta(r,c)<=112.5) || (theta(r,c)<=-67.5) && (theta(r,c)>-112.5))
theta2(r,c) = 90; end

if ((theta(r,c)>112.5) && (theta(r,c)<=157.5) || (theta(r,c)<=-22.5) && (theta(r,c)>-67.5))


theta2(r,c) = 135; end

if (theta2(r,c) == 0)
if (im_s(r, c)>im_s(r, c-1) && im_s(r, c)>im_s(r, c+1)) img3(r,c) = im_s(r,c);
else img3(r,c) = 0; end
end

if (theta2(r,c) == 45)
if (im_s(r, c) > im_s(r+1, c-1) && im_s(r,c)>im_s(r-1,c+1)) img3(r,c) = im_s(r,c);
else img3(r,c) = 0; end
end

if (theta2(r,c) == 90)
if (im_s(r, c) > im_s(r-1, c) && im_s(r, c) > im_s(r+1, c)) img3(r,c) = im_s(r,c);
else img3(r,c) = 0; end
end

if (theta2(r,c) == 135)
if (im_s(r,c) > im_s(r-1,c-1) && im_s(r,c) > im_s(r+1, c+1)) img3(r,c) = im_s(r,c);
else img3(r,c) = 0; end
end

end
end

%---------4. Double Thresholding


th= 0.2;
tl= 0.1; img4(r,c)= 0; for r = 2 : x-1

for c = 2 : y-1
if(img3(r,c) > th)
img4(r,c) = 1;
for i = -1:1:1
for j = -1:1:1
end
end
end
end
end

if(img3(r+i,c+j) >= tl && img3(r+i,c+j) < th ) img4(r+i,c+j) = 1;


end

subplot(1,3,2); imshow(img4);title('Canny Edge Detection');


im5=edge(im1,'canny',[0.1,0.2],1.4); %[0.1,0.2]=[tl,th] & 1.4=variance of Gaussian LPF
subplot(1,3,3); imshow(im5);title('Edge Detection By Direct Command');
OUTPUT:

RESULT: Canny edge detection is successfully performed using MATLAB.

.
EXPERIMENT NO- 7

OBJECTIVE: To determine frequency response of infinite impulse response filter.

HARDWARE/SOFTWARE REQUIRED: MATLAB

THEORY: The IIR filter can realize both the poles and zeroes of a system because it has a rational
transfer function, described by polynomials in z in both the numerator and the denominator m and
n are order of the two polynomials b and a are the filter coefficients.

IIR filters can be expanded as infinite impulse response filters.


PROGRAM
% MATLAB CODE FOR IIR FILTER

clc
clear all
close all
b=1;
a=[1 -1/2];
w=-pi:0.01:pi;
figure,freqz(b,a,w)

OUTPUT:

RESULT: Frequency response of an infinite impulse response filter is successfully achieved using
MATLAB.
EXPERIMENT NO- 8
OBJECTIVE: To determine frequency response of finite impulse response filter.

HARDWARE/SOFTWARE REQUIRED: MATLAB

THEORY: A Finite Impulse Response (FIR) filter is a discrete linear time-invariant system
whose output is based on the weighted summation of a finite number of past inputs. An FIR
transversal filter structure can be obtained directly from the equation for discrete-time convolution.

In this equation, x(k) and y(n) represent the input to and output from the filter at time n. h(n-k) is
the transversal filter coefficients at time n. FIR filter is a finite impulse response filter. This FIR
filter is an all zero filter.

PROGRAM
% MATLAB CODE FOR FIR FILTER
clc
clear all
close all
b=[1 2 3 4];
a=1;
w=-pi:0.01:pi;
figure,freqz(b,a,w)

OUTPUT:

RESULT: Frequency response of a finite impulse response filter is successfully achieved using
MATLAB.

You might also like