You are on page 1of 20

Experiment -02

Aim: To design and simulate FIR digital filter (LP/HP)


Software Required:- Matlab 7.0.1 version
Theory:- Finite Impulse Response:
A finite impulse response (FIR) filter is a filter structure that can
be used to implement almost any short of frequency response
digitally. An FIR filter is usually implemented by using a series of
delays, multipliers and adders to create the filters output.
Figure shows the basic block diagram for an FIR filter of length N.
The delay result in operating on prior input samples. The h k values
are the coefficients used for multiplication, so that the output at
time n is the simulation of all the delayed samples multiplied by
the appropriate coefficients.

Figure: The logical structure of an FIR filter


The process of selecting the filters length and coefficient is called
filter design. The goal is to set those parameters such that certain
desired stopband and passband parameters will result from
running the filter.
The longer the filter (more taps), the more finely the response can
be tuned.
The MATLAB program is used to design the filter.

PROGRAM:
i)

FIR low pass filter


Clc;
Clear all;
Close all;
B=fir1(50,0.48,low);
Freqz(b,1.512);

ii)

FIR high pass filter


Clc;
Clear all;
Close all;
B=fir1(50,0.48,high);
Freqz(b,1.512);

OUTPUT GRAPH:

Experiment -03

Aim: To design and simulate IIR digital filter (LP/HP)


Software Required:- Matlab 7.0.1 version
Theory:IIR filterd, the transfer is a ratio of polynomials. The numerator of the transfer function . When
ths expression falls to zero , the value of the transfer function is zero as well called of the
function. The denominator of the transfer function .When this expression goes to zero(division
by zero) . The value of the transfer function tends to infinity;called a pole of th function or filter.

PROGRAM:
% IIR filters LPF & HPF
clc;
clear all;
close all;
disp('enter the IIR digital filter design specifications');
rp=input('enter the passband ripple');
rs=input('enter the stopband ripple');
wp=input('enter the passband frequency');
ws=input('enter the stopband frequency');
fs=input('enter the sampling frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,rp,rs,'s');
c=input('enter the choice of filter 1.LPF, 2.HPF\n');
if(c==1)
disp('frequency response of IIR LPF');
[b,a]=butter(n,wn,'s');

end;
if(c==2)
disp('frequency response of IIR HPF');
[b,a]= butter(n,wn,'s');
end;
w=0:0.1:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure, subplot(2,1,1);
plot(om/pi,m);
title('magnitude response of IIR Filter:');
xlabel('Normalized frequency--->');
ylabel('Gain in dB--->');
subplot(2,1,2);
plot(om/pi,an);
title('phase response of IIr Filter:');
xlabel('Normalized frequency--->');
ylabel('Phase in radian--->');

Result: We have designed IIR low pass filter and high pass filter.

Experiment -04
Aim: Reading and displaying Gray/Colour images of different formats.

Software Required:- Matlab 7.0.1 version


Theory:Image formats supported by Matlab

The following image formats are supported by Matlab:

BMP

HDF

JPEG

PCX

TIFF

XWB

Most images you find on the Internet are JPEG-images which is the name for one of the most
widely used compression standards for images. If you have stored an image you can usually see
from the suffix what format it is stored in. For example, an image named myimage.jpg is stored
in the JPEG format and we will see later on that we can load an image of this format into Matlab.

Working formats in Matlab


If an image is stored as a JPEG-image on your disc we first read it into Matlab. However, in
order to start working with an image, for example perform a wavelet transform on the image, we
must convert it into a different format. This section explains four common formats.

Intensity image (gray scale image)


This is the equivalent to a "gray scale image" and this is the image we will mostly work with in
this course. It represents an image as a matrix where every element has a value corresponding to
how bright/dark the pixel at the corresponding position should be colored. There are two ways to
represent the number that represents the brightness of the pixel: The double class (or data type).
This assigns a floating number ("a number with decimals") between 0 and 1 to each pixel. The
value 0 corresponds to black and the value 1 corresponds to white. The other class is called

which assigns an integer between 0 and 255 to represent the brightness of a pixel. The
value 0 corresponds to black and 255 to white. The class uint8 only requires roughly 1/8 of the
storage compared to the class double. On the other hand, many mathematical functions can only
be applied to the double class. We will see later how to convert between double and uint8.
uint8

Binary image
This image format also stores an image as a matrix but can only color a pixel black or white (and
nothing in between). It assigns a 0 for black and a 1 for white.

Indexed image
This is a practical way of representing color images. (In this course we will mostly work with
gray scale images but once you have learned how to work with a gray scale image you will also
know the principle how to work with color images.) An indexed image stores an image as two
matrices. The first matrix has the same size as the image and one number for each pixel. The
second matrix is called the color map and its size may be different from the image. The numbers
in the first matrix is an instruction of what number to use in the color map matrix.

RGB image
This is another format for color images. It represents an image with three matrices of sizes
matching the image format. Each matrix corresponds to one of the colors red, green or blue and
gives an instruction of how much of each of these colors a certain pixel should use.

Multiframe image
In some applications we want to study a sequence of images. This is very common in biological
and medical imaging where you might study a sequence of slices of a cell. For these cases, the
multiframe format is a convenient way of working with a sequence of images. In case you
choose to work with biological imaging later on in this course, you may use this format.

How to read files

When you encounter an image you want to work with, it is usually in form of a file (for example,
if you down load an image from the web, it is usually stored as a JPEG-file). Once we are done
processing an image, we may want to write it back to a JPEG-file so that we can, for example,
post the processed image on the web. This is done using the imread and imwrite commands.
These commands require the Image processing tool box!

Reading and writing image files:

Operation:
Read an image.
(Within the parenthesis you type the name of the image file you wish to
read.
Put the file name within single quotes ' '.)
Displaying an image
(Within the parenthesis you type the name of the image file you wish to
display. )

Matlab
command:
imread()

Imshow( )

PROGRAM: For reading and writing the images


clc;
clear all;
I=imread('D:\flower.jpg');
J=imread('D:\flower1.png');
K=imread('D:\flower2.tif');
I1=rgb2gray(I);
figure,imshow(I);figure,imshow(I1);figure,imshow(J);figure,imshow(K);

OutPut Of the Program:

Experiment -05
Aim: Image Histogram and Histogram Equalization.
Software Required:- Matlab 7.0.1 version
Theory:-

Image Histogram:

It is a graphical representation of the intensity distribution of an image.


It quantifies the number of pixels for each intensity value considered.

Histogram Equalization:

It is a method that improves the contrast in an image, in order to stretch out


the intensity range.

To make it clearer, from the image above, you can see that the pixels seem
clustered around the middle of the available range of intensities. What Histogram
Equalization does is to stretch out this range. Take a look at the figure below: The
green circles indicate the underpopulated intensities. After applying the
equalization, we get an histogram like the figure in the center. The intensities. After
applying the equalization, we get an histogram like the figure in the center.
Histogram equalization is a technique for adjusting image
intensities to enhance contrast. Let f be a given image
represented as a mr by mc matrix of integer pixel intensities
ranging. from 0 to L 1. L is the number of possible intensity
values, often 256.

Coding for Histogram equalization:


I = imread('d:\flower.jpg');
I1=rgb2gray(I);
J = histeq(I1);
subplot(2,2,1);
imshow( I1 );
subplot(2,2,2);
imhist(I1);
subplot(2,2,3);
imshow( J );
subplot(2,2,4);
imhist(J);

On the image there are three objects: a jumping man, the blue sky and the white snow. Suppose,
that we want to segment the jumping man, so mark all the pixels belonging to the desired object.
This is the basic goal of all the image segmentation tasks.
If we look at the image, we can see, that the easiest way to segment the man is using the color
information. The sky is definitely blue, although it has a considerable vertical gradient from dark
to light-blue. The snow is white, having some gray shadow on it. The man has some different
colors, except blue. So, if we identify and remove the blue pixels, there are two big objects
remaining: the upper will be the jumping man.
If you are not familiar with handling RGB images, please have a look at section Usual indexing
into 3d matrices of this matrix indexing tutorial.
Open the image and visualize the three color channels. In addition we put them into three
different variables:
image = imread('jump.jpg');

% read image

% get image dimensions: an RGB image has three planes


% reshaping puts the RGB layers next to each other generating
% a two dimensional grayscale image
[height, width, planes] = size(image);
rgb = reshape(image, height, width * planes);
imagesc(rgb);
colorbar on

% visualize RGB planes


% display colorbar

r = image(:, :, 1);
g = image(:, :, 2);
b = image(:, :, 3);

% red channel
% green channel
% blue channel

The result is:

On the blue channel the sky is brighter, the man is darker, so out first idea could be to cut the
blue channel at a given threshold. All pixels under the threshold may belong to the objects:
threshold = 100;
imagesc(b < threshold);

% threshold value
% display the binarized image

Setting the threshold at 100, 110 and 120 gives us the following three results respectively:

At low thresholds we loose several pixels of the man. As increasing the threshold value, more
and more sky-pixels fall under it, so the sky and the man can not be segmented any more. The
problem is, that a pixel having high blue component is not necessarily blue. You can see some
colors and their components below

(255,0,0)(0,255,0)(0,0,255)(127,127,255)(255,0,255) (0,255,255)

The last two colors have high blue components, but in fact they are not blue, since the other
components are high, too.
Here comes the idea: actually we want not the pixels having high blue component, but we
want blue pixels. If you look carefully the figure above, you can see, that a pixel is blue really, if
the blue component is high and the others are low. Here is a simple equation for identifying the
blueness of a pixel:

b=Bmax(R,G)
See the result for the colors above:

255,0,0)(0,255,0)(0,0,255)(127,127,255)(255,0,255)
(0,255,255)25525525512800
This approach seems much better. Lets turn it to MATLAB code:
% apply the blueness calculation
blueness = double(b) - max(double(r), double(g));
imagesc(blueness);
colorbar on

% visualize RGB planes


% display colorbar

The result below seems much better. We have only to choose a proper threshold to segment the
blue pixels. Now 45 is chosen:
% apply thresholding to segment the foreground
mask = blueness < 45;
imagesc(mask);

There are some objects remaining in the image, for example the snow and some noise. To
remove them, we calculate a so-called label image, where each pixel belonging to the same
object has the same value. We have an a-priori knowledge, that the pixel at (200, 200) belongs to
the jumping man. We read the value of that pixel: all pixels having the same label belong to the
man.
% create a label image, where all pixels having the same value
% belong to the same object, example
% 1 1 0 1 1 0
1 1 0 2 2 0
% 0 1 0 0 0 0
0 1 0 0 0 0
% 0 0 0 1 1 0 -> 0 0 0 3 3 0
% 0 0 1 1 1 0
0 0 3 3 3 0
% 1 0 0 0 1 0
4 0 0 0 3 0
labels = bwlabel(mask);
% get the label at point (200, 200)
id = labels(200, 200);
% get the mask containing only the desired object
man = (labels == id);
imagesc(man);

% save the image in PPM (portable pixel map) format


imwrite(man, 'man.ppm');

If we want to use the resulting mask for example for designing a logo, we may convert it to
vectorgraphic format by using a great tool, called potrace. The input of potrace is a bitmap, the
output is a vectorgraphical file.
potrace man.ppm -i -c -s -o man.svg

Option i inverts the image, c generates a textual output instead of a compressed one, s defines the
SVG backend, while o is for giving the output file name. The original PPM image is jagged, but
the final, vectorized object looks smooth and great:

The technique above is similar chroma keying, which is widely used for removing a singlecolored background. Because we know the background color, we can easily remove it, cut the
foreground, and use a different image as background.

You might also like