Summer Internship Final Report
-
Upload
aravind-n-kumar -
Category
Documents
-
view
104 -
download
4
Transcript of Summer Internship Final Report
Page 1 of 122
Summer Internship Report
Topic: IMAGE PROCESSING
Operated at:
Department of Electrical & Electronics Engineering
Indian Institute of Technology Guwahati
Submitted by : Aravind N Kumar
BL.EN.U4EEE13006
Amrita School of Engineering Bengaluru
Page 2 of 122
IMAGE PROCESSING
AN INTERNSHIP REPORT
Submitted by
ARAVIND N KUMAR
BL.EN.U4EEE13006
in partial fulfillment of the award of the degree
of
BACHELOR OF TECHNOLOGY
IN
ELECTRICAL & ELECTRONICS
ENGINEERING
AMRITA SCHOOL OF ENGINEERING, BANGALURU
AMRITA VISHWA VIDYAPEETHAM
BANGALURU-560035
Page 3 of 122
ACKNOWLEDGEMENT
At first I would like to thank Dr. M.K.Bhuyan Associate Professor Department of EEE IIT
Guwahati for guiding me throughout this project so that the results could be achieved.
I express my sincere regards to the Department of Electrical & Electronics' Indian Institute
of Technology for providing the world class infrastructure and the resources for the internship.
I would take this opportunity to thank Dr. S. Ganguly Coordinator Summer Internship,
Department of EEE, IIT Guwahati for guiding me through the application process of
internship .
I would like to convey my thankfulness to Dr. Rakesh S. G., Associate Dean, Amrita school of
Engineering, Bangalore.
I also take this opportunity to thank Dr. Ravishankar S. Chairman, Electrical and Electronics
Engineering Department, for his motivation and valuable help during the project work. I would
be obliged to the support rendered by two teachers Mrs Sreekala M. Associate Professor & Mrs.
K.Sireesha Associate Professor department of EEE Amrita School of Engineering Bengaluru. I
express my sincere thanks to all the staff of Department of EEE for their kind help and
cooperation.I would also like to thank the Corporate & Industrial Relations (CIR)
department Amrita School of Engineering Bengaluru for providing support and encouragement
to take up this opportunity.
Above all, I thank my parents and the almighty for giving me the strength, courage and blessings
to complete this project.
Page 4 of 122
Table of Contents
1. Introduction ......................................................................................................................................... 6
1.1Image Processing ............................................................................................................................ 6
1.1.1Image Acquisition & Sampling .................................................................................................... 6
1.1.2 Sources of Image ........................................................................................................................ 6
1.1.3 Images and Digital images .......................................................................................................... 7
1.1.4Aspects of Image Processing ........................................................................................................ 8
1.1.4 Types of Digital Images .............................................................................................................. 9
2.Image Processing Toolbox .................................................................................................................. 10
2.1 Digital Image Representation ....................................................................................................... 10
2.2Coordinate Conventions ................................................................................................................ 10
2.3 Images as Matrices....................................................................................................................... 11
2.4 Reading Images ........................................................................................................................... 11
2.5 Displaying Images ....................................................................................................................... 12
2.6 Compression Ratio ....................................................................................................................... 12
2.7 Data Classes ................................................................................................................................ 13
2.7 Image Types ................................................................................................................................ 14
2.8 Converting between Image Classes and Types ............................................................................. 15
2.9 Array Indexing............................................................................................................................. 16
2.10 Matrix Indexing ......................................................................................................................... 17
2.11Arithmetic Operators .................................................................................................................. 18
2.12 A Model of the Image DegradationIRestoration Process ............................................................. 19
2.13 Adaptive Spatial Filters .............................................................................................................. 20
2.14 Histogram Equalization: ............................................................................................................. 21
2.15 Image Processing Toolbox Standard Spatial Filters .................................................................... 22
2.16 Nonlinear Spatial Filters............................................................................................................. 23
2.17 Lossless and lossy compression .................................................................................................. 24
2.18Discrete Cosine Transform .......................................................................................................... 24
2.18Canny Edge Detection ................................................................................................................ 26
2. Matlab & C Program Implementation ................................................................................................ 28
2.1 Image Arithmetic in MATLAB with example .......................................................................... 28
2.2 To write an m-function that computes the average intensity of an image. ...................................... 32
Page 5 of 122
2.3 M-function that multiplies two input images and outputs the product of the images, the maximum
and the minimum values of the product are computed and a normalized product image whose values
are in the range [0,1] .......................................................................................................................... 33
2.3 A matlab code to compute the ratio of the bytes in the two images. .............................................. 35
2.4 Write an M-function to compute the implementation of the following two dimensional image
function using for loops and vectorization. ......................................................................................... 37
f(x,y)=asin(u0x+v0y) .......................................................................................................................... 37
for x= 0,1,2,….M-1 and y=0,1,2,….N-1............................................................................................. 37
2.5To corrupt the original lenna image with impulse (Salt & Pepper ) noise of density 0.01.Attempt to
minimize the noise effects by using an appropriate spatial domain filter. ............................................ 39
2.6 To generate a bimodal Gaussian function. .................................................................................... 42
2.7 To extract a rectangular sub image from an image. ....................................................................... 44
2.8 To find the entropy of an image matrix........................................................................................ 45
2.9 To write an m function for Intensity Transformation, Intensity Scaling ......................................... 46
2.10 Program to find the error between two images/matrices. ............................................................. 51
2.11 To perform histogram equilisation. ............................................................................................. 52
2.12 Write a MATLAB code to perform the contrast stretching on the image. Show the original and
stretched image along with their histograms. ...................................................................................... 56
2.13To plot the histogram and bar graph of an image. ........................................................................ 58
2.14Coorrupt the original lenna of the image with a zero mean Gaussian noise,so that the SNR of the
noisy image is 5 dB.Attempt to minimize noise effects by using appropriate filters in spatial domain
and spectral domain. Indicate the cutoff frequency of the spectral domain filter.................................. 63
2.15 To Generate a 3X3 spatial averaging filter and to perform neighborhood averaging.To plot and
observe using fast Fourier transform .................................................................................................. 79
2.16 Read some images and convert them to binary images. Then to show/implement all the binary
image processing technique. .............................................................................................................. 87
2.17 To read an image having some textures and form the co-occurrence matrix. ............................... 89
2.18 (a)-To Load a test image and find the edges by gradient –based and Laplacian-based technique. 91
2.18 (b)-Canny Edge Detection .......................................................................................................... 92
2.19 Lossy Image Compression ....................................................................................................... 104
2.20 Capture a video footage of 1 min duration by a digital camera(Mega Pixels>7.0) and convert it to
frames(25frames/second) and read all the frames one by one .Then detect the edges of the moving
objects in the frame.......................................................................................................................... 116
References ....................................................................................................................................... 121
Page 6 of 122
Fundamentals Of Image Processing
1. Introduction
1.1Image Processing
Image Processing involves changing the nature of an image in order to either
1. Improve its pictorial information for human interpretation, or
2. Render it more suitable for autonomous machine perception
Digital image Processing involves using a computer to change the nature of the digital image.
The process of image processing in practical scenario include performing operations on image
such as edge detection & sharpening, removing noise from the image, removing motion blur, to
remove unnecessary information and obtain the coarse structure of the image.
1.1.1Image Acquisition & Sampling Sampling: It refers to the process of digitizing a continuous function. Sampling involves three
different types of sampling under sampling, critical sampling & oversampling.
Undersampling:In this condition the image is sampled at such a rate that it is not possible to
reconstruct the original function/image.
To ensure they have enough sampling points, we require that the sampling period is not greater
than one half the finest details in the function this can be explained by use of a criterion known
as the Niquist criterion.
Niqiust criterion can be simply explained as the sampling theorm,which says in effect that a
continuous function can be reconstructed from its samples provided that the sampling frequency
is at least twice the maximum frequency in the function. Here we consider image as our
continuous function of two variables and we wish to sample it to produce a digital image.
The procedure of under sampling often leads to a phenomenon called aliasing, the result is we
loose from useful information from the image and often the final resolution of the image is
affected.
1.1.2 Sources of Image Light is the predominant source of energy for images. Many digital images are captured using
visible light as the energy source this has the advantage of being safe ,cheap, easily detected and
Page 7 of 122
readily processed with suitable hardware. Two popular methods of producing a digital image are
digital camera and flat bed scanner.
CCD camera stands for “charge-coupled device". This is an array of light sensitive cells called
“photo sites” each of which produces a voltage proportional to the intensity of light falling on
them. Colors are obtained by the use of the red, green and blue filters.CCDs are used in most
digital cameras as they produce good results, can be made high resolution with robust noise. A
complimentary technology is the use of CMOS(Complementary Metal Oxide
Semiconductors),They have advantage of being cheaper to provide and require less power than a
CCD chip, but they are more susceptible to noise.
For a camera attached to a computer it is more efficient and convenient to output to a convenient
storage medium using a card called frame grabbing card. Large number of images are copied to a
permanent storage.
The output image will be an array of values, each representing a sampled point from the original
scene. The elements of this array are called picture elements or simply pixels.
Flat bed scanners work on the principle similar to the CCD camera instead of the entire image
being captured at once the on a large array a single row of photo sites is moved across the image
capturing the entire row as it moves.
Other sources include various components of the electromagnetic spectrum. Visible light is a part
of the electromagnetic spectrum. For microscopy we use x-rays or electron beams. An additional
method of obtaining images is by the use of x-ray tomography, where an object is encircled by
an x-ray beam. The image of the beam is detected such an image is called tomogram. In CAT
scan (computed axial tomography) scan the patient lies within a tube around which the x-ray is
fired. This enables large sections of topographic images in 3D view.
1.1.3 Images and Digital images
If a monochromatic photo is taken the image will have a 2D function where the function velues
give the brightness of the image at any given point. The brightness and darkness values range
between a maxima and a minimal.
**digital image differs from a photo in which all values are discrete.
A digital image can be considered as a large array of sampled points from the continuous image,
each point with a quantized brightness these points are called pixels, which constitute the digital
image. The matrix points around the pixel of consideration form the neighborhood of the pixel.
Applications
Page 8 of 122
Enormous applications
1. Medicine-Xrays, MRI or CAT scans
2. Analysis of cell image or chromosome karyotypes.
3. Agriculture-Satellite imaging of land, vegetation.
4. Industry-Inspection of item in a production line
5. Law enforcement-Fingerprint analysis, sharpening blurring of speed camera images.
1.1.4Aspects of Image Processing
There are different image processing algorithms such as
IMAGE ENHANCEMENT : Processing an image so that the result is more suitable for a
particular application is called image enhancement.
Sharpening or deblurring an out of focus image
Highlighting edges
Improving images contrast or brightening an image
Removing noise
IMAGE RESTORATION: An image may be restored by the damage done to it by an
unknown cause.
Removing of blur caused by linear motion
Removal of optical distortion
Removal of periodic interference
IMAGE SEGMENTATION: Segmentation involves subdividing an image into constituent
parts or isolating certain aspects of an image including
Finding lines, circles or particular shapes in an image and
Identifying cars,trees,buildings roads in an aerial photograph
Image Processing task
Page 9 of 122
1 Acquiring the image through a CCD or flat bed scanner.
2 Preprocessing is the task in which it is checked whether the image is compatible for the
processing operations to be performed.
3 Segmentation is performed to check which part of the image has to be extracted.
4 Representation & description the process of extraction allows us to differentiate between
objects.
5 Recognition and Interpretation means to assign labels to objects based on their
description’s and assigning meanings to those labels.
1.1.4 Types of Digital Images
1 Binary:Each pixel is either just black or white.Ther are only two possible values for a pixel
we need only 1 bit per pixel.Such images therefore be very efficient in terms of storage .
Applications: text , fingerprints, architectural plans
2 Greyscale Each pixel is a shade of gray,normally from 0(black) to 255(white).It can be
represented in 8bits.
3 True color or red-blue-green:Here each pixel has a particular color, that being described
by blue,green,red in it.If each of these components range from 0 to 255 this gives a total of
255^3 different possible colors in an image.It is a stack of three matrices red,green and blue.
4 Indexed :Most of color images have only a small subset of the more than16 million possible
colors.For the conveience and storage the image has been associated with color maps or color
palatte which is simply a list of colors in that image,each pixel has a value which doesnot give its
color but the index to the color in the map.
It is possible for an image to have 256 colors or fewer but it will only require 1 byte to
store.Image formats like GIF onl allow 256 or fewer colors in the image.
Image Size
1 kb = 1000 bytes
1 mb = 1 million bytes
Binary images contain only one layer,greyscale contains 2 layers and color images contain 3
layers using the respective pixel resolution multiplied with the no of layesa gives the size.
Page 10 of 122
2.Image Processing Toolbox
Matlab gives the users the power to compute extensively the Image processing using certain
functions in the Image processing toolbox.The image processing tool box is a collection of
functions which gives MATLAB extended capablity other than the numerical capablity.
2.1 Digital Image Representation
An image may be defined as a two-dimensional function, f (x, y), where x and y are spatial
(plane) coordinates, and the amplitude of f at any pair of coordinates (x, y) is called the intensity
of the image at that point.The term gray level is used often to refer to the intensity of
monochrone images. Color images are formed by a combination of individual 2-D images. For
example, in the RGB color system, a color image consists of three (red, green, and blue)
individual component images. For this reason, many of the techniques developed for
monochrome images can be extended to color images by processing the three component images
individually.The conversion of an image into the digitized form requires sampling along the two
axes ,the digitization along the x axis is called sampling & the digitization along the y axis is
called quantisation.
2.2Coordinate Conventions The result of sampling and quantization is a matrix of real numbers.
Fig2.2.1 :The standard form of representation of images in Image
Processing
Assume that an image f(x, y) is sampled so that the resulting image has M rows and N columns.
We say that the image is of size M x N. The values of the coordinates (x, y) are discrete
quantities.Mostly the matlab IPT tool box refers to this system as pixel distribution but less
frequently, the toolbox also employs another coordinate convention called spatial coordinates,
which uses x to refer to columns and y to refers to rows. This is the opposite of our use of
variables x and y.
Page 11 of 122
2.3 Images as Matrices
A digital image can be represented naturally as a MATLAB matrix,where each element in the
matrix can be termed as a pixel.
Fig2.3.1: The matrix representation of the image
Matrices in MATLAB are stored in variables with names such as A, a, RGB, real-array, and so
on. Variables must begin with a letter and contain only letters, numerals, and underscores.
2.4 Reading Images
The general syntax for reading images is in the form
f=imread('filename');
Supported file formats are as shown in the table below:
Fig2.3.2: Supported formats in matlab
Function size gives the row and column dimensions of an image:
>> s i z e ( f )
Used in the following form to determine automatically the size of an image:
Page 12 of 122
>> [M, N ] = s i z e ( f ) ;
This syntax returns the number of rows (M) and columns (N) in the image.
The function whos f gives the additional information about the variable name, type ,size of the
variable etc.
2.5 Displaying Images
Images are displayed on the MATLAB desktop using function imshow, which has the basic
syntax:
To keep the first image and output a second image, we use function figure as follows:
>> figure, imshow(g)
Using the statement
>> imshow(f), figure, imshow(g)
Writing Images
Images are written to disk using function imwrite, which has the following basic syntax:
imwrite(f, 'filename')
In order to get an idea of the compression achieved and to obtain other image file details, we can
use function imf inf o, which has the syntax
imfinfo filename
2.6 Compression Ratio
This compression ratio was achieved while maintaining image quality consistent with the
requirements of the application.In addition to the obvious advantages in storage space, this
reduction allows the transmission of approximately 35 times the amount of uncompressed data
per unit time.
The information fields displayed by i m f i n f o can be captured into a so called structure
variable that can be used for subsequent computations.
Page 13 of 122
As an illustration, consider the following use of structure variable K to compute
the compression ratio for bubbles25. j pg:
>> K = imfinfo('bubbles25.jpg');
>r image-bytes = K.Width*K.Height*K.BitDepth/8;
>> compressed-bytes = K.FileSize;
>> compression-ratio = image-bytes/compressed_bytes
compression-ratio =
35.1612
More control over export parameters is obtained by using the print command:
print -fno -dfileformat -rresno filename print
where no refers to the figure number in the figure window of interest, fileformat refers to one of
the file formats
2.7 Data Classes
All numeric computations in MATLAB are done using double quantities, so this is also a
frequent data class encountered in image processing applications.The first eight entries in the
table are referred to as numeric data classes.The ninth entry is the char class and, as shown, the
last entry is referred to as the logical data class.
Data class double requires 8 bytes to represent a number, uint8 and i n t a require 1 byte each,
uintl6 and i n t l 6 require 2 bytes, and uint32,int32, and s i n g l e , require 4 bytes each.The char
data class holds characters in Unicode representation. A character string is merely a 1 x n array
of characters.A l o g i c a l array contains only the values 0 and 1, with each element being stored
in memory using one byte per element.
Page 14 of 122
Fig2.7.1: Different types of data types being used are as in the table above
2.7 Image Types The toolbox supports four types of images:
Intensity images
Binary images
Indexed images
RGB images
Most monochrome image processing operations are carried out using binary or intensity images,
so our initial focus is on these two image types.
Intensity Images
An intensity image is a data matrix whose values have been scaled to represent intensities. When
the elements of an intensity image are of class uint8, or class uint 16, they have integer values in
the range [O, 2551 and [O, 65.5351], respectively. If the image is of class double, the values are
floating-point numbers.
Page 15 of 122
Binary Images
A binary image is a logical array of 0s and Is. Thus, an array of 0s and Is whose values are of
data class, say, uint8. is not considered a binary image in MATLAB. A numeric array is
converted to binary using function l o g i c a l.
Thus, if A is a array consisting of 0s and Is, we create a logical array B using the statement
To test whether the array given is binary or not
Converting between Data Classes
Converting between data classes is straightforward. The general syntax is
2.8 Converting between Image Classes and Types It is necessary to convert the image datatype from one format to another inorder to compute
operations so to allow this the IPT tool box provides a certain set of fuctions such as
im2double,im2uint8etc.
Fig2.8.1: This table shows the various functions involved in the image data conversion from one
form to another
Converting an arbitrary array of class double to an array of class double scaled to the range [O,
1] can be accomplished by using function mat2gray whose basic syntax is
Function im2double converts an input to class double. If the input is of class uint8, uintl6, or l o
g i c a l , function im2double converts it to class double with values in the range [0, 1].
Page 16 of 122
We consider conversion between binary and intensity image types. Function im2bw, which has
the syntax
2.9 Array Indexing MATLAB supports a number of powerful indexing schemes that simplify array manipulation
and improve the efficiency of programs. In this section we discuss and illustrate basic indexing
in one and two dimensions (i.e., vectors and matrices).
The elements of such a vector are accessed using one-dimensional indexing.Thus, v (1 ) is the
first element of vector v, v (2) its second element, and soforth. The elements of vectors 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 =
.
5
A row vector is converted to a column vector using the transpose operator ( . ' ):
>> w = v.'
W =
1
3
5
7
9
To access blocks of elements, we use MATLAB's colon notation. For example, to access the first
three elements of v we write
Page 17 of 122
Function linspace, with syntax
generates a row vector x of n elements linearly spaced between and including a and b.
2.10 Matrix Indexing Matrices can be represented conveniently in MATLAB as a sequence of row vectors enclosed by
square brackets and separated by semicolons. For example, typing
>>A=[1 2 3 ; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
We select elements in a matrix just as we did for vectors, but now we need two indices: one to
establish a row location and the other for the corresponding column.
>> A(2, 3)
ans =
6
The colon operator is used in matrix indexing to select a two-dimensional block of elements out
of a matrix. For example,
Page 18 of 122
Here, use of the colon by itself is analogous to writing A( 1 : 3 , 3 ) , which simply picks the third
column of the matrix. Similarly, we extract the second row as follows:
2.11Arithmetic Operators
MATLAB has two different types of arithmetic operations. Matrix arithmetic perations are
defined by the rules of linear algebra. Array arithmetic operations are carried out element by
element and can be used with multidimensional arrays. The period (dot) character (.)
distinguishes array operations from matrix operations. For example, A*B indicates matrix
multiplication in the traditional sense, whereas A. *B indicates array multiplication, in the sense
that the result is an array, the same size as A and 6, in which each element 1s the of
corresponding elements of A and B. In other words, if C = A. *B,then C ( I , J ) = A ( I , J ) "6 ( I
, J ) . Because matrix and array operations are the same for addition and subtraction, the
character pairs . + and .- are not used.
Fig:2.11.1 Flow Control
Page 19 of 122
Fig:2.11.2 Image Restoration
2.12 A Model of the Image DegradationIRestoration Process The degradation process is modeled in this chapter as degradation function that, together with an
additive noise term, operates on an input image f ( x , y ) to produce a degraded image g(x, y):
g(x3y) = H [ f ( x , y ) l + V ( X, Y )
If His a linear, spatially invariant process, it can be shown that the degraded & I image is given
in the spatial domain by
g(x>Y ) = h( x7Y ) * f ( x ,Y ) + V ( X ,Y )
Noise Models
Adding Noise with Function imnoise
Page 20 of 122
the basic syntax
g = imnoise(f, type, parameters)
2.13 Adaptive Spatial Filters In certain cases adaptive filters are designed which can act based on the behaviour of the filters
at the time of image restoration which include the following types.
zmin = minimum intensity value in S,y, filtering.
zmax = maximum intensity value in Sxy
zmed = median of the intensity values in S,,
zxy = intensity value at coordinates ( x , y)
The syntax is
f = adpmedian(g, Smax)
f , corrupted by salt-and pepper noise, generated using the command
Page 21 of 122
g=imnoise(f, ' s a l t & pepper', .25);
With the use of the median filter we can filter out the noise which has corrupted the lenna of the
image, the syntax is as follows:
f = medfilt2(g, [7 7], 'symmetric');
Intensity Transformation Functions
The simplest form of the transformation T is when the neighborhood in is of size 1 X 1 (a single
pixel). In this case, the value of g at (x, y) depends only on the intensity o f f at that point, and T
becomes an intensity or gray-level transformation function. These two terms are used
interchangeably, when dealing with monochrome (i.e., gray-scale) images.
Function imadjust
Function imadjust is the basic IPT tool for intensity transformations of grayscale images. It has
the syntax
g = imadjust(f, [low-in high-in], [low-out high-out], gamma)
this function maps the intensity values in image f to new values in g, such that values between
low-in and high-in map to low-in high-in low-in high-in slues 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 thoseabove 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 puts to function imad j ust, other than f,
are specified as values between 0 1, regardless of the class of f. If f is of class uint8, imad j ust
multiplies values supplied by 255 to determine the actual values to use; if f is of class uint 1 6,
the values are multiplied by 65535. Using the empty matrix ([ 1) for [low-in high-in] or for [low-
out high-out] results in the default values 0 1 1. If high-out is less than low-out, the output
intensity is reversed.
2.14 Histogram Equalization:
Histogram equalization is used to enhance contrast. It is not necessary that contrast will always
be increase in this. There may be some cases were histogram equalization can be worse. In that
cases the contrast is decreased.Intensity transformation functions based on information extracted
from image intensity histograms play a basic role in image processing, in areas such as
enhancement, compression, segmentation, and description.
The core function in the toolbox for dealing with image histograms is
imhist, which has the following basic syntax:
h = imhist (f , b)
Page 22 of 122
The 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.
We obtain the normalized histogram simply by
p = imhist(f, b)/numel(f);
The simplest way to plot its his- EXAMPLE 3.4:
gram is to use imhist with no output specified:
The histogram display default in the tool. However, there are many other ways to plot a
histogram, and we take this oppurtunity to explain some of the plotting options in MATLAB that
are repsentative cof those used in image processing applications.
Histograms often are plotted using bar graphs. For this purpose we can use
bar(horz, v, w i d t h )
Histogram equalization is implemented in the toolbox by function histeq:
which has the syntax
g = histeq(f, nlev)
where f is the input image and nlev is the number of intensity levels specified for the output
image. If nlev is equal to L (the total number of possible levels in the input image), then histeq
implements the transformation function, T(rk), directly. If nlev is less than L, then histeq
attempts to distribute the levels so that they will approximate a flat histogram. Unlike imhist, the
default value in histeq is nlev = 64. For the most part, we use the maximum possible number of
levels (generally 256) for nlev because this produces a true implementation of the histogram-
equalization method just described.
2.15 Image Processing Toolbox Standard Spatial Filters
Linear Spatial Filters
The toolbox supports a number of predefined 2-D linear spatial filters, obtained by using
function f special, which generates a filter mask, w, using the syntax
w = fspecial ( ' typ l, parameters)
Page 23 of 122
where ' t y p e ' specifies the filter type, and parameters further define the specified filter. The
spatial filters supported by f special are summarized in table , including applicable parameters
for each filter.
Fig:2.12.1 Syntax and parameters of different filters
2.16 Nonlinear Spatial Filters
generates order-statistic filters (also called rank filter).
These are nonlinear spatial filters whose response is based on ordering (ranking) the pixels
contained in an image neighborhood and then replacing th ecall that the median. 5, of a set of
values is such that half the values in the set are less than or equal value of the center pixel in the
neighborhood with the value determined by the ranking result.
Because of its practical importance, the toolbox provides a specialized implementation of the 2-
D median filter:
Page 24 of 122
g = medf ilt2( f , [m n ] , padopt) k
where the tuple [m n] defines a neighborhood of size m x n over which median is computed, and
padopt specifies one of three possible padding options: ' zeros ' (the default), ' symmetric ' in
which f is extended symmetrically by mirror-reflecting it across its border, and 'indexed '
which f is padded with 1s if it is of class double and with 0s otherwise.The fault form bf this
function is 3
g = m e d f l l t 2 ( f ) i
which uses a 3 X 3 neighborhood to compute the median, and pads the border
of the input with 0s.
2.17 Lossless and lossy compression
Lossless and lossy compression are terms that describe whether or not, in the compression of a
file, all original data can be recovered when the file is uncompressed. With lossless compression,
every single bit of data that was originally in the file remains after the file is uncompressed. All
of the information is completely restored. The Graphics Interchange File (GIF) is an image
format used on the Web that provides lossless compression.
On the other hand, 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 (although the user may not notice it). 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.
Image compression systems are composed of two structural blocks an encoder and a
decoder.Image f(x,y) is fed into the encoder,which creates a set of symbols from input data and
uses them to represent the image.If let n1,n2 denote the number of information carrying units in
the original and encoded images respectively,the compression that is achieved can be quantified
numerically via the compression ratio.
Cr=n1/n2
2.18Discrete Cosine Transform
The discrete cosine transform (DCT) represents an image as a sum of sinusoids of varying
magnitudes and frequencies. The dct2 function computes the two-dimensional discrete cosine
transform (DCT) of an image. The DCT has the property that, for a typical image, most of the
Page 25 of 122
visually significant information about the image is concentrated in just a few coefficients of the
DCT. For this reason, the DCT is often used in image compression applications. For example,
the DCT is at the heart of the international standard lossy image compression algorithm known
as JPEG. (The name comes from the working group that developed the standard: the Joint
Photographic Experts Group.
The two-dimensional DCT of an M-by-N matrix A is defined as follows
The values Bpq are called the DCT coefficients of A.
The DCT is an invertible transform, and its inverse is given by
The inverse DCT equation can be interpreted as meaning that any M-by-N matrix A can be
written as a sum of MN functions of the form
These functions are called the basis functions of the DCT. The DCT coefficients Bpq, then, can
be regarded as the weights applied to each basis function.
In accordance inorder to subdivide an image into blocks we use certain functions in matlab such
as blockproc() the general syntax of this function is as follows:
B = blkproc(A, [M N ] , FUN, PI, P2, . . . )
Page 26 of 122
B = im2col(A, [ M N ] , ' d i s t i n c t ' )
2.18Canny Edge Detection
The purpose of edge detection in general is to significantly reduce the amount of data in an
image, while preserving the structural properties to be used for further image processing.
The aim of JFC was to develop an algorithm that is optimal with
regards to the following
criteria:
1.Detection:
The probability of detecting real edge points should be maximized while the probability of
falsely detecting non-edge points should be minimized. This corresponds to maximizing the
signal-to-noise ratio.
2. Localization:
The detected edges should be as close as possible to the real edges.
3.Number of responses:
One real edge should not result in more than one detected edge (one can argue that this is
implicitly included in the first requirement). With JFC’s mathematical formulation of these
criteria, Canny’s Edge Detector is optimal for a certain class of edges (known as step edges).
The Canny Edge Detection Algorithm
The algorithm runs in 5 separate steps:
1.Smoothing:
Blurring of the image to remove noise.
2.Finding gradients:
The edges should be marked where the gradients of the image has large magnitudes.
3. Non-maximum suppression:
Only local maxima should be marked as edges.
Page 27 of 122
4. Double thresholding:
Potential edges are determined by thresholding.
5. Edge tracking by hysteresis:
Final edges are determined by suppressing all edges that are not connected to a very certain
(strong) edge
Page 28 of 122
2. Matlab & C Program Implementation
2.1 Image Arithmetic in MATLAB with example
Image Arithmetic
An image is represented in a matrix format. To perform image arithmetic the size of the two
matrices should be same. The operation on two images results in a new image. Consider two
images A and B with same size.
Image Addition
In a RGB image, the addition of two images can be done using the ‘+’ operator. C=A+B;
Here, the minimum value of A+B and 255 is taken.
(i.e) C(i,j,1)=min(A(i,j,1)+B(i,j,1),255) where (i,j) represents the pixel position.
Image addition can be used to add the components from one image into other image.
Program Code
B=imread('jerry.png')
C=imread('tom2.png')
D=C+B;
imshow(D)
Input : The image pixel size is 320 X 320
Page 29 of 122
Output
D(i,j,1)=min(B(i,j,1)+C(i,j,1),255) where (i,j) represents the pixel position.
Image addition can be used to add the components from one image into other image.
Image Multiplication
Image multiplication is used to increase the average gray level of the image by multiplying with
a constant.
It is used for masking operations.
C=A.*B;
Program Code
B=imread('jerry.png')
C=imread('tom2.png')
D=C.*B;
imshow(D)
Output:
Page 30 of 122
Image Division
Image division can be considered as multiplication of one image and the reciprocal of other
image.
C=A.\B;
Logical Operations:
Logical operations are done on pixel by pixel basis.
The AND and OR operations are used for selecting subimages in an image.
This masking operation is referred as Region of Interest processing.
Logical AND
To isolate the interested region from rest of the image portion logical AND or OR is used.
Consider a mask image L for the image A.
To obtain the interested area, D= and(L,A) ;
We can use L&A also.
The resulting image will be stored in D which contains the isolated image part.
Program Code
B=imread('jerry.png')
C=imread('tom2.png')
D= and(B,C) ;
Page 31 of 122
%the output is logical array of zeros and ones the input array becomes logical and binary hard to
display in @ 2D
Logical OR
Syntax: D= or(L,A). We can also use L|A
To find the MAXIMUM or MINIMUM values
PROGRAM CODE:
B=imread('jerry.png')
C=imread('tom2.png')
D=C.*B;
>>maxP = max(P(:));
>> E
E =
1
>> F=min(D(:));
>> F
F =
0
Aim
To wirte a program to vertically flip an image.
Code:
>> fp=imread('tom2.png');
>> fc = fp(end: -1 : 1 , : );
>> imshow(fc)
>> figure,imshow(fp)
Page 32 of 122
Input Output
2.2 To write an m-function that computes the average intensity of an
image.
Code:
>> A=imread('jerry.png');
>> n=numel(A)
n =
307200
>> av=sum(A(:))/numel(A)
av =
38.4374
>>whos A
Name Size Bytes Class Attributes
A 320x320x3 307200 uint8
Script function saved in matlabfile average.m
function av = average(A)
%"AVERAGE Computes the average value of an array.
Page 33 of 122
% AV = AVERAGE(A) computes the average value of input
% array, A, which must be a I-D o r 2-D array.
% Check the validity of the input . (Keep in mind that
% a I-D array is a special case of a 2-D array.)
if ndims(A) > 2
error ( ' The dimensions of the input cannot exceed 2 . ' )
end % Compute the average
av = sum(A(:))/length( A ( : ) ) ;
The input image
Result
The average intensity of the image was computed and its average intensity was obtained as
38.4374.
2.3 M-function that multiplies two input images and outputs the
product of the images, the maximum and the minimum values of the
product are computed and a normalized product image whose
values are in the range [0,1] Code
A=imread('jerry.png')
B=imread('tom2.png');
Page 34 of 122
[F,K]=sumprod(A,B)
imshow(F)
imshow(K)
subscript name sumprod.m
function [s,p] = sumprod(f,g)
s=f+g;
p=f.*g;
Output
Product
SUM
Page 35 of 122
Output
An mfile was written to add and multiply two different images.
Code
A=imread('jerry.png');
B=imread('tom2.png');
C=A.*B;
>> t=max(max(max(C)));
>> t
t =
255
>> t=min(min(min(C)));
>> t
t =
0
Result:The maximum value obtained was 255 and the minimum value obatained was 0
2.3 A matlab code to compute the ratio of the bytes in the two images.
Program Code
function cr = imratio(f1, f2)
%IMRATIO Computes the ratio of the bytes in two images/variables.
% CR = IMRATIO(F1, F2) returns the ratio of the number of bytes in
% variables/files F1 and F2. If F1 and F2 are an original and
% compressed image, respectively, CR is the compression ratio.
error(nargchk(2, 2, nargin)); % Check input arguments
Page 36 of 122
cr = bytes(f1) / bytes(f2); % Compute the ratio
%-------------------------------------------------------------------%
function b = bytes(f)
% Return the number of bytes in input f. If f is a string, assume
% that it is an image filename; if not, it is an image variable.
if ischar(f)
info = dir(f); b = info.bytes;
elseif isstruct(f)
% MATLAB's whos function reports an extra 124 bytes of memory
% per structure field because of the way MATLAB stores
% structures in memory. Don't count this extra memory; instead,
% add up the memory associated with each field.
b = 0;
fields = fieldnames(f);
for k = 1:length(fields)
b = b + bytes(f.(fields{k}));
end
else
info = whos('f'); b = info.bytes;
end
Execution Sequence in Command window
A=imread('jerry.png')
B=imread('tom2.png');
r = ratio(A,B)
r =
1
Result
The Code to compute the ratio of the bytes in the two images was computed and the result was
obtained.
mfile:ratio.m
Page 37 of 122
2.4 Write an M-function to compute the implementation of the
following two dimensional image function using for loops and
vectorization.
f(x,y)=asin(u0x+v0y)
for x= 0,1,2,….M-1 and y=0,1,2,….N-1
Code:
>> f=imread('tom2.png');
>> [rt,f,g] = twodsin(1,1/(4*pi),1/(4*pi),512,512);
rt
rt =
1.1353e+003
>> g=mat2gray(g);
imshow(g)
matlab function
m file :twodsin.m
function [rt,f,g] = twodsin(A,uO,vO,M,N)
%TWOCISIN Compares f o r loops vs. v e c t o r i z a t i o n .
% The comparison i s based on implementing the f u n c t i o n
% f ( x , y) = Asin(u0x + vOy) f o r x = 0, 1, 2, ..., M - 1 and
% y = 0, 1, 2, ..., N - 1. The i n p u t s t o t h e f u n c t i o n are
% M and N and the constants i n the f u n c t i o n .
% F i r s t implement using f o r loops.
Page 38 of 122
tic % S t a r t t i m i n g .
for r = 1:M
uOx = uO*(r - 1 ) ;
for c = 1 : N
vOy = vO*(c - 1 ) ;
f(r,c)= A*sin(uOx+vOy);
end
end
t1= toc; % End t i m i n g .
% Now implement using v e c t o r i z a t i o n . C a l l the image g.
tic % S t a r t timing.
r=0:M-1;
C=0:N - 1;
[C, R] = meshgrid(c, r ) ;
g = A*sin(uO*R + vO*C);
t2 = toc; % End t i m i n g .
% Compute the r a t i o of the two times.
rt = t1/(t2+eps); % Use eps i n case t 2 is close t o 0.
Input Image f becomes
Output
Page 39 of 122
2.5To corrupt the original lenna image with impulse (Salt & Pepper
) noise of density 0.01.Attempt to minimize the noise effects by using
an appropriate spatial domain filter. Code:
>> B=imread('tom2.png'); % B is an array to read the image tom2.png
>> J = imnoise(B,'salt & pepper',0.01); % adding 0.01 density of noise
>> imshow(J) %display the noise added image
%Now to remove the corrupt image there are linear and nonlinear spatial filters here first I am
using linear filter
>> w=fspecial('laplacian',0)
w =
0 1 0
1 -4 1
0 1 0
>> g1=imfilter(J,w,'replicate');
>> imshow(g1)
Page 40 of 122
>>g2=im2double(f);
>>g2=imfilter(J,w,'replicate');
>> imshow(g2)
Output
Image before adding Salt & Pepper Image after adding Salt & Pepper noise
After linear filtering the corrupted image g2 image
Program 2
Page 41 of 122
Aim
To corrupt the original lenna image with impulse (Salt & Pepper ) noise of density 0.01.Attempt
to minimize the noise effects by using an appropriate spatial domain filter.
Code
>> B=imread('tom2.png'); % B is an array to read the image tom2.png
>> J = imnoise(B,'salt & pepper',0.01); % adding 0.01 density of noise
>> imshow(J) %display the noise added image
%Now to remove the corrupt image there are linear and nonlinear spatial filters here first I am
using linear filter
>> w4=fspecial('Laplacian',0);
>> w8=[111;1-81;111];
>> J=im2double(J);
>> g4=J-imfilter(J, w4, 'replicate');
>> g8 = J - imfilter(J, w8, 'replicate');
>> imshow(J)
>> figure, imshow(g4)
>> figure, imshow(g8)
>> figure, imshow(g4)
Output
g1 g2
Page 42 of 122
g4 g5
Result
The image was rectified using linear spatial filter
2.6 To generate a bimodal Gaussian function. Code:
%M-file:twomodegauss.m
function p = twomodegauss(m1,sig1,m2,sig2,A1,A2,k )
%TWOMODEGAUSS Generates a bimodal Gaussian function.
% P = TWOMODEGAUSS(M1, SIG1, M2, SIG2, Al, A2, K) generates a bimodal,
% Gaussian-like function i n the interval [0, I ] . P is a 256-element
% vector normalized so that SUM(P) equals 1 . The mean and standard
% deviation of the modes are (MI, SIG1) and (M2, SIG2), respectively.
Page 43 of 122
% A1 and A2 are the amplitude values of the two modes. Since the output is normalized, only
the relative values of A1 and A2 areimportant. K is an offset value that raises the "floor" of the
%function. A good set of values to try is MI = 0.15, SIGI = 0.05,M2 = 0.75, SIG2 = 0.05, A1 =
1, A2 = 0.07, and K - 0.002.
c1=A1*(1/((2*pi)^0.5)*sig1);
k1=2*(sig1^2);
c2=A2*(1/((2*pi)^0.5)*sig2);
k2=2*(sig2^2);
z=linspace(0, 1, 256) ;
p= k+ c1 *exp(-((2 - m1).^2)./k1)+...
+ c2 *exp(-((2 - m2).^2) ./k2);
p=p./sum(p(:));
Command line Code
>>p=twomodegauss(0.15,0.05,0.75,0.05,1,0.07,0.002);
>>plot(p)
>>imhist(p)
>>f=imhist(p);
>> plot(f)
imshow(f)
Output
Page 44 of 122
2.7 To extract a rectangular sub image from an image. Code
mfile:subimage.m
function s = subim(f, m, n, rx, cy)
%SUBIM Extracts a subimage, s , from a given image, f .
% The subimage is of size m-by-n,and the coordinates
% of its top, left corner are(rx,cy).
s=zeros(m, n);
rowhigh= rx+ m - 1;
colhigh = cy + n - 1;
xcount = 0;
for r = rx:rowhigh
xcount = xcount + 1;
ycount = 0;
for c = cy:colhigh
ycount = ycount + 1;
s(xcount,ycount) = f(r,c);
Page 45 of 122
end
end
Command line execution
s = subimage(A,200,200,10,10);
imshow(s)
OUTPUT
Image before Image after execution
2.8 To find the entropy of an image matrix
Code:
function h = entropy(x,n)
% H = ENTROPY(X, N) returns the first-order estimate of matrix X
% with N symbols(N=256 if omitted)in bitslsymbol. The estimate
% assumes a statistically independent source characterized by t lsl
% relative frequency of occurrence of the elements i n X.
error(nargchk(1, 2, nargin)); % Check input argument
if nargin < 2
n = 256; % Default for n.
Page 46 of 122
end
x = double(x); % Make input double
xh = hist(x(:),n); % Compute N-bin histogram
xh = xh/sum(xh(:)); % Compute probabilities
% Make mask to eliminate 0's since log2(0) = -inf.
i = find(xh)
h = -sum(xh(i) .* log2(xh(i))); % Compute entropy
Command line code
h = entropy(A,256)
h =
2.7470
>> whos A
Name Size Bytes Class Attributes
A 320x320x3 307200 uint8
Result
The first order entropy of the image was computed as 2.7470
2.9 To write an m function for Intensity Transformation, Intensity
Scaling
Program Code
function g = intrans(f,varargin)
%INTRANS Performs lntenslty (gray-level) transformatlons. a - - -
% G = INTRANS(F, 'neg') computes the negatlve of Input Image F.
Page 47 of 122
%
% G = INTRANS(F, ' l o g ' , C, CLASS) computes C*log(l + F ) and
% multlplles the result by (posltlve) constant C. If the last t w o
% parameters are omltted, C defaults to 1 . Because the l o g 1s used
% frequently to dlsplay Fourler spectra, parameter CLASS offers the
% optlon to speclfy the class of the output as 'ulnt8' or
% 'ulntl6'. If parameter CLASS 1s omltted, the output 1s of the
% same class as the lnput.
%
% G = INTRANS(F, 'gamma', GAM) performs a gamma transformatlon on
% the Input Image uslng parameter GAM ( a requlred ~nput).
%
% G = INTRANS(F, 'stretch', M , E ) computes a contrast-stretching
% transformatlon uslng the expression 1. / ( I + ( M . I ( F +
% eps)). ^ E ) . Parameter M must be l n the range [0, I ] . The default
% value for M IS mean2(lm2double(F)),and the default value for E
% is 4.
%
% For the ' n e g ' , 'gamma', and 'stretch' transformatlons, double
% lnput lmages whose maxlmum value 1s greater than 1 are scaled
% flrst uslng MAT2GRAY. Other lmages are converted to double flrst
% uslng IM2DOUBLE. For the ' l o g ' transformatlon, double lmages are
% transformed wlthout berng scaled; other Images are converted to
% double flrst uslng IM2DOUBLE.
%
% The output 1s of the same class as the ~nput,except ~ . fa
% different class 1s speclfled for the ' l o g ' optlon.
% Verlfy the correct number of lnputs.
error(nargchk(2,4,nargin))
% Store the class of the input for use later
classin =class(f);
% If the input is of class double, and it is outside the range
% [O, 1], and the specified transformation i s not ' l o g ' , convert the
% input to the range (0, 1).
if strcmp(class(f),'double') & max(f(:))> 1 & ...
-strcmp(varargin{l}, 'log' )
f = mat2gray( f );
else % Convert to double, regardless of class(f).
f = im2double ( f );
end
% Determine the type of transformation specified.
method = varargin{1};
% Perform the intensity transformation specified.
switch method
case ' neg '
g = imcomplement( f ) ;
Page 48 of 122
case ' log '
if length (varargin) == 1
c = 1;
elseif length(varargin) == 2
c = varargin{2};
elseif length(varargin) == 3
c = varargin{2};
classin = varargin{3};
else
error('1ncorrect number of inputs for the log option.')
end
g = c*(log(l + double(f)));
case 'gamma'
if length(varargin) < 2
error('Not enough inputs for the gamma option.')
end
gam = varargin{2};
g = imadjust(f, [ ] , [ ], gam);
case 'stretch'
if length(varargin) == 1
% Use defaults.
m = meanZ(f);
E = 4.0 ;
elseif length (varargin) == 3
m = varargin{2};
E = varargin{3};
else error('1ncorrect number of inputs for the stretch option. ' )
end
g=1/(1+(m./(f+eps)).^E);
otherwise
error( ' U n k n o w n enhancement method. ' )
end
% Convert to the class of the input image.
Command Line Code
g =intrans( f ,'stretch',mean2(im2double(f)),0.9);
>> figure,imshow(g)
Page 49 of 122
Input Output
Program Code for Intensity Scaling
%Program for Intensity Scaling
function g=gscale(f,method,low,high)
% GSCALE adjusts the scale of an image.
%
% GSCALE returns the input image with the scale adjusted according to the
% input specifications.
%
% USAGE:
% g=gscale(f,'minmax'); adjusts the image to the default min and max of
% class of the input image
%
% g=gscale(f,'full8'); adjusts the image to a 8bit scale
%
% g=gscale(f,'full16'); adjsuts the image to a 16bit scale
%
% INPUTS:
% f: grayscale image (any class)
% method: scaling method
%
% OUTPUTS:
% g: adjusted image
% Modified Donald G. Dansereau 2013, minor fix: replace NaNs resulting from div-by-zero
with zeros
% input error checking
if ~exist('f','var') || isempty(f)
error('Please Input Image');
end
Page 50 of 122
if ~exist('method','var') || isempty(method)
error('Please Specify Method');
end
if ~exist('low','var') || isempty(low)
low=0;
end
if ~exist('high','var') || isempty(high)
high=1;
end
if numel(low)>1 || numel(high)>1
error('Low and High should be scalars');
end
if low>1 || low<0 || high>1 || high<0
error('Low and High should be between 0 and 1');
end
g=double(f);
low=double(low);
high=double(high);
gmin=min(g(:));
gmax=max(g(:));
%Adjust to [0,1]
g=(g-gmin)./(gmax-gmin);
g(isnan(g)) = 0; % removing NaNs from divide by zero, above
switch method
case 'full8'
g=im2uint8(g);
case 'full16'
g=im2uint16(g);
case 'minmax'
g=g.*(high-low)+low;
switch class(f)
case 'uint8'
g=im2uint8(g);
case 'uint16'
g=im2uint16(g);
case 'double'
g=im2double(g);
otherwise
error('Unsupported Format, Supported Formats: unit8, uint16, double');
end
otherwise
error('Invalid Method');
end
Page 51 of 122
Command Line Code
>> g=gscale(g,'full8',0,0.5) %There are two methods for intensity scaling either through full8 or
through full16.
>>imshow(g)
Output
The g image obtained after Intensity transformation is being applied through Intensity scaling
function.
2.10 Program to find the error between two images/matrices. Program Code:
A=imread('jerry.png')
B=imread('tom2.png')
C=A-B
imshow(C)
Page 52 of 122
Inputs
Output
Result
The error between the two matrices were found out and resultant image/,matrix was obtained.
2.11 To perform histogram equilisation. Code
>> I=imread('jerry.png');
>> I=rgb2gray(I);
>> Ieq=histeq(I);
>> subplot(2,2,1)
>> imshow(I)
>> title('original image');
Page 53 of 122
>> subplot(2,2,2)
>> imshow(Ieq)
>> title('Ieq');
>> subplot(2,2,3)
>> imshow(imhist(I))
>> title('histogram of I');
>> subplot(2,2,4)
>> imshow(imhist(Ieq))
>> title('histogram of Ieq');
%plot of images and respective bar graphs
>> subplot(2,2,1)
>> imshow(I)
>> title('original image');
>> subplot(2,2,2)
>> bar(I)
>> title('bar graph of original image');
>> subplot(2,2,3)
>> imshow(Ieq)
>> title('After histogram Equilisation');
>> subplot(2,2,4)
>> bar(Ieq)
>> title('bar graph of histogram equilized plot')
Page 54 of 122
Output:
Program 2
>> I=imread('lesscontrast.jpg');
Page 55 of 122
>> I=rgb2gray(I);
Ieq=histeq(I);
subplot(2,2,1)
imshow(I)
title('original image');
subplot(2,2,2)
imshow(Ieq)
title('Ieq');
subplot(2,2,3)
imshow(imhist(I))
title('histogram of I');
subplot(2,2,4)
imshow(imhist(Ieq))
title('histogram of Ieq');
>> %plot of images and respective bar graphs
subplot(2,2,1)
imshow(I)
title('original image');
subplot(2,2,2)
bar(I)
title('bar graph of original image');
subplot(2,2,3)
imshow(Ieq)
title('After histogram Equilisation');
subplot(2,2,4)
Page 56 of 122
bar(Ieq)
title('bar graph of histogram equilized plot')
OUTPUT
Result
Histogram equlisation of the image was performed.
2.12 Write a MATLAB code to perform the contrast stretching on
the image. Show the original and stretched image along with their
histograms. clear all;
close all;
clc;
%% Reading an image
a=imread('Mario_png.png');
a=double(a);
s=size(a);
%% Defingin points and calculating equation parameters
p1=[0,0];
p2=[150,20];
p3=[200,200];
Page 57 of 122
p4=[255,255];
m1=(p1(1,2)-p2(1,2))/(p1(1,1)-p2(1,1));
m2=(p2(1,2)-p3(1,2))/(p2(1,1)-p3(1,1));
m3=(p3(1,2)-p4(1,2))/(p3(1,1)-p4(1,1));
c1=p1(1,2)-m1*p1(1,1);
c2=p2(1,2)-m2*p2(1,1);
c3=p3(1,2)-m3*p3(1,1);
%% Transformation function
t=[];
for x=0:255
if(x<=p2(1,1))
t=[t (m1*x+c1)];
end
if(x>p2(1,1) && x<=p3(1,1))
t=[t (m2*x+c2)];
end
if(x>p3(1,1) && x<=p4(1,1))
t=[t (m3*x+c3)];
end
end
%% Getting output image
for n=1:s(1,1)
for m=1:s(1,2)
ot(n,m)=t(a(n,m)+1);
end
end
plot(t)
grid on;
xlabel('Intensity in input image');
ylabel('Intensity in output image')
title('Piece-wise linear transformation : Contrast stretching function')
figure()
subplot(1,2,1)
imshow(a/255)
title('Original image')
subplot(1,2,2)
imshow(ot./255)
title('Contrast stretching')
Page 58 of 122
Output
2.13To plot the histogram and bar graph of an image. To find the bar-graph of the image.
Code:
>> h=imread('tom2.png');
>> h=rgb2gray(h);
Page 59 of 122
>> hl = h(1:10:256);
horz = 1:10:256;
>> subplot(1,2,1)
>> imshow(h)
>> subplot(1,2,2)
>> title('bargraph of the image');
>> bar(h)
>> title('bargraph of the image');
Output of the image
Result:
The bar-graph of the image was plotted.
Code
>> a=imread('tom2.png');
Page 60 of 122
>> b=zeros(1,256);
[row,col]=size(a);
for x=1:1:row
for y=1:1:col
if a(x,y)<1
continue;
else
t=a(x,y);
end
b(t)=b(t)+1;
end
end
subplot(1,2,1);
>> imshow(uint8(a));
>> title('Original Image');
subplot(1,2,2);
bar(b);
title('Histogarm of image');
>>
Page 61 of 122
Output
Alternate method using imhist( ) function which only uses 2D array
>> I=rgb2gray(I);
BW = edge(I);
>> imhist(I)
>> I=imread('tom2.png');
>> I=rgb2gray(I);
>> imhist(I)
>> imshow(I)
>> subplot(1,2,1);
>> imshow(uint8(I));
Page 62 of 122
>> title('Original Image');
>> subplot(1,2,2);
>> imhist(I)
>> imhist(I);
>> title('histogram of image');
Output
Page 63 of 122
2.14Coorrupt the original lenna of the image with a zero mean
Gaussian noise,so that the SNR of the noisy image is 5 dB.Attempt
to minimize noise effects by using appropriate filters in spatial
domain and spectral domain. Indicate the cutoff frequency of the
spectral domain filter.
Spectral Domain filter
Low pass filter
Code
>> SNR = 5;
footBall=imread('football.jpg');
footBall=im2double(footBall);
vm = var(footBall(:)) / 10^(SNR/10);
>> footBall= imnoise(footBall, 'gaussian', 0, vm);
>> imshow(footBall);
%Convert to grayscale
footBall=rgb2gray(footBall);
imshow(footBall)
%Determine good padding for Fourier transform
PQ = paddedsize(size(footBall));
%Create a Gaussian Lowpass filter 5% the width of the Fourier transform
D0 = 0.05*PQ(1);
H = lpfilter('gaussian', PQ(1), PQ(2), D0);
% Calculate the discrete Fourier transform of the image
F=fft2(double(footBall),size(H,1),size(H,2));
% Apply the highpass filter to the Fourier spectrum of the image
LPFS_football = H.*F;
Page 64 of 122
% convert the result to the spacial domain.
LPF_football=real(ifft2(LPFS_football));
% Crop the image to undo padding
LPF_football=LPF_football(1:size(footBall,1), 1:size(footBall,2));
%Display the blurred image
figure, imshow(LPF_football, [])
% Display the Fourier Spectrum
% Move the origin of the transform to the center of the frequency rectangle.
Fc=fftshift(F);
Fcf=fftshift(LPFS_football);
% use abs to compute the magnitude and use log to brighten display
S1=log(1+abs(Fc));
S2=log(1+abs(Fcf));
figure, imshow(S1,[])
figure, imshow(S2,[])
Output
Original Image Fourier
For applying a high pass filter
Page 65 of 122
M-files (paddedsize.m, dftuv.m, and hpfilter.m)
function PQ = paddedsize(AB, CD, PARAM)
%PADDEDSIZE Computes padded sizes useful for FFT-based filtering.
% PQ = PADDEDSIZE(AB), where AB is a two-element size vector,
% computes the two-element size vector PQ = 2*AB.
%
% PQ = PADDEDSIZE(AB, 'PWR2') computes the vector PQ such that
% PQ(1) = PQ(2) = 2^nextpow2(2*m), where m is MAX(AB).
%
% PQ = PADDEDSIZE(AB, CD), where AB and CD are two-element size
% vectors, computes the two-element size vector PQ. The elements
% of PQ are the smallest even integers greater than or equal to
% AB + CD -1.
%
% PQ = PADDEDSIZE(AB, CD, 'PWR2') computes the vector PQ such that
% PQ(1) = PQ(2) = 2^nextpow2(2*m), where m is MAX([AB CD]).
if nargin == 1
PQ = 2*AB;
elseif nargin == 2 & ~ischar(CD)
PQ = AB + CD - 1;
PQ = 2 * ceil(PQ / 2);
elseif nargin == 2
m = max(AB); % Maximum dimension.
% Find power-of-2 at least twice m.
P = 2^nextpow2(2*m);
PQ = [P, P];
elseif nargin == 3
m = max([AB CD]); %Maximum dimension.
P = 2^nextpow2(2*m);
PQ = [P, P];
else
error('Wrong number of inputs.')
end
function [U, V] = dftuv(M, N)
%DFTUV Computes meshgrid frequency matrices.
% [U, V] = DFTUV(M, N) computes meshgrid frequency matrices U and
% V. U and V are useful for computing frequency-domain filter
% functions that can be used with DFTFILT. U and V are both M-by-N.
% Set up range of variables.
u = 0:(M-1);
Page 66 of 122
v = 0:(N-1);
% Compute the indices for use in meshgrid
idx = find(u > M/2);
u(idx) = u(idx) - M;
idy = find(v > N/2);
v(idy) = v(idy) - N;
% Compute the meshgrid arrays
[V, U] = meshgrid(v, u);
function H = hpfilter(type, M, N, D0, n)
%HPFILTER Computes frequency domain highpass filters
% H = HPFILTER(TYPE, M, N, D0, n) creates the transfer function of
% a highpass filter, H, of the specified TYPE and size (M-by-N).
% Valid values for TYPE, D0, and n are:
%
% 'ideal' Ideal highpass filter with cutoff frequency D0. n
% need not be supplied. D0 must be positive
%
% 'btw' Butterworth highpass filter of order n, and cutoff D0.
% The default value for n is 1.0. D0 must be positive.
%
% 'gaussian' Gaussian highpass filter with cutoff (standard deviation)
% D0. n need not be supplied. D0 must be positive.
%
% The transfer function Hhp of a highpass filter is 1 - Hlp,
% where Hlp is the transfer function of the corresponding lowpass
% filter. Thus, we can use function lpfilter to generate highpass
% filters.
if nargin == 4
n = 1; % Default value of n.
end
% Generate highpass filter.
Hlp = lpfilter(type, M, N, D0, n);
H = 1 - Hlp;
Code
>> SNR = 5;
footBall=imread('football.jpg');
Page 67 of 122
footBall=im2double(footBall);
vm = var(footBall(:)) / 10^(SNR/10);
footBall= imnoise(footBall, 'gaussian', 0, vm);
imshow(footBall);
%Convert to grayscale
footBall=rgb2gray(footBall);
imshow(footBall)
%Determine good padding for Fourier transform
PQ = paddedsize(size(footBall));
%Create a Gaussian Highpass filter 5% the width of the Fourier transform
D0 = 0.05*PQ(1);
H = hpfilter('gaussian', PQ(1), PQ(2), D0);
% Calculate the discrete Fourier transform of the image
F=fft2(double(footBall),size(H,1),size(H,2));
% Apply the highpass filter to the Fourier spectrum of the image
HPFS_football = H.*F;
% convert the result to the spacial domain.
HPF_football=real(ifft2(HPFS_football));
% Crop the image to undo padding
HPF_football=HPF_football(1:size(footBall,1), 1:size(footBall,2));
%Display the "Sharpened" image
figure, imshow(HPF_football, [])
% Display the Fourier Spectrum
% Move the origin of the transform to the center of the frequency rectangle.
Fc=fftshift(F);
Page 68 of 122
Fcf=fftshift(HPFS_football);
% use abs to compute the magnitude and use log to brighten display
S1=log(1+abs(Fc));
S2=log(1+abs(Fcf));
figure, imshow(S1,[])
figure, imshow(S2,[])
>>
Ouput
Original Image
Image found after adding zero mean Gaussian noise and SNR of 5 db
Page 69 of 122
Fourier Spectrum of image
Fourier with high pass filter
Spectrum of image with high pass filter
>> SNR = 5;
footBall=imread('football.jpg');
footBall=im2double(footBall);
vm = var(footBall(:)) / 10^(SNR/10);
Page 70 of 122
footBall= imnoise(footBall, 'gaussian', 0, vm);
imshow(footBall);
>> footBall=rgb2gray(footBall);
>> imshow(footBall);
>> footBall= medfilt2(footBall);
imshow(footBall);
Image when the original lenna is corupted with zero-mean Gaussian noise, so that the SNR of the
noisy image is 5 dB.
The noise of the image is being reduced by using median filter
To find the spectral domain filtering of an image.
Mfunctions required paddedsize.m, lpfilter.m and dftuv.m
Page 71 of 122
function PQ = paddedsize(AB, CD, PARAM)
%PADDEDSIZE Computes padded sizes useful for FFT-based filtering.
% PQ = PADDEDSIZE(AB), where AB is a two-element size vector,
% computes the two-element size vector PQ = 2*AB.
%
% PQ = PADDEDSIZE(AB, 'PWR2') computes the vector PQ such that
% PQ(1) = PQ(2) = 2^nextpow2(2*m), where m is MAX(AB).
%
% PQ = PADDEDSIZE(AB, CD), where AB and CD are two-element size
% vectors, computes the two-element size vector PQ. The elements
% of PQ are the smallest even integers greater than or equal to
% AB + CD -1.
%
% PQ = PADDEDSIZE(AB, CD, 'PWR2') computes the vector PQ such that
% PQ(1) = PQ(2) = 2^nextpow2(2*m), where m is MAX([AB CD]).
if nargin == 1
PQ = 2*AB;
elseif nargin == 2 & ~ischar(CD)
PQ = AB + CD - 1;
PQ = 2 * ceil(PQ / 2);
elseif nargin == 2
m = max(AB); % Maximum dimension.
% Find power-of-2 at least twice m.
P = 2^nextpow2(2*m);
PQ = [P, P];
elseif nargin == 3
m = max([AB CD]); %Maximum dimension.
P = 2^nextpow2(2*m);
PQ = [P, P];
else
error('Wrong number of inputs.')
end
function H = lpfilter(type, M, N, D0, n)
%LPFILTER Computes frequency domain lowpass filters
% H = LPFILTER(TYPE, M, N, D0, n) creates the transfer function of
% a lowpass filter, H, of the specified TYPE and size (M-by-N). To
% view the filter as an image or mesh plot, it should be centered
% using H = fftshift(H).
%
% Valid values for TYPE, D0, and n are:
%
Page 72 of 122
% 'ideal' Ideal lowpass filter with cutoff frequency D0. n need
% not be supplied. D0 must be positive
%
% 'btw' Butterworth lowpass filter of order n, and cutoff D0.
% The default value for n is 1.0. D0 must be positive.
%
% 'gaussian' Gaussian lowpass filter with cutoff (standard deviation)
% D0. n need not be supplied. D0 must be positive.
% Use function dftuv to set up the meshgrid arrays needed for
% computing the required distances.
[U, V] = dftuv(M, N);
% Compute the distances D(U, V).
D = sqrt(U.^2 + V.^2);
% Begin fiter computations.
switch type
case 'ideal'
H = double(D <=D0);
case 'btw'
if nargin == 4
n = 1;
end
H = 1./(1 + (D./D0).^(2*n));
case 'gaussian'
H = exp(-(D.^2)./(2*(D0^2)));
otherwise
error('Unknown filter type.')
end
function [U, V] = dftuv(M, N)
%DFTUV Computes meshgrid frequency matrices.
% [U, V] = DFTUV(M, N) computes meshgrid frequency matrices U and
% V. U and V are useful for computing frequency-domain filter
% functions that can be used with DFTFILT. U and V are both M-by-N.
% Set up range of variables.
u = 0:(M-1);
v = 0:(N-1);
% Compute the indices for use in meshgrid
idx = find(u > M/2);
u(idx) = u(idx) - M;
idy = find(v > N/2);
v(idy) = v(idy) - N;
Page 73 of 122
% Compute the meshgrid arrays
[V, U] = meshgrid(v, u);
Code
>> SNR = 5;
footBall=imread('football.jpg');
footBall=im2double(footBall);
vm = var(footBall(:)) / 10^(SNR/10);
>> footBall= imnoise(footBall, 'gaussian', 0, vm);
>> imshow(footBall);
%Convert to grayscale
footBall=rgb2gray(footBall);
imshow(footBall)
%Determine good padding for Fourier transform
PQ = paddedsize(size(footBall));
%Create a Gaussian Lowpass filter 5% the width of the Fourier transform
D0 = 0.05*PQ(1);
H = lpfilter('gaussian', PQ(1), PQ(2), D0);
% Calculate the discrete Fourier transform of the image
F=fft2(double(footBall),size(H,1),size(H,2));
% Apply the highpass filter to the Fourier spectrum of the image
LPFS_football = H.*F;
% convert the result to the spacial domain.
LPF_football=real(ifft2(LPFS_football));
% Crop the image to undo padding
LPF_football=LPF_football(1:size(footBall,1), 1:size(footBall,2));
%Display the blurred image
figure, imshow(LPF_football, [])
Page 74 of 122
% Display the Fourier Spectrum
% Move the origin of the transform to the center of the frequency rectangle.
Fc=fftshift(F);
Fcf=fftshift(LPFS_football);
% use abs to compute the magnitude and use log to brighten display
S1=log(1+abs(Fc));
S2=log(1+abs(Fcf));
figure, imshow(S1,[])
figure, imshow(S2,[])
Orignal Image
Image found after adding zero mean Gaussian noise and SNR of 5 db
Page 75 of 122
Fourier Spectrum of image
Fourier with gaussian lowpass filter
Spectrum of image with Gaussian Lowpass filter
Page 76 of 122
Aim
Spectral Filtering using notch filter.
Code
>> SNR = 5;
footBall=imread('football.jpg');
footBall=im2double(footBall);
vm = var(footBall(:)) / 10^(SNR/10);
footBall= imnoise(footBall, 'gaussian', 0, vm);
imshow(footBall);
%Convert to grayscale
footBall=rgb2gray(footBall);
imshow(footBall)
%Determine good padding for Fourier transform
PQ = paddedsize(size(footBall));
>> %Create a Gaussian Highpass filter 5% the width of the Fourier transform
D0 = 0.05*PQ(1);
H = hpfilter('gaussian', PQ(1), PQ(2), D0);
% Calculate the discrete Fourier transform of the image
F=fft2(double(footBall),size(H,1),size(H,2));
% Apply the highpass filter to the Fourier spectrum of the image
HPFS_football = H.*F;
>> % convert the result to the spacial domain.
HPF_football=real(ifft2(HPFS_football));
% Crop the image to undo padding
HPF_football=HPF_football(1:size(footBall,1), 1:size(footBall,2));
Page 77 of 122
%Display the "Sharpened" image
figure, imshow(HPF_football, [])
% Display the Fourier Spectrum
% Move the origin of the transform to the center of the frequency rectangle.
Fc=fftshift(F);
Fcf=fftshift(HPFS_football);
% use abs to compute the magnitude and use log to brighten di
>> S1=log(1+abs(Fc));
S2=log(1+abs(Fcf));
figure, imshow(S1,[])
figure, imshow(S2,[])
Output
Image found after adding zero mean Gaussian noise and SNR of 5 db
Page 78 of 122
Fourier Spectrum of image
Fourier with high pass filter
Spectrum of image with high pass filter
Page 79 of 122
2.15 To Generate a 3X3 spatial averaging filter and to perform
neighborhood averaging.To plot and observe using fast Fourier
transform Aim:
To Generate a 3X3 spatial averaging filter and to perform neighborhood averaging
Code
>> h = fspecial('average');
>> h
h =
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
>> I = im2double(imread('tire.tif'));
>> f = @(h) sqrt(min(h(:)));
>> I2 = nlfilter(I,[3 3],f);
>>imshow(I);
>>figure, imshow(I2);
Output
Before applying 3X3 neighborhood averaging filter
Page 80 of 122
After applying a 3X3 neighborhood filter h
Code: To find the Fourier Transform of the images
>> F = fftshift(I);
>> % Center FFT
>>F = abs(F); % Get the magnitude
>> F = log(F+1); % Use log, for perceptual scaling, and +1 since log(0) is undefined
>>F = mat2gray(F); % Use mat2gray to scale the image between 0 and 1
>>imshow(F,[]); % Display the result
The Fourier Transform of the original Image
Page 81 of 122
The Fourier Transform of the image I2 on which neighborhood averaging is being performed
Aim
To plot and observe using fast Fourier transform
Code
>> h = fspecial('average');
>> h
h =
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
Page 82 of 122
0.1111 0.1111 0.1111
>> I = im2double(imread('tire.tif'));
>> f = @(h) sqrt(min(h(:)));
>> I2 = nlfilter(I,[3 3],f);
imshow(I)
figure, imshow(I2)
imageA =I;
imageB = I2;
%Perform 2D FFTs
fftA = fft2(double(imageA));
fftB = fft2(double(imageB));
%Display magnitude and phase of 2D FFTs
figure, imshow(abs(fftshift(fftA)),[24 100000]), colormap gray
title('Image A FFT2 Magnitude')
figure, imshow(angle(fftshift(fftA)),[-pi pi]), colormap gray
title('Image A FFT2 Phase')
figure, imshow(abs(fftshift(fftB)),[24 100000]), colormap gray
title('Image B FFT2 Magnitude')
figure, imshow(angle(fftshift(fftB)),[-pi pi]), colormap gray
title('Image B FFT2 Phase')
%Switch magnitude and phase of 2D FFTs
Page 83 of 122
fftC = abs(fftA).*exp(i*angle(fftB));
fftD = abs(fftB).*exp(i*angle(fftA));
%Perform inverse 2D FFTs on switched images
imageC = ifft2(fftC);
imageD = ifft2(fftD);
%Calculate limits for plotting
cmin = min(min(abs(imageC)));
cmax = max(max(abs(imageC)));
dmin = min(min(abs(imageD)));
dmax = max(max(abs(imageD)));
%Display switched images
figure, imshow(abs(imageC), [cmin cmax]), colormap gray
title('Image C Magnitude')
figure, imshow(abs(imageD), [dmin dmax]), colormap gray
title('Image D Magnitude')
%Save images
Page 84 of 122
saveas(1,'imageA.png')
saveas(2,'imageB.png')
saveas(3,'imageAfftmag.png')
saveas(4,'imageAfftpha.png')
saveas(5,'imageBfftmag.png')
saveas(6,'imageBfftpha.png')
saveas(7,'imageC.png')
saveas(8,'imageD.png')
The Output after performing fast fourier transform is as follows.
Figure 1
Figure 2
Page 85 of 122
Image A FFT2 magnitude
Image A FFT2 Phase
Image B FFT2 Magnitude
Page 86 of 122
Image B FFT2 phase
Image C Magnitude
Image D magnitude
Page 87 of 122
2.16 Read some images and convert them to binary images. Then to
show/implement all the binary image processing technique. Code:
>> I=imread('Mario_png.png');
>> BW = im2bw(I,0.5);%converts the intensity image I to black and white.
>> imshow(BW)
Input
Output
Alternate methods:
>> BW = im2bw(I,winter,0.4);
>> imshow(BW)
Output:
Page 88 of 122
>> BW = im2bw(I,summer,0.4);
>> imshow(BW)
Alternate
>> I=imread('Mario_png.png');
>> I=im2double(I);
>> BW = im2bw(I,0.5);
>> imshow(BW)
Output
Page 89 of 122
Result:
Three various methods of binary image conversion was performed using
BW = im2bw(I,level) %converts the intensity image I to black and white.
BW = im2bw(X,map,level)% converts the indexed image X with colormap map to black and
white.
BW = im2bw(RGB,level) %converts the RGB image RGB to black and white.
2.17 To read an image having some textures and form the co-
occurrence matrix. Code:
>> I = imread('pikachu.png');
>> I=rgb2gray(I);%to convert the image to a 2d array
BW = edge(I);
imshow(BW)
>> glcm = graycomatrix(I,'Offset',[2 0])
Page 90 of 122
Input Image
Output after Conversion to grayscale and then to double
Result
An image with texture was uploaded and a co-occurrence matrix was obtained.
Page 91 of 122
2.18 (a)-To Load a test image and find the edges by gradient –based
and Laplacian-based technique. Aim
To Load a test image and find the edges by gradient –based and Laplacian-based technique.
Code:
>> I=imread('Mario_png.png');
>> I=rgb2gray(I);
>> BW = edge(I,'log');
>> imshow(BW)
%Alternate code
BW = edge(I,'log',[],2);
imshow(BW)
Output
Original Image
Image after edge detection
Page 92 of 122
2.18 (b)-Canny Edge Detection Code:
>> I=imread('Mario_png.png');
I=rgb2gray(I);
>> h = fspecial('gaussian');
>> h
h =
0.0113 0.0838 0.0113
0.0838 0.6193 0.0838
0.0113 0.0838 0.0113
>> C = conv2(h,I);
>> imshow(I)
>> imshow(C)
>> h2 = fspecial('sobel');
Page 93 of 122
>> h2
h2 =
1 2 1
0 0 0
-1 -2 -1
>> C2 = conv2(h2,C);
>> imshow(C2)
>> canny_edges(1, 0.04,1.2,C2);
adjust =
5
mfile used canny_edges.m
function canny_edges(max_hysteresis_thresh, min_hysteresis_thresh,sigma,I);
ORIGINAL_IMAGE=I;
%%Save height and width
[H,W]=size(ORIGINAL_IMAGE);
%%Derivatives in x and y
derivative_x=zeros(H,W);
derivative_y=zeros(H,W);
%%Gaussian kernel
size_of_kernel = 6*sigma+1;
adjust= ceil(size_of_kernel/2)
Page 94 of 122
Y_GAUSSIAN=zeros(size_of_kernel,size_of_kernel);
X_GAUSSIAN=zeros(size_of_kernel,size_of_kernel);
%%Create gaussian kernels for both x and y directions based on the sigma
%%that was given.
for i=1:size_of_kernel
for iiii=1:size_of_kernel
Y_GAUSSIAN(i,iiii) = -( (i-((size_of_kernel-1)/2)-1)/( 2* pi * sigma^3 ) ) * exp ( - ( (i-
((size_of_kernel-1)/2)-1)^2 + (iiii-((size_of_kernel-1)/2)-1)^2 )/ (2*sigma^2) );
end
end
for i=1:size_of_kernel
for iiii=1:size_of_kernel
X_GAUSSIAN(i,iiii) = -( (iiii-((size_of_kernel-1)/2)-1)/( 2* pi * sigma^3 ) ) * exp ( - ( (i-
((size_of_kernel-1)/2)-1)^2 + (iiii-((size_of_kernel-1)/2)-1)^2 )/ (2*sigma^2) );
end
end
GRADIENT = zeros(H,W);
non_max = zeros(H,W);
post_hysteresis = zeros(H,W);
Page 95 of 122
%%Image Derivatives:
for r=1+ceil(size_of_kernel/2):H-ceil(size_of_kernel/2)
for c=1+ceil(size_of_kernel/2):W-ceil(size_of_kernel/2)
reference_row= r-ceil(size_of_kernel/2);
reference_colum= c-ceil(size_of_kernel/2);
for yyy=1:size_of_kernel
for yyy_col=1:size_of_kernel
derivative_x(r,c) = derivative_x(r,c) + ORIGINAL_IMAGE(reference_row+yyy-1,
reference_colum+yyy_col-1)*X_GAUSSIAN(yyy,yyy_col);
end
end
end
end
for r=1+ceil(size_of_kernel/2):H-ceil(size_of_kernel/2)
for c=1+ceil(size_of_kernel/2):W-ceil(size_of_kernel/2)
reference_row= r-ceil(size_of_kernel/2);
reference_colum= c-ceil(size_of_kernel/2);
for yyy=1:size_of_kernel
for yyy_col=1:size_of_kernel
derivative_y(r,c) = derivative_y(r,c) + ORIGINAL_IMAGE(reference_row+yyy-1,
reference_colum+yyy_col-1)*Y_GAUSSIAN(yyy,yyy_col);
end
end
Page 96 of 122
end
end
%%Compute the gradient magnitufde based on derivatives in x and y:
for r=1+ceil(size_of_kernel/2):H-ceil(size_of_kernel/2)
for c=1+ceil(size_of_kernel/2):W-ceil(size_of_kernel/2)
GRADIENT(r,c) = sqrt (derivative_x(r,c)^2 + derivative_y(r,c)^2 );
end
end
%%Perform Non maximum suppression:
non_max = GRADIENT;
for r=1+ceil(size_of_kernel/2):H-ceil(size_of_kernel/2)
for c=1+ceil(size_of_kernel/2):W-ceil(size_of_kernel/2)
%%quantize:
if (derivative_x(r,c) == 0) tangent = 5;
else tangent = (derivative_y(r,c)/derivative_x(r,c));
end
if (-0.4142<tangent & tangent<=0.4142)
if(GRADIENT(r,c)<GRADIENT(r,c+1) | GRADIENT(r,c)<GRADIENT(r,c-1))
non_max(r,c)=0;
Page 97 of 122
end
end
if (0.4142<tangent & tangent<=2.4142)
if(GRADIENT(r,c)<GRADIENT(r-1,c+1) | GRADIENT(r,c)<GRADIENT(r+1,c-1))
non_max(r,c)=0;
end
end
if ( abs(tangent) >2.4142)
if(GRADIENT(r,c)<GRADIENT(r-1,c) | GRADIENT(r,c)<GRADIENT(r+1,c))
non_max(r,c)=0;
end
end
if (-2.4142<tangent & tangent<= -0.4142)
if(GRADIENT(r,c)<GRADIENT(r-1,c-1) | GRADIENT(r,c)<GRADIENT(r+1,c+1))
non_max(r,c)=0;
end
end
end
end
post_hysteresis = non_max;
for r=1+ceil(size_of_kernel/2):H-ceil(size_of_kernel/2)
for c=1+ceil(size_of_kernel/2):W-ceil(size_of_kernel/2)
Page 98 of 122
if(post_hysteresis(r,c)>=max_hysteresis_thresh) post_hysteresis(r,c)=1;
end
if(post_hysteresis(r,c)<max_hysteresis_thresh &
post_hysteresis(r,c)>=min_hysteresis_thresh) post_hysteresis(r,c)=2;
end
if(post_hysteresis(r,c)<min_hysteresis_thresh) post_hysteresis(r,c)=0;
end
end
end
vvvv = 1;
while (vvvv == 1)
vvvv = 0;
for r=1+ceil(size_of_kernel/2):H-ceil(size_of_kernel/2)
for c=1+ceil(size_of_kernel/2):W-ceil(size_of_kernel/2)
if (post_hysteresis(r,c)>0)
if(post_hysteresis(r,c)==2)
if( post_hysteresis(r-1,c-1)==1 | post_hysteresis(r-1,c)==1 | post_hysteresis(r-
1,c+1)==1 | post_hysteresis(r,c-1)==1 | post_hysteresis(r,c+1)==1 | post_hysteresis(r+1,c-1)==1
| post_hysteresis(r+1,c)==1 | post_hysteresis(r+1,c+1)==1 ) post_hysteresis(r,c)=1;
vvvv == 1;
end
Page 99 of 122
end
end
end
end
end
for r=1+ceil(size_of_kernel/2):H-ceil(size_of_kernel/2)
for c=1+ceil(size_of_kernel/2):W-ceil(size_of_kernel/2)
if(post_hysteresis(r,c)==2)
post_hysteresis(r,c)==0;
end
end
end
imwrite(ORIGINAL_IMAGE,'D:\addition\original_image.bmp');
imwrite(derivative_x,'D:\addition\derivative_x.bmp');
imwrite(derivative_y,'D:\addition\derivative_y.bmp');
imwrite(GRADIENT,'D:\addition\gradient.bmp');
imwrite(non_max,'D:\addition\non_max_supr.bmp');
imwrite(post_hysteresis,'D:\addition\original_image.bmp');
Output
For maxthresh=1,minthresh=0.04,sigma=1.2,image=C2
Page 100 of 122
Original Image
Derivative x
Derivative y
Page 101 of 122
gradient
Non Maximum Suppressed Image
Aim
To convolve gradient at each pixel by convolving it with 2 D gaussian and then by sobel
operators
>> I=imread('Mario_png.png');
I=rgb2gray(I);
>> h = fspecial('gaussian');
>> h
Page 102 of 122
h =
0.0113 0.0838 0.0113
0.0838 0.6193 0.0838
0.0113 0.0838 0.0113
>> C = conv2(h,I);
>> imshow(I)
>> imshow(C)
>> h2 = fspecial('sobel');
>> h2
h2 =
1 2 1
0 0 0
-1 -2 -1
>> C2 = conv2(h2,C);
>> imshow(C2)
Mfile to be used is NonMaximalSupp.m
function H = nonMaximalSupp(dx, dy, magGrad, lowThresh, highThresh)
% Perform Non-Maximum Suppression Thining and Hysteresis Thresholding of Edge
% Strength
% H = nonMaximalSupp(dx, dy, magGrad, lowThresh, highThresh)
% H is suppressed image
% dx and dy are the gradients
% magGrad is the magnitude of gradient
Page 103 of 122
% lowThresh and highThresh are the thresholds
magmax = max(magGrad(:));
if magmax > 0
magGrad = magGrad / magmax;
end
[row, col] = size(magGrad);
E = false(row, col);
idxStrong = [];
for dir = 1:4
idxLocalMax = cannyFindLocalMaxima(dir,dx,dy,magGrad);
idxWeak = idxLocalMax(magGrad(idxLocalMax) > lowThresh);
E(idxWeak)=1;
idxStrong = [idxStrong; idxWeak(magGrad(idxWeak) > highThresh)]; %#ok<AGROW>
end
[m,n] = size(E);
if ~isempty(idxStrong) % result is all zeros if idxStrong is empty
rstrong = rem(idxStrong-1, m)+1;
cstrong = floor((idxStrong-1)/m)+1;
H = bwselect(E, cstrong, rstrong, 8);
else
H = zeros(m, n);
end
Output
Original Image
Page 104 of 122
Image after convolution of I with gaussian operator
Image C2 after circular convolution with the sobel operator
2.19 Lossy Image Compression
Code:
>> RGB = imread('autumn.tif');
I = rgb2gray(RGB);
J = dct2(I);
Page 105 of 122
imshow(log(abs(J)),[]), colormap(jet(64)), colorbar
>> J(abs(J) <64) = 0;
>> K = idct2(J);
imshow(I)
figure, imshow(K,[0 255])
>> err = immse(I,K);
>> err
err =
1.8432e+04
Output
Original Image
Image restored after compression
Page 106 of 122
>> I = imread('cameraman.tif');
>> T = dctmtx(8);
dct = @(block_struct) T * block_struct.data * T';
B = blockproc(I,[8 8],dct);
mask = [1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];
B2 = blockproc(B,[8 8],@(block_struct) mask .* block_struct.data);
invdct = @(block_struct) T' * block_struct.data * T;
I2 = blockproc(B2,[8 8],invdct);
imshow(I)
figure
imshow(I2)
Page 107 of 122
InputImage=I;
ReconstructedImage=I2;
n=size(InputImage);
Code:
>> I = imread('cameraman.tif');
>> f = @(x) uint8(round(mean2(x)*ones(size(x))));
I2 = blkproc(I,[2 2],f);
imshow(I)
figure, imshow(I2);
>> err = immse(I,I2);
>> B=size(I2);
err=
183.8470
OUTPUT
Original Image
Page 108 of 122
After 2X2 subdivision
Code for 8X8 subdivision
I = imread('cameraman.tif');
f = @(x) uint8(round(mean2(x)*ones(size(x))));
>> I2 = blkproc(I,[8 8],f);
>> imshow(I)
figure, imshow(I2);
>> C=size(I2);
>> err = immse(I,I2);
err =
733.2871
Output
Page 109 of 122
Code for 16 X 16
f = @(x) uint8(round(mean2(x)*ones(size(x))));
>> I2 = blkproc(I,[16 16],f);
>> imshow(I)
figure, imshow(I2);
>> D=size(I2);
>> err = immse(I,I2);
err =
1.0829e+03
Output:
Page 110 of 122
Code for 64X64
I = imread('cameraman.tif');
f = @(x) uint8(round(mean2(x)*ones(size(x))));
>> I2 = blkproc(I,[64 64],f);
>> imshow(I)
figure, imshow(I2);
>> err = immse(I,I2);
>> E=size(I2);
>> err =
2.2994e+03
Output
I = imread('cameraman.tif');
f = @(x) uint8(round(mean2(x)*ones(size(x))));
>> I2 = blkproc(I,[128 128],f);
>> imshow(I)
figure, imshow(I2);
>> F=size(I2);
>> err = immse(I,I2);
>> err =
Page 111 of 122
3.1064e+03
Output
Code:
I = imread('cameraman.tif');
f = @(x) uint8(round(mean2(x)*ones(size(x))));
>> I2 = blkproc(I,[256 256],f);
>> imshow(I)
figure, imshow(I2);
>> G=size(I2);
>> err = immse(I,I2);
>> err
err =
3.8865e+03
Page 112 of 122
Aim:
To subdivide an image of 256 X 256 into 8 X8 subimages.
>> I=imread('super-paper-mario.png');
>> %# desird number of horizontal/vertical tiles to divide the image into
>>numBlkH =8;
>> numBlkW = 8;
>> %# compute size of each tile in pixels
>>[imgH,imgW] = size(I);
>>szBlkH = [repmat(fix(imgH/numBlkH),1,numBlkH-1) imgH-
fix(imgH/numBlkH)*(numBlkH-1)];
>>szBlkW = [repmat(fix(imgW/numBlkW),1,numBlkW-1) imgW-
fix(imgW/numBlkW)*(numBlkW-1)];
%# divide into tiles, and linearize using a row-major order
C = mat2cell(I, szBlkH, szBlkW)';
C = C(:);
%# display tiles i subplots
figure, imshow(I)
Page 113 of 122
figure
for i=1:numBlkH*numBlkW
subplot(numBlkH,numBlkW,i), imshow( C{i} )
end
Output:
OriginalImage
Image divided into 8X8 sub images
Code:
>> T = dctmtx(8);
dct = @(block_struct) T * block_struct.data * T';
B = blockproc(I,[8 8],dct);
>> mask = [1 1 1 1 0 0 0 0
Page 114 of 122
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];
>> B2 = blockproc(B,[8 8],@(block_struct) mask .* block_struct.data);
>> invdct = @(block_struct) T' * block_struct.data * T;
I2 = blockproc(B2,[8 8],invdct);
>> imshow(I)
figure
imshow(I2)
>> InputImage=I;
>> ReconstructedImage=I2;
>> n=size(InputImage);
M=n(1);
N=n(2);
MSE = sum(sum((InputImage-ReconstructedImage).^2))/(M*N)
MSE =
0.0029
Page 115 of 122
Output
Original Image
The image reconstructed after DCT compression
Page 116 of 122
2.20 Capture a video footage of 1 min duration by a digital
camera(Mega Pixels>7.0) and convert it to frames(25frames/second)
and read all the frames one by one .Then detect the edges of the
moving objects in the frame.
Code:
clc;
close all;
clear all;
%nframes = length(video);
nframes=video.NumberOfFrames;
for i=1:nframes
mov(i).cdata=read(video,i)
%creating '.cdata' field to avoid much changes to previous code
end
temp = zeros(size(mov(1).cdata));
[M,N] = size(temp(:,:,1));
for i = 1:10
temp = double(mov(i).cdata) + temp;
end
imbkg = temp/10;centroidx = zeros(nframes,1);
centroidy = zeros(nframes,1);
Page 117 of 122
predicted = zeros(nframes,4);
actual = zeros(nframes,4);
R=[[0.2845,0.0045]',[0.0045,0.0455]'];
H=[[1,0]',[0,1]',[0,0]',[0,0]'];
Q=0.01*eye(4);
P = 100*eye(4);
dt=1;
A=[[1,0,0,0]',[0,1,0,0]',[dt,0,1,0]',[0,dt,0,1]'];
kfinit = 0;
th = 38;
for i=1:nframes
imshow(mov(i).cdata);
hold on
imcurrent = double(mov(i).cdata);
diffimg = zeros(M,N);
diffimg = (abs(imcurrent(:,:,1)-imbkg(:,:,1))>th) ...
| (abs(imcurrent(:,:,2)-imbkg(:,:,2))>th) ...
| (abs(imcurrent(:,:,3)-imbkg(:,:,3))>th);
labelimg = bwlabel(diffimg,4);
markimg = regionprops(labelimg,['basic']);
[MM,NN] = size(markimg);
for nn = 1:MM
if markimg(nn).Area > markimg(1).Area
tmp = markimg(1);
markimg(1)= markimg(nn);
Page 118 of 122
markimg(nn)= tmp;
end
end
bb = markimg(1).BoundingBox;
xcorner = bb(1);
ycorner = bb(2);
xwidth = bb(3);
ywidth = bb(4);
cc = markimg(1).Centroid;
centroidx(i)= cc(1);
centroidy(i)= cc(2);
hold on
rectangle('Position',[xcorner ycorner xwidth ywidth],'EdgeColor','b');
hold on
plot(centroidx(i),centroidy(i), 'bx');
kalmanx = centroidx(i)- xcorner;
kalmany = centroidy(i)- ycorner;
if kfinit == 0
predicted =[centroidx(i),centroidy(i),0,0]' ;
else
predicted = A*actual(i-1,:)';
end
kfinit = 1;
Ppre = A*P*A' + Q;
K = Ppre*H'/(H*Ppre*H'+R);
Page 119 of 122
actual(i,:) = (predicted + K*([centroidx(i),centroidy(i)]' - H*predicted))';
P = (eye(4)-K*H)*Ppre;
hold on
rectangle('Position',[(actual(i,1)-kalmanx)...
(actual(i,2)-kalmany) xwidth ywidth],'EdgeColor','r','LineWidth',1.5);
hold on
plot(actual(i,1),actual(i,2), 'rx','LineWidth',1.5);
drawnow;
end
OUTPUT:
Page 120 of 122
Page 121 of 122
Broad Conclusions and Discussions
References
[1] 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins Digital Image Processing Using
MATLAB, Prentice-Hall, 2004
[2] http://in.mathworks.com/help/images/ref/imnoise.html
[3] http://learnprotech.blogspot.in/2014/02/write-matlab-code-to-perform-contrast.html
[4] http://what-when-how.com/embedded-image-processing-on-the-tms320c6000-dsp/contrast-
stretching-image-processing/
[5]http://www.rocktheit.com/2012/09/matlab-program-to-plot-histogram.html
[6] http://in.mathworks.com/help/images/ref/imhist.html
[7] http://in.mathworks.com/help/matlab/ref/bar.html
[8] http://www.cs.uregina.ca/Links/class-info/425/Lab5/
[9] http://matlabgeeks.com/tips-tutorials/how-to-do-a-2-d-fourier-transform-in-matlab/
[10] http://www-rohan.sdsu.edu/doc/matlab/toolbox/images/im2bw.html
[11] http://in.mathworks.com/help/images/ref/im2bw.html
[12] http://in.mathworks.com/matlabcentral/answers/21527-edge-function-error
[13] http://in.mathworks.com/help/images/ref/graycomatrix.html
[14]Fundamentals of Digital Image Processing -A Practical Approach
with Examples in Matlab- Chris Solomon, Toby Breckon
[15]Fundamentals of Digital Signal Processing by Anil K Jain.
Page 122 of 122