DSIP Manual

36
RAMRAO ADIK INSTITUTE OF TECHNOLOGY,NERUL COMPUTER ENGINEERING DEPARTMENT LAB MANUAL DIGITAL SIGNAL AND IMAGE PROCESSING SEMESTER : VII YEAR: 2010-11

Transcript of DSIP Manual

Page 1: DSIP Manual

RAMRAO ADIK INSTITUTE OF TECHNOLOGY,NERUL

COMPUTER ENGINEERING DEPARTMENT

LAB MANUAL

DIGITAL SIGNAL AND IMAGE PROCESSING

SEMESTER : VII YEAR: 2010-11

Page 2: DSIP Manual

RAMRAO ADIK INSTITUTE OF TECHNOLOGY, NERUL

List of Experiments

Name of Faculty: Leena Ragha /Ashwini Gudewar Term : July 2010 to Oct 2011

Subject: DSIP Lab Session : 2 hrs / week

Year & Semester : B.E Sem VII No. of Lectures per week : 4

SERIAL NO. NAME OF EXPERIMENT

1. Write a program in MATLAB for generation and manipulation of signals.

2. Write a Program in MATLAB to demonstrate convolution and correlation.

3. Write Matlab/C++ Program for Discrete Fourier Transform.

4. Write a Program in MATLAB for finding the negative of an image and to show grey level slicing .

5. Write a Program to demonstrate Dynamic range compression and Bit Plane Slicing.

6. Write a program in MATLAB for histogram processing.

7. Write MATLAB program for image smoothing.

8. Write MATLAB program for image sharpening.

9. Write MATLAB program for edge detection.

10. Study image restoration and write a MATLAB program to implement Weiner filter.

11. Write MATLAB program to compress an image using the lossless image compression technique.

12. Write MATLAB program to compress an image using the lossy image compression technique.

Page 3: DSIP Manual

EXPERIMENT NO. 1 Aim : Write a program in MATLAB for generation and manipulation of signals. Theory : Basic signals are:

1. Unit Impulse Signal 2. Unit Step Signal 3. Unit Ramp Signal 4. Exponential Signal 5. Sinusoidal Signal

Generate all these basic signals using following mathematical expressions : UNIT IMPULSE SIGNAL : δ(n) = 1 if n=0 0 if n!=0 UNIT STEP SIGNAL : u(n) = 1 if n>=0 0 if n<0 UNIT RAMP SIGNAL : ur(n) = n if n>=0 0 if n<0 EXPONENTIAL SIGNAL : x(n) = an for all n Manipulation of the signal can be done by performing different operations like addition, folding, scaling, advance and delay. (Note: Create different sine waves, perform addition of these waves. Change parameters of the sine wave like frequency, phase angle and then observe the effect.) The functions used in the following program are : 1. plot( ) : Used to generate 2D plots. Basically plot command is used to generate continuous signal. Syntax : plot(Y) plot(X1,Y1,...) plot(X1,Y1,LineSpec,...) plot(..., 'PropertyName', PropertyValue,...) plot(axes_handle, ...)

Page 4: DSIP Manual

Desciption : Plot(Y) plots the columns of Y versus their index if Y is a real number. If Y is complex, plot(Y) is equivalent to plot(real(Y),imag(Y)). In all other uses of plot, the imaginary component is ignored. Plot(X1,Y1,...) plots all lines defined by Xn versus Yn pairs. If only Xn or Yn is a matrix, the vector is plotted versus the rows or columns of the matrix, depending on whether the vector's row or column dimention matches the matrix. If Xn is a scalar and Yn is a vector, disconnected line objects are created and plotted as discrete points vertically at Xn. Plot(X1,Y1, LineSpec,...) plots all lines defined by the Xn, Yn, LineSpec triples, where LineSpec is a Line speification that determines line type, marker symbol, and colour of the plotted lines. 2. stem( ): It is used to plot discrete sequence data. Syntax: stem(Y) stem(X,Y) stem(..., 'fill') stem(..., LineSpec) stem(axes_handles,...) Description : A 2 D stem plot displays data as lines extending from a baseline along the x-axis. A circle (default) or other marker whose y-position represents the data value terminates each stem. Stem(Y) plots the data sequence Y as stems that extend from equally spaced and automatically generated values along the x-axis. When Y is a matrix, stem plots all elements in a row against the same x value. Stem(X,Y) plots X vs the columns of Y. X and Y must be vectors or matrices of the same size. Additionally, X can be a row or a column vector and Y a matrix with length(X) rows. Stem (..., 'fill') specifies whether to colour the circle at the end of the stem. Stem(..., LineSpec) specifies the line style, marker symbol, and colour for the stem and top marker. Stem(Access_handles,...) plots into the axis obbject with handle axis_handle instead of the current axis object(gca). 3. subplot( ) : It is used to create axes in tiled positions. When we want to display multiple figures at the same time then we can use subplot command to divide output window in parts.

Page 5: DSIP Manual

Syntax : subplot (m,n,p) Description : Subplot divides the current figure into rectangular panes that are numbered row wise. Each Pane contains an axes object. Subsequent plots are output to the current pane. This command breaks the figure window into an m by n matrix of all axes, selects the pth axes object for the current plot, and returns the axes handle. The axes are counted along the top row of the figure window, then the second row, etc. For eg: subplot(2,1,1); plot(income); subplot(2,1,2); plot(outgo); Plots income on the top half of the window and outgo on the bottom half. 4. xlabel, ylabel : Used to label the X and Y axis. Syntax : xlabel('string'); ylabel('string'); Description : Each axes graphics object can have one label for the x, y and z-axis. The label appears beneath its respective axis in a 2D plot and to side or beneath the axis in a 3D plot. xlabel('string') labels the x-axis of the current axes . 5. title( ): This command is used to add title to current figure. Syntax : title('string'); Description :

Each axes graphics object(figure) can have title. The title is located at the top and in the center of the axes.

Page 6: DSIP Manual

title('string') outputs the string at the top and in the center of the current axes.

Algorithm : ( Write down all the steps that we follow while performing this experiment)

Conclusion :

Page 7: DSIP Manual

EXPERIMENT NO. 2

Aim : Write a Program in MATLAB to demonstrate convolution and correlation. Theory: This program will accept two input signals from the user and will generate the convoluted and correlated signals, based on the input signals. The process of convolution and correlation is explained below: Linear Convolution – A discrete time system gives a response y(n) to an input x(n). If the input to the system is a unit impulse signal δ(n) then the output of the system is known as impulse response and is denoted by h(n).

Since an arbitrary input x(n) can be represented using shifted scaled impulses, y(n)=T[x(n)] can be written as follows:

Which is equivalent to:

This equation follows from the linearity property of the system. When the input is delayed by k time units, the systems response is denoted by y(n, k). Similarly, the response to delayed impulse can be denoted by h(n, k). Thus,

For the time invariant system, y(n) can be written as follows:

The above equation is known as convolution sum, and, is equivalent to

Convolution allows us to compute the system’s response for any arbitrary input. Correlation – It is basically used to compare two signals. It can be defined as a measure of the degree to which two signals are similar. It is of two types : Cross Correlation and Auto Correlation. Correlation between two different signals x(n) and y(n) is called Cross Correlation. It is given by formula,

Page 8: DSIP Manual

The index l is called the shift parameter. The order of subscript xy indicates that x(n) is the reference sequence and remains unshifted whereas the sequence y(n) is shifted l units w.r.t. x(n). Thus, the equation can also be shifted and written as follows:

Many times it is necessary to correlate the signal with itself for example determination of the time delays between transmitted and received signal. This is called as Auto Correlation . The various commands used in the program are stated below along with their syntax. 1. input( ) – Used to request user input. Syntax : input(‘Enter number); user_entry = input(' prompt') user_entry = input(' prompt', 's')

Description:

user_entry = input(' prompt') displays prompt as a prompt on the screen, waits for input from the keyboard, and returns the value entered in user_entry .

user_entry = input(' prompt', 's') returns the entered string as a text variable rather than as a variable name or numerical value.

2. disp( ) : This function is used to display text or array. Syntax : disp(X);

Description : disp(X) displays an array, without printing the array name. If X contains a text string, the string is displayed.

Example 1 — Display a matrix with column labels

One use of disp in an M-file is to display a matrix with column labels:

disp(' Corn Oats Hay') disp(rand(5,3))

Page 9: DSIP Manual

which results in

Corn Oats Hay 0.2113 0.8474 0.2749 0.0820 0.4524 0.8807 0.7599 0.8075 0.6538 0.0087 0.4832 0.4899 0.8096 0.6135 0.7741

Example 2 — Display multiple items on the same line

Use concatenation to display multiple items using disp . For example:

x = [1 2 3]; disp(['The values of x are: ', num2str(x)]);

displays

The values of x are: 1 2 3

( Note : Study MATLAB built-in function used for convolution and correlation and write explanation of that function in theory. Compare results of your program with the built-in function )

Algorithm : ( Write down all the steps that we follow while performing this experiment)

Conclusion :

Page 10: DSIP Manual

EXPERIMENT NO. 3

Aim : Write Matlab/C++ Program for Discrete Fourier Transform. Theory : This program will accept an input signal/function from the user and will calculate the Fourier Transform of the function and display the output. The functions that are aperiodic but whose area under the curve is finite can be expressed as the integral of sines or cosines multiplied by weighing function. This formulation is called Fourier Transform. This implies that a given random function can be expressed as a linear combination of fixed frequency functions called basis functions. A signal when represented in terms of basis functions is said to be in the frequency domain. The most important advantage of Fourier Transform is that an original function can be reconstructed from its Fourier Representation without any loss of information. The Fourier Transform of a 1 dimensional continuous function f(x) can be given as:

The inverse transform can be given as:

In these equations, the term cosθ + j sinθ is represented in exponential form using Euler’s formula. Extending the Fourier Transform function to a 1 Dimensional Discrete Function f(x) of N samples, is given by:

This equation represents DFT of f(x). Similarly, we can extend this to a 2 Dimensional Domain function f(x,y) of size NxN as follows:

( Note : Study MATLAB built-in function used for DF T and write explanation of that function in theory. Compare results of your program with the built-in function )

Algorithm : ( Write down all the steps that we follow while performing this experiment)

Conclusion :

Page 11: DSIP Manual

EXPERIMENT NO. 4

Aim : Write a Program in MATLAB for finding the negative of an image and to show grey level slicing . Theory : This program is divided into two parts. The first part takes a predetermined image as the input and displays its negative as the output. The second part is similar to the first, it takes a predetermined input image and shows the grey levelled sliced image as the output. Image Negative – Image Negative is one of the image enhancement technique in spatial domain. It is a basic grey level transformation. It comes under point processing section, where we work with single pixel at a time. Image negative inverts all input grey levels i.e. black in the original image will look white and vice versa using the highest grey level available i.e.(L-1). For an 8 bits per pixel system, L is 255. The transfer function is defined as:

Which is equivalent to:

Where L is the number of grey levels. Example: Consider a 3x3 image, which uses 3 bits per pixel to demonstrate pixel intensity. Thus, the system has 23-1 = 7 Intensity levels. Input Image:

Applying the transfer function (L-1) – r, i.e., 7-r. We get the image negative as the output as shown:

Image negative is useful when we have to analyze small white details embedded in dark backgrounds such as X-Ray images. Gray Level Slicing – Thresholding splits grey levels in two parts, but sometimes we need to highlight a specific range of grey values. So in this case we use grey level slicing. The transfer function is defined as:

Which is equivalent to:

Page 12: DSIP Manual

Example: Consider an image with 2 objects on a background. Object 1 is very bright, Object 2 is moderately bright and the background is dark. The objective is to highlight Object 2. After the transfer function is applied, it highlights the selected object, but destroys all other objects and the background (i.e. they have same intensity x). Hence, this technique is called Gray Level Slicing Without Background. It can be implemented as follows, S = T(r) = L-1 if r1≤ r ≤ r2 = 0 otherwise Gray Level Slicing With Background – It is similar to the above concept, but, with the difference that the linear transformation function is applied to all other areas except r1 and r2. The transfer function is given as:

Page 13: DSIP Manual

The various commands used in the program are stated below along with their syntax: 1. imread( ) - Read image from graphics file Syntax: A = imread(filename, fmt) [X, map] = imread(...) [...] = imread(filename) [...] = imread(URL,...) [...] = imread(...,Param1,Val1,Param2,Val2...) Description: A = imread(filename, fmt) reads a grayscale or color image from the file specified by the string filename. If the file is not in the current folder, or in a folder on the MATLAB path, specify the full pathname. The text string fmt specifies the format of the file by its standard file extension. For example, specify 'gif' for Graphics Interchange Format files. To see a list of supported formats, with their file extensions, use the imformats function. If imread cannot find a file named filename, it looks for a file named filename.fmt. [X, map] = imread(...) reads the indexed image in filename into X and its associated colormap into map. Colormap values in the image file are automatically rescaled into the range [0,1]. 2. imshow( ) – Displays the Image. Syntax: imshow(I) imshow(I,[low high]) imshow(RGB) imshow(filename) Description: imshow(I) displays the grayscale image I. imshow(I,[low high]) displays the grayscale image I, specifying the display range for I in [low high]. The value low (and any value less than low) displays as black; the value high (and any value greater than high) displays as white. Values in between are displayed as intermediate shades of gray, using the default number of gray levels. If you use an empty matrix ([]) for [low high], imshow uses [min(I(:)) max(I(:))]; that is, the minimum value in I is displayed as black, and the maximum value is displayed as white. imshow(RGB) displays the truecolor image RGB. imshow(filename) displays the image stored in the graphics file filename. The file must contain an image that can be read by imread or dicomread. imshow calls imread or dicomread to read the image from the file, but does not store the image data in the MATLAB workspace. If the file contains multiple images, imshow displays the first image in the file. The file must be in the current directory or on the MATLAB path.

Page 14: DSIP Manual

3. double( ) - Convert symbolic matrix to MATLAB numeric form

Syntax :

r = double(S)

Description :

r = double(S) converts the symbolic object S to a numeric object. If S is a symbolic constant or constant expression, double returns a double-precision floating-point number representing the value of S. If S is a symbolic matrix whose entries are constants or constant expressions, double returns a matrix of double precision floating-point numbers representing the values of S's entries.

Examples :

double(sym('(1+sqrt(5))/2')) returns 1.6180 .

The following statements a = sym(2*sqrt(2)); b = sym((1-sqrt(3))^2); T = [a, b]; double(T)

return ans =

2.8284 0.5359

4. im2uint8 – Convert image to 8-bit unsigned integers

Syntax :

I2 = im2uint8(I1)

RGB2 = im2uint8(RGB1)

I = im2uint8(BW)

X2 = im2uint8(X1,'indexed')

Description :

im2uint8 takes an image as input and returns an image of class uint8. If the input image is of class uint8, the output image is identical to the input image. If the input image is not uint8, im2uint8 returns the equivalent image of class uint8, rescaling or offsetting the data as necessary.

I2 = im2uint8(I1) converts the grayscale image I1 to uint8, rescaling the data if necessary.

RGB2 = im2uint8(RGB1) converts the truecolor image RGB1 to uint8, rescaling the data if necessary.

Page 15: DSIP Manual

I = im2uint8(BW) converts the binary image BW to a uint8 grayscale image, changing 1-valued elements to 255.

X2 = im2uint8(X1,'indexed') converts the indexed image X1 to uint8, offsetting the data if necessary. Note that it is not always possible to convert an indexed image to uint8. If X1 is of class double, the maximum value of X1 must be 256 or less; if X1 is of class uint16, the maximum value of X1 must be 255 or less.

5. size( ) - Array dimensions

Syntax :

d = size(X) [m,n] = size(X) m = size(X,dim) [d1,d2,d3,...,dn] = size(X),

Description :

d = size(X) returns the sizes of each dimension of array X in a vector d with ndims(X) elements. If X is a scalar, which MATLAB software regards as a 1-by-1 array, size(X) returns the vector [1 1].

[m,n] = size(X) returns the size of matrix X in separate variables m and n.

m = size(X,dim) returns the size of the dimension of X specified by scalar dim.

Example :

If X = ones(3,4,5), then

[d1,d2,d3] = size(X) d1 = d2 = d3 = 3 4 5

6. rgb2gray – Converts image to greyscale image. Syntax: I = rgb2gray(RGB) newmap = rgb2gray(map) Description:

Page 16: DSIP Manual

I = rgb2gray(RGB) converts the truecolor image RGB to the grayscale intensity image I. rgb2gray converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance. newmap = rgb2gray(map) returns a grayscale colormap equivalent to map. 7. imresize( ) – Resize an image Syntax: B = imresize(A, scale) B = imresize(A, [mrows ncols]) [Y newmap] = imresize(X, map, scale) Description: B = imresize(A, scale) returns image B that is scale times the size of A. The input image A can be a grayscale, RGB, or binary image. If scale is between 0 and 1.0, B is smaller than A. If scale is greater than 1.0, B is larger than A. B = imresize(A, [mrows ncols]) returns image B that has the number of rows and columns specified by [mrows ncols]. Either NUMROWS or NUMCOLS may be NaN, in which case imresize computes the number of rows or columns automatically to preserve the image aspect ratio. [Y newmap] = imresize(X, map, scale) resizes the indexed image X. scale can either be a numeric scale factor or a vector that specifies the size of the output image ([numrows numcols]). By default, imresize returns a new, optimized colormap (newmap) with the resized image. To return a colormap that is the same as the original colormap, use the 'Colormap' parameter (see below).

( Note : Study MATLAB built-in function used for im age negation and grey level slicing. Write explanation of that function in theory. Compare results of your program with the built-in function )

Algorithm : ( Write down all the steps that we follow while performing this experiment)

Conclusion :

Page 17: DSIP Manual

EXPERIMENT NO. 5

Aim : Write a Program to demonstrate Dynamic range compression and Bit Plane Slicing.

Theory :

Dynamic Range Compression :

Sometimes, the dynamic range of the image exceeds the capability of the display device i.e. some pixel values are so large that the other low value pixels are hided.

Foe example, during daytime we cannot see the stars. The reason is, the intensity of the sun is so large and that of stars is so low that the eye cannot adjust to such a large dynamic range.

Thus the technique of compressing the dynamic range is known as Dynamic Range Compression.

Dynamic range compression is achieved by using a log operator.

S = C * log( 1 + | r | )

Bit-plane slicing:

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.

Page 18: DSIP Manual

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).

The bit-plane 7 of an image corresponds just to its binarization with treshold 128 but the general rule to calcuate the graymap of a bit-plane is made building a byte with n bit set to 1, where n is the plan number, and increasing the correspondent decimal value of 1.

Plane Num Byte Val Interval

7 1111111 127+1

6 111111 63+1

5 11111 31+1

4 1111 15+1

( Note : Study MATLAB built-in function and write e xplanation of that function in theory. Compare results of your program with the built-in function )

Algorithm : ( Write down all the steps that we follow while performing this experiment)

Conclusion :

Page 19: DSIP Manual

EXPERIMENT NO. 6

Aim: Write a program in MATLAB for histogram processing.

Theory: A perfect image is one which has equal number of pixels in all its grey levels. Hence our objective is not only to spread the dynamic range , but also to have equal pixels in all the grey levels. This technique is known as histogram equalization. We now search for a transformation that would transform a bad histogram to a flat histogram. We know S = T( r ) To produces equal values in each grey level. The transformation must satisfy the following conditions: (1) T( r ) must be single valued and monotoniically increasing in the interval 0 ≤ r ≤ 1 and (2) 0 ≤ T( r ) ≤ 1 for 0 ≤ r ≤ 1 i.e. 0 ≤ s ≤ 1 for 0 ≤ r ≤ 1 Here the range of r is taken as [0,1] . This is called the normalized range. This range is taken for simplicity. So instead of r being in the range [0,255] we take [0,1]. Advantage : It helps to improve the functionality of contrast enhancement. Disadvantage: Histogram equalization is automatic. It is not interactive i.e. it always gives one result an approximation to an uniform histogram.

Syntax :

J = histeq(I, hgram)

J = histeq(I, n)

[J, T] = histeq(I,...)

newmap = histeq(X, map, hgram)

newmap = histeq(X, map)

[newmap, T] = histeq(X,...)

Page 20: DSIP Manual

Description :

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.

J = histeq(I, hgram) transforms the intensity image I so that the histogram of the output intensity image J with length(hgram) bins approximately matches hgram. The vector hgram should contain integer counts for equally spaced bins with intensity values in the appropriate range: [0, 1] for images of class double, [0, 255] for images of class uint8, and [0, 65535] for images of class uint16. histeq automatically scales hgram so that sum(hgram) = prod(size(I)). The histogram of J will better match hgram when length(hgram) is much smaller than the number of discrete levels in I.

J = histeq(I, n) transforms the intensity image I, returning in J an intensity image with n discrete gray levels. A roughly equal number of pixels is mapped to each of the n levels in J, so that the histogram of J is approximately flat. (The histogram of J is flatter when n is much smaller than the number of discrete levels in I.) The default value for n is 64.

[J, T] = histeq(I,...) returns the grayscale transformation that maps gray levels in the image I to gray levels in J.

newmap = histeq(X, map, hgram) transforms the colormap associated with the indexed image X so that the histogram of the gray component of the indexed image (X,newmap) approximately matches hgram. The histeq function returns the transformed colormap in newmap. length(hgram) must be the same as size(map,1).

newmap = histeq(X, map) transforms the values in the colormap so that the histogram of the gray component of the indexed image X is approximately flat. It returns the transformed colormap in newmap.

[newmap, T] = histeq(X,...) returns the grayscale transformation T that maps the gray component of map to the gray component of newmap.

Class Support

For syntax that include an intensity image I as input, I can be of class uint8, uint16, int16, single, or double. The output image J has the same class as I.

For syntax that include an indexed image X as input, X can be of class uint8, single, or double; the output colormap is always of class double. The optional output T (the gray-level transform) is always of class double.

( Note : Compare results of your program with the built-in function )

Algorithm : ( Write down all the steps that we follow while performing this experiment)

Conclusion :

Page 21: DSIP Manual

EXPERIMENT NO. 7 Aim : Write MATLAB program for image smoothing. Theory : A low-pass filter attenuates high frequencies and retains low frequencies unchanged. The result in the spatial domain is equivalent to that of a smoothing filter; as the blocked high frequencies correspond to sharp intensity changes, i.e. to the fine-scale details and noise in the spatial domain image. The result in the spatial domain is equivalent to that of a smoothing filter; as the blocked high frequencies correspond to sharp intensity changes, i.e. to the fine-scale details and noise in the spatial domain image. The spatial low pass filter or mask used is as follow :

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. Smoothing is also usually based on a single value representing the image, such as the average value of the image or the middle (median) value.

Smoothing is achieved in the frequency domain by dropping out the high frequency components. The reason for doing the filtering in the frequency domain is generally because it is computationally faster to perform two 2D Fourier transforms and a filter multiplies than to perform a convolution in the image (spatial) domain. Filtering in the frequency domain can be much faster – especially for large images. The basic model for filtering is: G(u,v) = H(u,v)F(u,v) where F(u,v) is the Fourier transform of the image being filtered and H(u,v) is the filter transform function Simply cut off all high frequency components that are at specified distance D0 from the origin of the transform,

Page 22: DSIP Manual

changing the distance changes the behaviour of the filter. The transfer function for the ideal low pass filter can be given as: where D(u,v) is given as:

( Note : Study MATLAB built-in function used for hi gh pass filter and write explanation of that function in theory. Compare results of your program with the built-in function )

Algorithm : ( Write down all the steps that we follow while performing this experiment)

Conclusion :

>≤

=0

0

),( if 0

),( if 1),(

DvuD

DvuDvuH

2/122 ])2/()2/[(),( NvMuvuD −+−=

Page 23: DSIP Manual

EXPERIMENT NO. 8 Aim : Write MATLAB program for image sharpening. Theory : There are basically three different kinds of filters: lowpass, highpass and bandpass filters. A low-pass filter attenuates high frequencies and retains low frequencies unchanged. The result in the spatial domain is equivalent to that of a smoothing filter; as the blocked high frequencies correspond to sharp intensity changes, i.e. to the fine-scale details and noise in the spatial domain image. A highpass filter, on the other hand, yields edge enhancement or edge detection in the spatial domain, because edges contain many high frequencies. Areas of rather constant gray level consist of mainly low frequencies and are therefore suppressed. A bandpass filter attenuates very low and very high frequencies, but retains a middle range band of frequencies. Bandpass filtering can be used to enhance edges (suppressing low frequencies) while reducing the noise at the same time (attenuating high frequencies). Spatial filters are designed to highlight or supress features in an image based on their spatial frequency. The spatial frequency is related to the textural characteristics of an image. Rapid variations in brightness levels ('roughness') reflect a high spatial frequency; 'smooth' areas with little variation in brightness level or tone are characterized by a low spatial frequency. Spatial filters are used to suppress 'noise' in an image, or to highlight specific image characteristics. We obtain a high pass filter function by inverting the corresponding low pass filter, e.g. an ideal high pass filter blocks all smaller frequencies and leaves the others unchanged A high pass filter, on the other hand, yields edge enhancement or edge detection in the spatial domain, because edges contain many high frequencies. Areas of rather constant gray level consist of mainly low frequencies and are therefore suppressed. The high pass spatial filter or mask used is as follow :

The reason for doing the filtering in the frequency domain is generally because it is computationally faster to perform two 2D Fourier transforms and a filter multiplies than to perform a convolution in the image (spatial) domain. Filtering in the frequency domain can be much faster – especially for large images.

Page 24: DSIP Manual

High pass frequencies are precisely the reverse of low pass filters, so: Hhp(u, v) = 1 – Hlp(u, v) The ideal high pass filter is given as: where D0 is the cut off distance

( Note : Study MATLAB built-in function used for hi gh pass filter and write explanation of that function in theory. Compare results of your program with the built-in function )

Algorithm : ( Write down all the steps that we follow while performing this experiment)

Conclusion :

>≤

=0

0

),( if 1

),( if 0),(

DvuD

DvuDvuH

Page 25: DSIP Manual

EXPERIMENT NO. 9

Aim : Write a program in MATLAB for edge detection Theory : The various algorithms used for edge detection technique are as follows:-

1. CANNY EDGE DETECTOR : The Canny edge detection operator was developed by John F. Canny in 1986 and uses a multi-stage algorithm to detect a wide range of edges in images. Most importantly, Canny also produced a computational theory of edge detection explaining why the technique works.

The Canny algorithm contains a number of adjustable parameters, which can affect the computation time and effectiveness of the algorithm.

• The size of the Gaussian filter: the smoothing filter used in the first stage directly affects the results of the Canny algorithm. Smaller filters cause less blurring, and allow detection of small, sharp lines. A larger filter causes more blurring, smearing out the value of a given pixel over a larger area of the image. Larger blurring radii are more useful for detecting larger, smoother edges – for instance, the edge of a rainbow.

• Thresholds: the use of two thresholds with hysteresis allows more flexibility than in a single-threshold approach, but general problems of thresholding approaches still apply. A

Page 26: DSIP Manual

threshold set too high can miss important information. On the other hand, a threshold set too low will falsely identify irrelevant information (such as noise) as important. It is difficult to give a generic threshold that works well on all images. No tried and tested approach to this problem yet exists.

2. PREWITT : It is a method of edge detection in image processing which calculates the maximum response of a set of convolution kernels to find the local edge orientation for each pixel

Various kernels can be used for this operation. The whole set of 8 kernels is produced by taking one of the kernels and rotating its coefficients circularly. Each of the resulting kernels is sensitive to an edge orientation ranging from 0° to 315° in steps of 45°, where 0° corresponds to a vertical edge.

The maximum response for each pixel is the value of the corresponding pixel in the output magnitude image. The values for the output orientation image lie between 1 and 8, depending on which of the 8 kernels produced the maximum response.

This edge detection method is also called edge template matching, because a set of edge templates is matched to the image, each representing an edge in a certain orientation. The edge magnitude and orientation of a pixel is then determined by the template that matches the local area of the pixel the best.

The Prewitt edge detector is an appropriate way to estimate the magnitude and orientation of an edge. Although differential gradient edge detection needs a rather time-consuming calculation to estimate the orientation from the magnitudes in the x- and y-directions, the Prewitt edge detection obtains the orientation directly from the kernel with the maximum response. The set of kernels is limited to 8 possible orientations; however experience shows that most direct orientation estimates are not much more accurate.

On the other hand, the set of kernels needs 8 convolutions for each pixel, whereas the set of kernel in gradient method needs only 2, one kernel being sensitive to edges in the vertical direction and one to the horizontal direction. The result for the edge magnitude image is very similar with both methods, provided the same convolving kernel is used.

Formulation :

Mathematically, the operator uses two 3×3 kernels which are convolved with the original image to calculate approximations of the derivatives - one for horizontal changes, and one for vertical.

3. SOBEL OPERATOR : The Sobel operator is used in image processing, particularly within edge detection algorithms. Technically, it is a discrete differentiation operator, computing an approximation of the gradient of the image intensity function. At each point in the image, the result of the Sobel operator is either the corresponding gradient vector or the norm of this vector. The Sobel operator is based on convolving the image with a small, separable, and integer valued filter in horizontal and vertical direction and is therefore relatively inexpensive in terms of computations. On the other hand, the gradient

Page 27: DSIP Manual

approximation which it produces is relatively crude, in particular for high frequency variations in the image.

4. ROBERTS CROSS : The Robert’s Cross operator is used in image processing and computer vision for edge detection. It was one of the first edge detectors and was initially proposed by Lawrence Roberts in 1963. As a differential operator, the idea behind the Robert's Cross operator is to approximate the gradient of an image through discrete differentiation which is achieved by computing the sum of the squares of the differences between diagonally adjacent pixels.

Page 28: DSIP Manual

( Note : Study MATLAB built-in function used for convolution and correlation and write explanation of that function in theory. Compare results of your program with the built-in function )

Algorithm : ( Write down all the steps that we follow while performing this experiment)

Conclusion:

Page 29: DSIP Manual

EXPERIMENT NO. 10

Aim : Study image restoration and write a MATLAB program to implement Weiner filter. Theory : The purpose of image restoration is to "compensate for" or "undo" defects which degrade an image. Degradation comes in many forms such as motion blur, noise, and camera misfocus. In cases like motion blur, it is possible to come up with an very good estimate of the actual blurring function and "undo" the blur to restore the original image. In cases where the image is corrupted by noise, the best we may hope to do is to compensate for the degradation it caused. In this project, we will introduce and implement several of the methods used in the image processing world to restore images. Weiner Filtering : The Wiener filter purpose is to reduce the amount of noise present in a signal by comparison with an estimation of the desired noiseless signal. It is based on a statistical approach. Typical filters are designed for a desired frequency response. The Wiener filter approaches filtering from a different angle. One is assumed to have knowledge of the spectral properties of the original signal and the noise, and one seeks the LTI filter whose output would come as close to the original signal as possible. Wiener filters are characterized by the following: 1. Assumption: signal and (additive) noise are stationary linear stochastic processes with known spectral characteristics or known autocorrelation and crosscorrelation. 2. Requirement: the filter must be physically realizable, i.e. causal (this requirement can be dropped, resulting in a non-causal solution).

3. Performance criteria: minimum mean-square error.

The inverse filtering is a restoration technique for deconvolution, i.e., when the image is blurred by a known lowpass filter, it is possible to recover the image by inverse filtering or generalized inverse filtering. However, inverse filtering is very sensitive to additive noise. The approach of reducing one degradation at a time allows us to develop a restoration algorithm for each type of degradation and simply combine them. The Wiener filtering executes an optimal tradeoff between inverse filtering and noise smoothing. It removes the additive noise and inverts the blurring simultaneously.

The Wiener filtering is optimal in terms of the mean square error. In other words, it minimizes the overall mean square error in the process of inverse filtering and noise smoothing. The Wiener filtering is a linear estimation of the original image. The approach is based on a stochastic framework. The orthogonality principle implies that the Wiener filter in Fourier domain can be expressed as follows:

Page 30: DSIP Manual

where are respectively power spectra of the original image and the additive noise, and is the blurring filter. It is easy to see that the Wiener filter has two separate part, an inverse filtering part and a noise smoothing part. It not only performs the deconvolution by inverse filtering (highpass filtering) but also removes the noise with a compression operation (lowpass filtering).

Implementation To implement the Wiener filter in practice we have to estimate the power spectra of the original image and the additive noise. For white additive noise the power spectrum is equal to the variance of the noise. To estimate the power spectrum of the original image many methods can be used. A direct estimate is the periodogram estimate of the power spectrum computed from the observation:

where Y(k,l) is the DFT of the observation. The advantage of the estimate is that it can be implemented very easily without worrying about the singularity of the inverse filtering. Another estimate which leads to a cascade implementation of the inverse filtering and the noise smoothing is

which is a straightforward result of the fact: The power spectrum can be estimated directly from the observation using the periodogram estimate. This estimate results in a cascade implementation of inverse filtering and noise smoothing:

The disadvantage of this implementation is that when the inverse filter is singular, we have to use the generalized inverse filtering. People also suggest the power spectrum of the original image can be estimated based on a model such as the model. To illustrate the Wiener filtering in image restoration we use the standard 256x256 Lena test image. We blur the image with the lowpass filter

Page 31: DSIP Manual

then put into the blurred image the additive white Gaussian noise of variance 100. The Wiener filtering is applied to the image with a cascade implementation of the noise smoothing and inverse filtering. The images are listed as follows together with the PSNRs and MSEs. Notice that the restored image is improved in terms of the visual performance, but the MSEs don't indicate this, the reason of which is that MSE is not a good metric for deconvolution.

( Note : Study MATLAB built-in function used for Weiner filter and write explanation of that function in theory. Compare results of your program with the built-in function )

Algorithm : ( Write down all the steps that we follow while performing this experiment)

Conclusion : Wiener filtering is the optimal tradeoff between the inverse filtering and noise smoothing. It can be interpreted as a inverse filtering step followed by a noise attenuation step. Wiener filters are one of the tools of choice in removing noise from photographic images. However, to implement the Wiener filter we have to estimate the power spectrum of the original image from the corrupted observation. This is the main task in making the Wiener filtering work well in practice.

Page 32: DSIP Manual

EXPERIMENT NO. 11

Aim : Write MATLAB program to compress an image using the lossless image compression technique. Theory : Lossless data compression is a class of data compression algorithms that allows the exact original data to be reconstructed from the compressed data. The term lossless is in contrast to lossy data compression, which only allows an approximation of the original data to be reconstructed, in exchange for better compression rates. Lossless compression is used in cases where it is important that the original and the decompressed data be identical, or where deviations from the original data could be deleterious.

Lossless compression techniques :

Most lossless compression programs do two things in sequence: the first step generates a statistical model for the input data and the second step uses this model to map input data to bit sequences in such a way that "probable" (e.g. frequently encountered) data will produce shorter output than "improbable" data.

The primary encoding algorithms used to produce bit sequences are Huffman coding (also used by DEFLATE) and arithmetic coding. Arithmetic coding achieves compression rates close to the best possible for a particular statistical model, which is given by the information entropy, whereas Huffman compression is simpler and faster but produces poor results for models that deal with symbol probabilities close to 1.

There are two primary ways of constructing statistical models: in a static model, the data is analyzed and a model is constructed, then this model is stored with the compressed data. This approach is simple and modular, but has the disadvantage that the model itself can be expensive to store, and also that it forces a single model to be used for all data being compressed, and so performs poorly on files containing heterogeneous data. Adaptive models dynamically update the model as the data is compressed. Both the encoder and decoder begin with a trivial model, yielding poor compression of initial data, but as they learn more about the data performance improve. Most popular types of compression used in practice now use adaptive coders.

Lossless compression methods may be categorized according to the type of data they are designed to compress. While, in principle, any general-purpose lossless compression algorithm (general-purpose meaning that they can compress any bit string) can be used on any type of data, many are unable to achieve significant compression on data that are not of the form for which they were designed to compress. Many of the lossless compression techniques used for text also work reasonably well for indexed images.

Page 33: DSIP Manual

Lossless data compression algorithms fall into two broad categories : Dictionary based techniques and Statistical methods. Dictionary based techniques generate a compressed file containing fixed codes, each of which represents a particular sequence of bytes in the original file. Statistical methods on the other hand implement data compression by representing frequently occurring characters in the file with fewer bits than they do for less commonly occurring ones. 1. Dictionary Based Coding

a. Run Length Encoding ( RLE ) :

Run Length Encoding (RLE) is a simple and popular data compression algorithm. It is based on the idea to replace a long sequence of the same symbol by a shorter sequence and is a good introduction into the data compression field for newcomers. The sequence of length l of a repeated symbol 's' is replaced by a shorter sequence, usually containing one or more symbols of 's', a length information and sometimes an escape symbol. RLE algorithms differ from each other mainly in three points: the threshold t, the marking of the start of a run and the coding of the length information. If l is smaller than t, the run stays unchanged, and if l is greater or equal t, the run is replaced. The start of a run can be indicated by a threshold run or an escape symbol 'c'. If a threshold run is used the start is characterized by a small sequence of 's', which has a length greater or equal than t. If an escape symbol 'c' indicates the start of a run, 's' is normally put behind 'c' in order to characterize the run symbol. 'c' must not be an element of the alphabet or occurrences of 'c' have to be coded in a way, that they are not mixed up with the start of a run. b. The LZW Compression :

LZW compression is the compression of a file into a smaller file using a table-based lookup

algorithm invented by Abraham Lempel, Jacob Ziv, and Terry Welch. Two commonly-used file

formats in which LZV compression is used are the GIF image format served from Web sites and

the TIFF image format. LZW compression is also suitable for compressing text files.

A particular LZW compression algorithm takes each input sequence of bits of a given length (for

example, 12 bits) and creates an entry in a table (sometimes called a "dictionary" or "codebook")

for that particular bit pattern, consisting of the pattern itself and a shorter code. As input is read,

any pattern that has been read before results in the substitution of the shorter code, effectively

compressing the total amount of input to something smaller. Unlike earlier approaches, known as

LZ77 and LZ78, the LZW algorithm does include the look-up table of codes as part of the

compressed file. The decoding program that uncompresses the file is able to build the table itself

by using the algorithm as it processes the encoded input.

Page 34: DSIP Manual

2. Statistical Coding

3. Huffman Coding

Huffman coding is a statistical technique which attempts to reduce the amount of bits required to represent a string of symbols. The algorithm accomplishes its goals by allowing symbols to vary in length. Shorter codes are assigned to the most frequently used symbols, and longer codes to the symbols which appear less frequently in the string. Arithmetic coding is another statistical coding technique.

Building a Huffman Tree

The Huffman code for an alphabet (set of symbols) may be generated by constructing a binary tree with nodes containing the symbols to be encoded and their probabilities of occurrence. The tree may be constructed as follows:

Step 1. Create a parentless node for each symbol. Each node should include the symbol and its probability.

Step 2. Select the two parentless nodes with the lowest probabilities.

Step 3. Create a new node which is the parent of the two lowest probability nodes.

Step 4. Assign the new node a probability equal to the sum of its children's probabilities.

Step 5. Repeat from Step 2 until there is only one parentless node left.

The code for each symbol may be obtained by tracing a path to the symbol from the root of the tree. A 1 is assigned for a branch in one direction and a 0 is assigned for a branch in the other direction. For example a symbol which is reached by branching right twice, then left once may be represented by the pattern '110'. The figure below depicts codes for nodes of a sample tree.

* / \ (0) (1) / \ (10)(11) / \ (110)(111)

( Note : Study MATLAB built-in function used for lo ssless data compression and write explanation of that function in theory. Compare results of your program with the built-in function )

Algorithm : ( Write down all the steps that we follow while performing this experiment)

Conclusion :

Page 35: DSIP Manual

EXPERIMENT NO. 12 Aim : Write MATLAB program to compress an image using the lossy image compression technique Theory : Lossy compression reduces a file by permanently eliminating certain information, especially redundant information. When the file is uncompressed, only a part of the original information is still there. Lossy compression is generally used for video and sound, where a certain amount of information loss will not be detected by most users. The JPEG image file, commonly used for photographs and other complex still images on the Web, is an image that has lossy compression. Using JPEG compression, the creator can decide how much loss to introduce and make a trade-off between file size and image quality. Lossy compression achieves higher compression ratios than lossless, but at the expense of image quality, with the degree of lossiness under user control. The artifact introduced during lossy compression depends on the compression scheme and how it is implemented.

Lossy File Formats :

• JPEG

Developed by the Joint Photographics Expiratory group, JPEG is a compression scheme, with JFIF as the associated file format. At present, JPEG compression is based on the DCT (Discrete Cosine Transform) approach. JPEG compression is lossy, although it can be made to operate in lossless mode.

With JPEG compression, the degree of lossiness is under operator control. Because the current implementation of JPEG operates on 8 x 8 pixel segments, images can appear blocky at high compression ratios.

• Wavelet

The wavelet compression algorithm has features similar to, yet different from, the Fourier transform. (3). Although often used for lossy compression, the wavelet algorithm can be operated in a lossless mode. An important point is that wavelet compression operates on an entire image at once, thus avoiding the "blockiness" associated with JPEG methodology.

Lossy compression techniques :

1. Improved Grey Scale ( IGS ) Quantization

2. Predictive Coding

3. Transform Coding

Page 36: DSIP Manual

( Note : Study MATLAB built-in function used for l ossy data compression and write explanation of that function in theory. Compare results of your program with the built-in function )

Algorithm : ( Write down all the steps that we follow while performing this experiment)

Conclusion :