Top Banner
LABORATORY WORK BOOK Digital Image Processing Department of Electronic Engineering Lahore College for Women University, Lahore
40
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Digital Image Processing

LABORATORY WORK BOOK

Digital Image Processing

Department of Electronic Engineering

Lahore College for Women University, Lahore

Page 2: Digital Image Processing

Digital Image Processing Laboratory Work Book

CONTENTS

Sr.

No. Experiment Name

1 Introduction to the Lab and reading of image

2 Introduction to Display and Write Images

3 Introduction to Vector Indexing

4

Introduction to Matrix indexing

5 Image operation using Array indexing:

6 Introduction to Flow Control

7 Introduction to basic image processing techniques in spatial domain

such as histogram and morphological operations

8

Introduction to image filtering in spatial and in frequency domains

9 Introduction to image restoration

10 Introduction of principles of the JPEG baseline coding system.

11

Introduction to Imadjust , IPT tool for intensity transformations of gray scale images.

12

Introduction to Histogram Processing and Function Plotting

13 Computing and Visualizing the 2-D DFT in Matlab

Page 3: Digital Image Processing

Digital Image Processing Laboratory Work Book

Lab 1 –Introduction to the Lab and reading of image

1. Introduction

Lab Software

In this lab implement image processing techniques that you will learn during the

frontal course. Work in the lab will be carried out in Matlab

2. MATLAB working environment

MATLAB desktop: is the main window which contains five subwindows: the

command window, the workspace browser, the current directory window, the

command history window and figure window.

Command window: is where the user types MATLAB commands and expressions at

the prompt (>>) and where the outputs of those command are displayed.

Workspace browser: shows the variables and some information about it.

Current directory: the current directory tab above the workspace tab shows the

contents of the current directory window, whose path is shown in the current directory.

Command history window: contains the record of the command a user has entered in

command window, including both current and previous.

3. Reading Images.

Images are read into the MATLAB environment using function imread whose syntax

is

imread (‘filename’)

Here filename is a string containing the complete name of the image file (including

extension)

>> f= imread (‘chestxray.jpg’);

Above command read the image and store into image array f.

The simplest way to read an image from specified directory to include a full or relative

path to the directory in filename.

>> f= imread(‘D:\myimages\chestray.jpg’);

Read the image from a folder called myimages.

4. Size of the Image:

>> size(f)

Ans=1024 1024

Above function gives rows and columns dimension of an image

This function is useful in programming when used in the following form to determine

automatically the size of an image.

>> [M, N] = size (f);

Page 4: Digital Image Processing

Digital Image Processing Laboratory Work Book

The whos function display additional information about an array.

>> whos f

Name size bytes class

F 1024 x1024 1048576 Unit8array

Page 5: Digital Image Processing

Digital Image Processing Laboratory Work Book

Lab 2 – Introduction to Display and Write Images

Images are displayed on MATLAB desktop using function imshow,

� imshow(f,G)

Where f is an image array and G is the number of intensity levels used

to display it. If G is omitted, it defaults to 256 levels.

� imshow (f, [low high])

display as black all values less than or equal to low, and as white all

values greater than or equal to high. The values in between are displayed as

intermediate intensity values using default number of levels.

� Imshow(f,[ ] )

Sets variable low to minimum value of array f and high to its maximum

value.

Pixval

This function is used frequently to display the intensity values of the

individual pixels interactively.

If another image, g, is displayed using imshow, MATLAB replaces

the image in the screen with new image. To keep the first image and

output a second image, we use figure as follows:

>> figure, imshow(g)

Page 6: Digital Image Processing

Digital Image Processing Laboratory Work Book

Using the statement

>> imshow(f), figure, imshow(g)

Display both images.

Writing images:

Images are written to the disk using function imwrite.

imwrite (f, ‘filename’)

The string contained in filename must include a recognized file format

extension. For example, the following command writes f to a TIFF file

named patient10_run1:

>> imwrite(f, ‘patient10_run1’,’tif’)

Or alternatively

>> imwrite (f, ‘patient10_run1.tif’)

The imwrite function can have other parameter, depending on the file format

selected. More general imwrite syntax applicable only to JPEG images is:

imwrite (f, ‘filename.jpg’,’quality’,q)

Where q is an integer between 0 and 100(the lower the number the higher the

degradation due to JPEG compression)

Page 7: Digital Image Processing

Digital Image Processing Laboratory Work Book

To obtain other image file details,

Iminfo filename

While filename is complete file name of the image.

>> iminfo bubbles25.jpg

Out put of the following information

Filename: ‘bubbles25.jpg’

FileModDate: ’04-Jan-2003 12:31:26’

FileSize: 13849

Format: ‘jpg’

FormatVersion: ‘ ’

Width: 714

Height: 682

BitDepth: 8

ColorType: ‘grayscale’

FormatSignature: ‘ ’

Page 8: Digital Image Processing

Digital Image Processing Laboratory Work Book

Comment: { }

More general imwrite syntax applicable only to tif images has the form

imwrite (g,’filename.tif’,’compression’,’parameter’ . . . ‘Resolution’,[colres

rores])

Where ‘parameter’ can have one of the following principal values:’none’

indicates no compression; ‘packbits’ indicates packbits compression;

Page 9: Digital Image Processing

Digital Image Processing Laboratory Work Book

Lab 3. Introduction to Vector Indexing

The elements of vector in MATLAB are enclosed by square brackets and are

separated by spaces or by commas. For example

>> v= [1 3 5 7 9]

V=

1 3 5 7 9

>> v (2)

Ans=3

A row vector is converted to a column vector using the transpose operator(.’)

>>w = v.’

W= 1

3

5

7

9

Page 10: Digital Image Processing

Digital Image Processing Laboratory Work Book

To access blocks of elements, we use MATLAB’s colon notation.

>>v (1:3)

Ans= 1 3 5

>>v (2:4)

Ans=3 5 7

>>v (3: end)

Ans=5 7 9

>>v (: )

Produce a column vector

>>v (1:end)

Produce a row vector

>>v (1:2: end)

Ans=1 5 9

The notation 1:2: end says to start at 1, count by 2 and stop when the count

reaches the last element.

Page 11: Digital Image Processing

Digital Image Processing Laboratory Work Book

>>v (end:-2:1)

Ans=9 5 1

Here the index count start form end and decreased by 2 and stop when it

reached the first element

X=linspace(a,b,n)

Generate a row vector x of n element linearly spaced between and including a

and b.

>>v ([1 4 5])

Ans=1 7 9

Page 12: Digital Image Processing

Digital Image Processing Laboratory Work Book

Lab 4:Introduction to Matrix indexing

Matrix can be represented in MATLAB as a sequence of row vector enclosed

by square brackets and separated by semicolons.For example

>>A= [1 2 3; 4 5 6; 7 8 9]

Display 3x3 matrix

A= 1 2 3

4 5 6

7 8 9

>> A (2, 3)

Ans= 6

Above statement extract element form 2nd row and 3

rd column

>>c3=A (:,3)

C3=

3

6

9

Page 13: Digital Image Processing

Digital Image Processing Laboratory Work Book

Extract the second row as follows

>>R2=A (2,: )

R2= 4 5 6

The following statement extracts the top two rows:

>> T2=A (1:2, 1:3)

T2=

1 2 3

4 5 6

To create a matrix B equal to A but with its last column set to 0s,we write

>> B=A;

>> B (: , 3)=0

B= 1 2 0

4 5 0

7 8 0

Page 14: Digital Image Processing

Digital Image Processing Laboratory Work Book

>> A (end , end)

Ans= 9

>> A (end, end-2)

Ans=7

>>A (2: end, end:-2:1)

Ans= 6 4

9 7

Using vectors to index into a matrix provides a powerful approach for element

selection.

>> E=A ([1 3], [2 3])

E= 2 3

8 9

>> D=logical ([1 0 0; 0 0 1; 0 0 0])

D=

1 0 0

0 0 1

Page 15: Digital Image Processing

Digital Image Processing Laboratory Work Book

0 0 0

Then

>>A (D)

Ans=

1

6

>>v=T2 (: )

V=

1

4

2

5

3

6

Page 16: Digital Image Processing

Digital Image Processing Laboratory Work Book

To find the sum of all the elements of a matrix

>>s= sum (A (:))

S= 45

>> C= [1 2 3; 4 5 6; 7 8 9];

>>B= [0 2 4; 3 5 6; 3 4 9];

>>A= =B

Ans=

0 1 0

0 1 1

0 0 1

>>A>= B

Ans=

1 1 0

1 1 1

1 1 1

Page 17: Digital Image Processing

Digital Image Processing Laboratory Work Book

Lab 5:Image operation using Array indexing:

Rose image is read by using

F=imread (‘rose_512.tif’)

Above statement gives image which is vertically flipped using

>>fp=f (end: -1:1, : );

The obtained image is cropped by using

>> fc = f(1:2:end, 1:2:end);

Similarly image is sub sampled by using

>>fs = f(1:2:end, 1:2:end);

Finally horizontal scan line through the middle of original image obtained by using

>> Plot (f (512, : )

Some inportane standard arrays:

• Zeros(M,N) generates an MxN matrix of 0s

• Ones(M,N) generates an MxN matrix of 1s

• True (M,N) generate MxN logical matrix of 1s

Page 18: Digital Image Processing

Digital Image Processing Laboratory Work Book

• False(M,N) generate MxN logical matrix of 0s

• Rand(M,N) generate MxN matrix whose entries are uniformly

distributed random numbers in the interval[0,1]

• Randn(M,N) generate MxN matrix whose entries are normally

distributed random numbers with mean 0 and variance 1

• Magic (M) generates MxM square matrix. This is square array in

which the sum along any row, column or main diagonal, is the same.

Page 19: Digital Image Processing

Digital Image Processing Laboratory Work Book

Lab 6: Introduction to Flow Control

MATLAB provides the eight flow control statement.

For loop

For loop executes a group of statements a specified number of times: the

syntax is

For index = start: increment: end

Statements

End

It is possible to nest two or more for loops, as follows:

For index1 = start1:increment1: end

Statemens1

For index2 = start2:increment2: end

Statement2

End

Additional loop1 statements

End

Page 20: Digital Image Processing

Digital Image Processing Laboratory Work Book

For example

Count=0;

For k =0:0.1: 1

Count= count +1;

End

While

A while loop execute a group of statements for as long as the expression

controlling the loop is true.

While expression

Statements

End

While loop can be nested

While expression1

Statement1

While expression2

Statements2

Page 21: Digital Image Processing

Digital Image Processing Laboratory Work Book

End

Additional loop1 statements

End

For example

A=10

B=5

While a

A=a-1;

While b

B=b-1;

End

End

Page 22: Digital Image Processing

Digital Image Processing Laboratory Work Book

Lab 7 – Introduction to basic image processing \chetechniques in spatial domain such as histogram and morphological operations.

In this lab we will study two basic image-processing techniques in spatial domain:

histogram algorithms and morphology.

Histogram techniques allow us to analyze the distribution of gray levels in an image. In

this lab we will study histogram, contrast stretching and histogram equalization.

Morphology is aimed to study the form and structure of objects in the image. In the lab

we will start with binary morphology and then we will continue with grayscale

morphology.

1. Preliminary report

Part 1 - Histogram

1. Give the definition of histogram. What can a histogram be used for?

2. Give the definition of contrast stretching. What can contrast stretching be used

for?

3. Give the definition of histogram equalization. What can histogram equalization

are used for?

4. Explain why after histogram equalization the histogram values don’t become

equal (in other words, they are not lying on a straight line).

Part 2 – Binary morphology [1,2]

1. What is the structuring element used in morphological operators?

2. Describe operations of erosion, dilation, opening and closing in binary

morphology.

Implement in Matlab functions that perform opening and closing using Matlab’s

functions imerode and imdilate.

Part 3 – Grayscale morphology [1]

Describe operations of erosion, dilation, opening and closing in grayscale

morphology. What is a morphological gradient?

1. What is top-hat transformation?

2. What is bottom-hat transformation?

Find a binary image and grayscale image (you may use the auxiliary programs

from lab 1). These images will be used to test the histogram and morphological

operations. Send these images to your university (or other) web mail that you can

Page 23: Digital Image Processing

Digital Image Processing Laboratory Work Book

access from the lab.

3. Description of the experiment

Part 1 – Histogram

Use the supplied program hist_demo.m.

1. Observe the following demonstrations:

a. Contrast stretching.

b. Histogram equalization.

3. Test these demonstrations with the image of your choice.

Part 2 – Binary morphology

Use the supplied program bin_morph_demo.m.

3. Observe the following demonstrations:

a. Erosion,

b. Dilation,

c. Opening,

d. Closing.

4. Test these demonstrations with the image of your choice. Change the structure

element to a cross, to a bounding box and to a structuring element of your choice.

Observe different results for different structuring elements.

Part 3 – Grayscale morphology

Use the supplied program gray_morph_demo.m.

1. Observe the following demonstrations:

a. Erosion,

b. Dilation,

c. Opening,

d. Closing,

e. Morphological gradient,

f. Top-hat transformation,

g. Bottom-hat transformation,

h. Edge enhancement.

2. Test these demonstrations with the image of your choice.

4. Final report

Part 1 – Histogram

1. Submit the results of demonstrations from Part 1 with the image of your choice.

2. Suppose the histogram of an image has two sharp peaks. The histogram with

two sharp peaks is called bimodal. Explain how bimodal histogram can be used

Page 24: Digital Image Processing

Digital Image Processing Laboratory Work Book

for binarization.

Part 2 – Binary morphology

1. Submit the results of demonstrations from Part 2 with the image of your choice.

2. What is the connection between the box and cross structuring elements and

pixel connectivity?

Part 3 – Grayscale morphology

1. Submit the results of demonstrations from Part 3 with the image of your choice.

2. Explain the implementation of edge enhancement in the program

gray_morph_demo.m.

Page 25: Digital Image Processing

Digital Image Processing Laboratory Work Book

Lab 8 – Introduction to image filtering in spatial and in frequency domains

1. Introduction

In the early days of image processing the use of Discrete Fourier Transform (DFT)

was very restricted because of its high computational complexity. With the

introduction of the FFT algorithm, the complexity of DFT was reduced and DFT

became an extremely important practical tool of image processing.

In this lab we'll study the properties of DFT and two practical applications:

1. Computation of convolution by two methods – direct method (in spatial

domain) and indirect method (in frequency domain).

2. Computation of edge enhancement by unsharp masking.

2. Preliminary report

1. Prove the following properties of the Continuous FT:

a. Linearity property,

b. Scaling property,

c. Rotation property,

2. Explain the purpose of Matlab’s command fftshift.

3. The magnitude of spectrum usually has a large dynamic range. How do you

display the magnitude of spectrum in Matlab?

Select a grayscale image of your choice (not of the supplied images) to be used as a

test case for the experiments and send it to your university (or other) web mail that

you can access from the lab.

3. Description of the experiment

Part 1 - DFT Properties

Use the supplied program dft_demo.m.

1. Observe the following properties and demonstrations:

Matlab’s fftshift command.

• Linearity.

Page 26: Digital Image Processing

Digital Image Processing Laboratory Work Book

• Scaling.

• Rotation.

• Exchange between magnitude and phase.

2. Test these properties and demonstrations with the image of your choice.

Part 2 – Convolution

Use the supplied program conv_demo.m.

1. Observe the implementation of convolution in two ways (in spatial domain and in

frequency domain).

2. Test the implementation of convolution in two ways with the image of your

choice.

3. Implement convolution with an averaging filter using Matlab’s commands

fspecial (‘average’ …) and imfilter (...).

Part 3 – Unsharp masking

Use the supplied program unsharp_masking_demo.m.

1. Test the unsharp masking algorithm with the image of your choice.

2. Implement unsharp masking using Matlab’s commands fspecial (‘unsharp’...)

and imfilter (...).

Page 27: Digital Image Processing

Digital Image Processing Laboratory Work Book

Lab 9 – Introduction to image restoration. 1. Introduction

In practical imaging systems the acquired image often suffers from effects of

blurring and noise. Image restoration algorithms are aimed to restore the original

undistorted image from its blurry and noisy version. The lab experiment

demonstrates the evolution of restoration algorithms from the simple Inverse Filter,

through its improved variant, the Pseudo Inverse Filter

to the Wiener Filter

where S ff (u,v) is the spectral density of the original signal and S nn (u,v) is the

spectral density of the noise.

Now let’s assume that the noise is white, that is, it has a constant spectral density

for all spatial frequencies:

where is a constant.

Furthermore, let’s assume that the spectral density of the original signal is in

inverse proportion to the square of the spatial frequency (the squared distance

from an origin in the frequency space):

that is,

Page 28: Digital Image Processing

Digital Image Processing Laboratory Work Book

where k is a constant.

Substituting two assumptions into the general formula of the Wiener Filter, we

obtain:

In the Preliminary report you will be asked to prove one more formula regarding

Wiener Filter.

2. Preliminary report

Prepare the following tasks and m-files and bring to the lab:

1. Derive the following formula for Wiener filter:

where is the variance of the noise and α is a constant. Derive a relation

between the constants k and α.

2. Explain the differences between Matlab commands randn and

imnoise(I,’gaussian’,...). Try to understand how the imnoise(I,’gaussian’,...)

command utilizes a randn command.

3. Implement in Matlab the image acquisition and degradation process:

a) Read image from file.

b) Blur the image using a filter of your choice.

c) Add Gaussian noise to the blurred image.

Hint: The variance argument of the imnoise(I,’gaussian’,...) command is

measured as a fraction of the full dynamic range available for the image –

[0, ... 255], so for this command it is recommended to use mean 0 and

variance argument 0.01 and multiples of 0.01. The “real” variance is

equal to variance argument multiplied by 255.

d) Write the resulting images to image files.

Page 29: Digital Image Processing

Digital Image Processing Laboratory Work Book

4. Implement the restoration filters mentioned above in Matlab, pay attention to

numerical accuracy issues.

Choose a grayscale image of your liking to be used for experimenting and send it to

your university (or other) web mail that you can access from the lab.

3. Description of the experiment

1. Inverse Filter.

a. Test the restoration with the Inverse Filter for deblurring and denoising.

b. What is the problem with the inverse filter ? How can this be solved ?

2. Pseudo Inverse Filter.

The Root Mean Square (RMS) error of restoration is defined in the following way:

where f (i, j) is the original image, fˆ (i, j) is the restored image and both

images are of size M × N .

a. Test the restoration with the Pseudo Inverse Filter for deblurring and

denoising.

b. Plot the graph of the RMS error (Y axis) versus the parameter ε (X axis) c. Now fix the parameter ε to the default value. Plot the graph of the Root Mean Square

(RMS) error of restoration (Y axis) versus the variance of the noise (X axis). Show the result of the best restoration.

d. For what maximal value of the variance of noise you still get an acceptable

restoration?

3. Wiener Filter. Assume that the variance used in the Wiener filter formula is equal to the variance of the noise

, and both of them are equal to 0.01∗255

a. Plot the graph of the Root Mean Square (RMS) error of restoration (Y-axis) versus

the parameter α (X axis). Show the result of the best restoration. b. Compare the result of the best restoration (optimal alpha in the RMS sense) with the

results obtained for non-optimal alpha. Compare the visual quality of the obtained

images. Do you think that RMS error is a good measure to analyze the visual quality of

images?

c. Now fix the parameter α to the default value .Change the variance of the noise to different values, (Keep the variance of the filter equal to the variance of noise.). For

what maximal value of the variance of noise you still get an acceptable restoration?

d. Observe the trade-off between edge preservation and denoising.

Lab10 – Introduction of principles of the JPEG baseline coding system.

Page 30: Digital Image Processing

Digital Image Processing Laboratory Work Book

1. Introduction

The JPEG standard provides a powerful compression tool used worldwide for different

applications. This standard has been adopted as the leading lossy compression standard

for natural images due to its excellent compression capabilities and its configurability. In

this lab we will present the basic concepts used for JPEG coding and experiment with

different coding parameters.

JPEG baseline coding algorithm (simplified version) [1]

The JPEG baseline coding algorithm consists of the following steps:

1. The image is divided into 8×8 non-overlapping blocks.

2. Each block is level-shifted by subtracting 128 from it.

3. Each (level-shifted) block is transformed with Discrete Cosine Transform (DCT).

4. Each block (of DCT coefficients) is quantized using a quantization table. The

quantization table is modified by the “quality” factor that controls the quality of

compression.

5. Each block (of quantized DCT coefficients) is reordered in accordance with a zigzag

pattern.

6. In each block (of quantized DCT coefficients in zigzag order) all trailing zeros

(starting immediately after the last non-zero coefficient and up to the end of the block)

are discarded and a special End-Of-Block (EOB) symbol is inserted instead in order to

represent them.

Remark: In the real JPEG baseline coding, after removal of the trailing zeros, each block is

coded with Huffman coding, but this issue is beyond our scope. Furthermore, in each

block all zero-runs are coded and not only the final zero-run. Each DC coefficient (the first

coefficient in the block) is encoded as a difference from the DC coefficient in a previous

block

a) (Programming) Implement in Matlab the Discrete Cosine Transform of a 1-D real

signal of length N ( N is even) via Discrete Fourier Transform. You have to write a

function X_dct = dct_new(x_sig),

where the input parameter is x_sig – a 1-D real signal of length N ( N is even) that has

to be transformed, and the output parameter is X_dct – the DCT of x_sig.

The use of a 'dct' command is prohibited. You are allowed to use an 'fft' command.

b) (Programming) Implement zigzag ordering of a pattern (matrix) of arbitrary size M ×

N. You have a M × N pattern and you put sequential numbers (indices) in its entries

Page 31: Digital Image Processing

Digital Image Processing Laboratory Work Book

filling row by row. Now you have to extract the indices from the pattern in zigzag

order. The order of zigzag must be according to the following figure – starting at the

upper left corner and moving down.

i.

In this Figure the parameters { M= 3, N= 4} were used.

You have to write a function Zig = zigzag(M, N),

where the input parameters are M – number of rows in a pattern, N – number of columns

in a pattern, and the output parameter is Zig - the row vector (of the size 1 x(MN)) that

contains the indices of a pattern in zigzag order.For example, for the Figure above the

function call

>> Zig = zigzag(3, 4) ;

has to yield the following vector: Zig = [1 5 2 3 6 9 10 7 4 8 11 12].

Test your program for small values of M and N. Make sure that your program works

correctly for the cases { M= 4, N= 4},{ M= 8, 8= N} and{ M=16,N=16 }. Make sure

that the result that you get for the case {M= 8, N= 8} coincides with the vector

“order” in the im2jpeg.m program.

Choose a grayscale image of your liking to be used for experimenting and send it to your

university (or other) web mail that you can access from the lab.

3. Description of the experiment

Load the image of Lena and your image. For each image perform the following tasks:

1. Compress the image using the JPEG compression algorithm (use the supplied

program im2jpeg.m). Restore the image from its compressed form using the supplied

program jpeg2im.m. Observe blocking effects in the restored image. You can use the

Page 32: Digital Image Processing

Digital Image Processing Laboratory Work Book

Magnifying Glass (Zoom In feature) in the Figure window.

2. Plot the graph of compression ratio (Y axis) versus a “quality” parameter of

im2jpeg.m (X axis). This graph defines the “operational point” of the algorithm.

Remark: the “quality” parameter defines the quality of compression and not the

quality of restoration. It is more (or equal) than 1. The “quality” equal to 1

corresponds to the best quality of restoration and the worst quality of compression.

3. The Root Mean Square (RMS) error of restoration is defined in the following way:

where f (i, j) is the original image, fˆ (i, j) is the restored image and both

images are of size M × N .

Plot the graph of Root Mean Square (RMS) Error of restoration (Y axis)

versus compression ratio (X axis). This graph is called Rate-Distortion curve.

4. Repeat questions 1 and 2 for block sizes 4× 4 and 16×16. Use the function zigzag.m that you wrote for the preliminary report. Compare blocking effects

for different block sizes. Compare the performance of the algorithm for

different block sizes.

5. Now work with a block size 8×8 . Repeat questions 1 and 2 for Discrete Fourier Transform and Hadamard Transform (instead of Discrete Cosine

Transform). Compare the performance of the algorithm for different

transforms.

Now work with a block size 8×8. Repeat questions 1 and 2 for Discrete Fourier Transform

and Hadamard Transform (instead of Discrete Cosine Transform). Compare the performance

of the algorithm for different transforms. Fourier Transform and Hadamard Transform.

Compare the performance of the algorithm for different transforms (Discrete Cosine

Transform, Discrete Fourier Transform and Hadamard Transform).

Page 33: Digital Image Processing

Digital Image Processing Laboratory Work Book

Lab11 – Introduction to Imadjust , IPT tool for intensity transformations of gray scale

images.

The simplest form of the transformation T is when the neighborhood in Fig.

is of size 1 X 1 (a single pixel). In this case, the value of g at (x, y) depends only on the

intensity of f at

that point, and T becomes an intensity gray-level transformation function. These two terms

are used

interchangeable when dealing with monochrome (i.e., gray-scale) images. When dealing

with color

images, the term intensity is used to denote color image components certain color spaces.

s = T(r)

where r denotes the intensity of f and s the intensity of g, both at any corresponding point

(x, y) in the images.

FUNCTION IMADJUST:

Function imadjust is the basic IPT tool for intensity transformations of gray scale images.

It has the syntax

g=[imadjust(f,[low_in_ high_in],[low_out high_out], gamma)

As illustrated in Fig 1,

Page 34: Digital Image Processing

Digital Image Processing Laboratory Work Book

This function maps the intensity values in image to new values in g, such that values

between low_in and high_in map to values between low_out and high_out. Values below

low_in and above high_in are clipped; that is, values below low_in map to low_out, and

those above high_in map to high_out. The input image can be of class uint8, uint16, or

double, and the output image has the same class as the input. All inputs to function

imadjust, other than f, are specified as values between 0 and 1, regardless of the class of f. If

f is of class uint8, imadjust multiplies unit16, values supplied by 255 to determine the actual

values to use; if f is of class uint16, the values are multiplied by 65535. Using the empty

matrix ([ ]) for [low_in high_in] or for [low_out high_out] results in the default values [0 1].

If high_out is less than low_out, the output intensity is reversed.

Parameter gamma specifies the shape of the curve that maps the intensity values in f to

create g. If gamma is less than 1, the mapping is weighted toward higher (brighter) output

values, as shown in above figure. If gamma is greater than 1, the mapping is weighted

toward lower (darker) output values. If it is omitted from the function argument, gamma

defaults to 1 (linear mapping).

• Figure 1(a) is a digital mammogram image, f, showing a small lesion, and Fig. 1(b) is the

negative image, obtained using the command

This process, which is the digital equivalent of obtaining a photographic negative, is

particularly useful for enhancing white or gray detail embedded in a large, predominantly

dark region. Note, for example, how much easier it is to analyze the breast tissue in Fig.

1(b). 1be negative of an image can be obtained also with IPT function imcomplement:

g=imcomplement (f)

• Figure 1 (c) is the result of using the command

>> g2=imadjust (f,[0.5 0.75],[0 1 ]);

Page 35: Digital Image Processing

Digital Image Processing Laboratory Work Book

Which expands the gray scale region between 0.5 and 0.75 to the full [0, 1] range. This type

of processing is useful for highlighting an intensity band of interest. Finally, using the

command

>>g3 = imadjust (f,[ ] ,[ ] ,2);

Produces a result similar to (but with more gray tones than) Fig. 1(c) by compressing the

low end and expanding the high end of the gray scale [see Fig. 1(d)].

Page 36: Digital Image Processing

Digital Image Processing Laboratory Work Book

Lab12 – Introduction to Histogram Processing and Function Plotting

Intensity transformation functions based on information extracted from image intensity

histograms playa basic role in image processing, in areas such as enhancement,

compression, segmentation, and description.

Generating and Plotting Image Histograms

The histogram of a digital image with L total possible intensity levels in range [0, G] is

defined as the discrete function

h(rk) = nK

where rk is the kth intensity level in the interval [0, G] and nK is the number pixels in the

image whose intensity level is rk' The value of G is 255 for images class uint8, 65535 for

images of class unit 16, and 1.0 for images of class daub Keep in mind that indices in

MATLAB cannot be 0, so rL corresponds to intensity level 0, r2 corresponds to intensity

level, and so on, with RL corresponding level G. Note also that G = L - 1 for images

of class uint8 and uint16.

The core function in the toolbox for dealing with image histograms is imhist, which has

the following basic syntax:

H=imhist(f,b)

Where f is the input image, h is its histogram,

h( rk), and b is the number of bins used in forming the histogram (if b is not included in

the argument, b = 256 is used by default). A bin is simply a subdivision of the intensity

scale. For example, if we are working with uint8 images and we let b = 2, then the

intensity is subdivided into two ranges: 0 to 127 and 128 to 255. The resulting histogram

will have two values: h (1) equal to the number of pixels in the image with values in the

interval [0,127], and h (2) equal to the number of pixels with values in the interval

[128,255]. We obtain the normalized histogram simply by using the expression

p= imhist (f, b)/numel(f)

Function numel (f) gives the number of elements in array f (i.e., the number of pixels in

the image)

• Consider the image, f. The simplest way to plot its histogram is to use imhist with no

output specified:

>> imhist (f);

Page 37: Digital Image Processing

Digital Image Processing Laboratory Work Book

Figure shows the result.

This is the histogram display default in the toolbox. However, there are many other ways

to plot a histogram, and we take this opportunity to explain some of the plotting options in

MATLAB that are representative of those used in image processing applications.

Histograms often are plotted using bar graphs. For this purpose we can use the function

Bar (horz,v,width)

Where v is a row vector containing the points to be plotted, horz is a vector of the same

dimension as v that contains the increments of the horizontal scale, and width is a number

between 0 and 1. If horz is omitted, the horizontal axis is divided in units from 0 to length

(v). When width is 1, the bars touch; when it is 0, the bars are simply vertical lines as in

Fig.1 (a). The default value is 0.8. When plotting a bar graph, it is customary to reduce the

resolution of the horizontal axis by dividing it into bands. The following statements

produce a bar graph, with the horizontal axis divided into groups of 10 levels

Page 38: Digital Image Processing

Digital Image Processing Laboratory Work Book

Figure 1(b) shows the result. The peak located at the high end of the intensity scale in Fig.

1(a) is missing in the bar graph as a result of the larger horizontal increments used in the

plot.

The fifth statement in the preceding code was used to expand the lower range of the

vertical axis for visual analysis, and to set the horizontal axis to same range as in Fig.1 (a).

The axis function has the syntax

Axis ([horzmin horzmax vertmin vertmax])

This sets the minimum and maximum values in the horizontal and vert axes. In the last

two statements, gca means "get current axis,"

Stem Graph:

A stem graph is similar to a bar graph. The syntax is

Stem(horz,v,’color_linestyle_marker’,’fill’)

where v is row vector containing the points to be plotted, and harz is as described for bar.

The argument,

’color_linestyle_marker’

is a triplet of values from Table . For example, stem (v, ‘r- -s’) produces a tern plot where

the lines and markers are red, the lines are dashed, and the markers are squares. If f ill is

used, and the marker is a circle, square, or diamond, the marker is filled with the color

specified in color. The default color black, the line default is solid, and the default marker

is a circle. The stem graph in Fig.1(c) was obtained using the statements

» h = imhist (f);

» h1 = h (1 : 10: 256) ;

>> horz=1:10:256;

>> stem(horz,h1, 'fill')

>> axis ([0 255 0 15000])

>> set (gca, ‘xtick', [0: 50: 255] )

>> set ( gca,’ytick', [0:2000:15000])

Function plot which plots a set of points by linking them with straight lines. The syntax is

Plot (horz, v, 'color', 'g' ,line style, 'none', 'marker', 's )

The plot graph in Fig.1(d) was obtained using the statements

» h = imhist (f);

» plot (h)

>> horz=1:10:256;

>> axis ([0 255 0 15000])

>> set (gca, ‘xtick', [0: 50: 255] )

>> set ( gca,’ytick', [0:2000:15000])

Page 39: Digital Image Processing

Digital Image Processing Laboratory Work Book

Lab13 – Computing and Visualizing the 2-D DFT in Matlab.

Introduction:

The DFT and its inverse are 'obtained in practice using a fast Fourier transform (FIT)

algorithm. The FFT of an M X N image array f is obtained in the toolbox with function

fft2, which has the simple syntax:

F=fft2(f)

This function returns a Fourier transform that is also of size M X N, with the data arranged

in the form with the origin of the data at the top left, and with four quarter periods meeting

at the center of the frequency rectangle.

It is necessary to pad the input image with zeros when the Fourier transform is used for

filtering. In this case, the syntax becomes

F=fft2(f,P,Q)

Visual analysis of the spectrum by displaying it as an image is an important aspect of

working in the frequency domain. As an illustration, consider the simple image, f, in Fig.

(a). We compute its Fourier transform and display the spectrum using the following

sequence of steps:

>> F=fft2(f);

>> S =abs(F);

>>imshow(S , [ ])

Figure (b) shows the result. The four bright spots in the corners of tlit image are due to the

periodicity property.

Page 40: Digital Image Processing

Digital Image Processing Laboratory Work Book

The following commands perform filtering without padding:

» [M, N] = size(f);

» F = fft2(f);

» sig = 10;

»H=lpfilter( 'gaussian', M, N, sig);

» G = H.*F;

» g = real(ifft2(G));

»imshow(g, [])