DIP Zero To Hero Practice Manual
DIP Zero To Hero Practice Manual
TO
HERO
A RSATH N ATHEEM S
2
WHY I WROTE THIS BOOK
I wrote this book because Image processing is fresh and interesting topic
for Research work. This book is all about that how to develop theory and project
based notion about Image processing. This book contain plenty of programming
illustration that are analyzed can also be beneficial for learners and under
graduates pupils. This book starts from very basic Knowledge and gradually
cover all of the improvement topics of a Image processing with MATLAB
examples.
3
WHY YOU SHOULD READ THIS BOOK
This book will help you learn all about digital image processing
Importance, and necessity of image processing stems from application areas the
first being the Improvement of data for individual interpretation and the second
being that the Processing of a spectacle data for an machine perception. Digital
image processing includes a assortment of applications such as remote sensing,
image and information storage for transmission in acoustic imaging, medical
imaging, business applications , Forensic sciences and industrial automation.
Images are helpful in tracking of earth resources mapping, and forecast of urban
populations, agricultural crops, climate forecasting, flooding and fire control.
Space imaging applications include comprehension and analyzation of objects
contained in images obtained from deep space-probe missions. There are also
medical programs such as processing of X-Rays, Ultrasonic scanning, Electron
micrographs, Magnetic Resonance Imaging, Nuclear Magnetic Resonance
Imaging, etc.. In addition to the aforementioned applications, digital image
processing is being used to solve a variety of issues. Even unrelated, these
problems commonly require methods effective at improving information. The
Image processing Procedures like restoration and Image enhancement are used to
procedure images that were degraded or blurred. Powerful uses of image
processing concepts are observed in defense astronomy, biology, medical and
industrial applications. As per Medical Imaging is concerned almost all of the
pictures could be utilized in the discovery of tumors or for viewing the patients.
The current key field of use of digital image processing (DIP) methods is in
solving the issue of machine vision so as to attain superior results
4
TABLE OF CONTENTS
2 Image Segmentation 36
Thresholding 37
Application, Conclusion 65
3 Intensity Transformation 67
Introduction:Practical approach 84
Conclusion 104
5
MATLAB Source Code For Spatial Intensity 111
Resolution
12 Image Transformation
6
INTRODUCTION
What is MATLAB?
7
Workspace
Command Window
Command History
MATLAB Help
8
• MATLAB Help is an very powerful assistance to learning MATLAB
• Help not only contains the theoretic background, but also shows demos
for implementation
9
• Some command description can be found by typing the command in the
search field
• We can also use MATLAB Help from the command window as shown
Matrices in MATLAB
➢ A=[1 2 3; 4 5 6; 7 8 9];
➢ Creates matrix A of size 3 x 3
Special matrices:
10
Basic Operations on Matrices
- are case-sensitive
Logical Operators
11
• Example:
A=
735
621
Idx=
Flow Control
1. if statement
2. for loop
3. while loop
4. switch statement
5. break statement
12
Scripts and Functions
• Example:
n=length(x);
mean=sum(x)/n;
stdev=sqrt(sum((x-mean).^2)/n);
Defines a novel function called STAT that computes the mean and
standard deviation of a vector. Function name and file name should
be the SAME!
13
• mesh(x_ax,y_ax,z_mat) – view surface
Save mysession
Save mysession a b
Clear all
Clear a b
Load my session
% load session
14
What is the Image Processing Toolbox?
Images in MATLAB
▪ PCX (Paintbrush)
15
▪ raw-data and other types of image data
Images in MATLAB
16
Image Import and Export
img = imread('apple.jpg');
dim = size(img);
figure;
imshow(img);
Alternatives to imshow
imagesc(I)
imtool(I)
image(I)
Image Display
17
CHAPTER 1.
Introduction
DILATION
Dilation operator can be used to binary and grey scale images. The purpose
of this operator is to expand the window and shrinks background. It slowly
increases the boundaries of the region, while the small holes existing in the image
become smaller. It increases the illumination of the object.
Fig.1.Example: Dilation
18
Fig.1.1. Applied Structuring Element
EROSION
Erosion is significant operation. The purpose of erosion operators is to
shrinks the window and grows background. Erosion is used to make an object
shorter by eliminating is outside region of pixels. After implementing the erosion
operator on the image, the image gets darker. This particular operator will take
the image and structuring element as inputs and thins the subject.
Fig.1.2.Example: Erosion
19
Fig.1.3. Applied Structuring Element
OPENING
Opening operation is mixed of dilation and erosion operations. If A and are
two sets of pixels, then in the opening, 1st erode A by B then dilate the result by
B. Opening is the union of all B objects
Totally contained in A.
• Similar to Erosion
• Spot and noise removal
• Less destructive
• Erosion next dilation
• The similar structuring element for both operations.
Input:
• Binary Image
• Structuring Element.
20
Fig.1.4.Example: Opening
➢ Choose the structuring element (SE) and move it around inside each
highlight area.
➢ All highlight pixels which cannot be accessed by the structuring element
without lapping over the edge of the feature object will be eroded away
➢ All pixels which can be protected by the SE with the SE being completely
within the highlight region will be preserved.
➢ Opening is idempotent, repeating application has no further impact
21
CLOSING
Closing operation is a dilation operation adopted by an erosion
operation. Closing is actually the group of points, which the intersection of
object B about them with object A is not vacant.
➢ Choose the structuring element (SE) and move it around outside each
highlight area.
22
MATLAB PROGRAM FOR DILATION, EROSION, OPENING, CLOSING
AND THEIR PROPERTIES
SOURCE CODE:
clc;
clear all;
close all;
a=imread('C:\Users\natheem\Desktop\images\India_satellite.png');
b=strel('line',11,90);
figure(1);
imshow(a);
title('Original image');
c=imdilate(a,b);
figure(2);
imshow(c);
title('Dilate image');
23
d=strel('disk',11);
e=imerode(a,d);
figure(3);
imshow(e);
title('Erode image')
c=imopen(a,b);
figure(4);
imshow(c);
title('Open image');
d=strel('disk',11);
e=imclose(a,d);
figure(5);
imshow(e);
title('Close image')
%Properties
24
i1=imdilate(a,h);
figure(6);
imshow(i1);
i2=imerode(a,h);
figure(7);
imshow(i2);
i3=a-i2;
figure(8);
imshow(i3);
title('pro3');
i4=i1-a;
figure(9);
imshow(i4);
title('pro4');
25
i5=i1-i2;
figure(10);
imshow(i5);
title('pro5');
26
STEP BY STEP EXPLANATION
27
STEP 3: You will get Corresponding Output for Morphological Operation
28
RESULT:
Fig.1.Orignal Image
Fig.2.Dialating Image
29
Fig.3.Eroded Image
Fig.4.Opening Image
30
Fig.5.Closing Image
31
Fig.7.Erode Image property 2
Fig.8. property 3
32
Fig.9. property 1
33
FUTURE SCOPE
➢ Robotics:
Recognition and understanding of objects in a scene, motion control
and implementation through visual feedback
➢ Radar imaging:
• Target detection and identification.
• Selection of Structuring element for object classification over
morphology is still interesting to this technique and has been chosen
to be the main direction of the future work.
APPLICATIONS
Morphology is applied as a technique for image transformation. It
has been utilized for extraction of edges and detection of the attribute
objects in mobile photogrammetric techniques to making maps from
images taken from a car, named mobile mapping systems Morphology is
used primarily for decrease an region of interest and extracting particular
objects like street signals. Features of morphology are also used in
discovering sewer pipelines problems. Architectural commemorations as
34
well as industrial things have edges and areas which can be probably
detected by use of mathematical morphology functions.
CONCLUSION
35
CHAPTER 2.
IMAGE SEGMENTATION
What is segmentation?
1. Thresholding
36
THRESHOLDING
Types
g ( x, y) 01 if f(x,y)T
if f(x,y)T
37
• Calculate a fresh threshold:
1
T (m 1 m 2 )
2
38
Otsu’s Technique
• Based on a very simple idea: Find the threshold that minimizes the
weighted within-class variance.
• This turns out to be the same as maximizing the between-class variance.
• Operates directly on the gray level histogram.
Multiple Threshold
Disadvantage:
It becomes too complex when amount of region more than two or three.
39
Variable Thresholding
1) Image partitioning
It is work once the objects of interest and the background inhabit areas of
sensibly similar size. If not, it will fail.
3) By moving average
40
REGION BASED SEGMENTATION
1. Region Growing
2. Cheng-Jin Kuo`s method is used
3. Data Grouping (Clustering Technique)
4. Partitional clustering
Algorithm:
41
Reproduction of region growing (90% pixels )
42
Data Clustering
Hierarchical clustering
Diameter of cluster
43
Partitional clustering
44
ADVANTAGES & DISADVANTAGES
Hierarchical algorithm
Advantages
• Result is reliable
• Idea is simple
Disadvantages
Partitional algorithms
Advantages
Disadvantages
45
EDGE BASED SEGMENTATION
Edge-Based Segmentation
46
Gradient
f
g x x
f grad( f )
g y f
y
mag(f ) g x g y
gy
( x, y) tan 1
g x
Roberts
Robert edge detection, the perpendicular and horizontal edges carry out
separately and then place together for resulting edge detection. The Roberts edge
detector uses the next masks to approximate digitally the first derivatives as
differences between adjacent pixels.
47
Prewitt operator
Prewitt operator edge detection masks are the one of the oldest and
greatest understood methods of detecting edges in images The Prewitt edge
detector uses the following mask to approximate digitally the first derivatives Gx
and Gy.
Sobel operator
The sobel edge detector calculates the gradient by using the discrete
differences between rows and columns of a 3X3 neighborhood. The sobel
operator is based on convolving the image with a minor, divisible, and number
valued filter.
G( x, y) 1
0
if R ( x , y ) T
otherwise
48
Canny Edge Detection
49
The Marr-Hildreth edge detector (LoG)
Filter the input image with an n*n Gaussian low pass filter. 99.7% of the
capacity under a 2-D Gaussian surface lies between around the mean.
50
Short response Hilbert Transform (SRHLT)
1) Hilbert transform
1
g H ( ) h( x) * g ( x), where h( x)
x
GH ( f ) H ( f )G ( f ) H ( f ) j sgn( f )
51
Choose a suitable
HLT value differentiation
b0 b
52
Watersheds
Algorithm:
53
Markers
Outside markers:
Interior markers:
54
MATLAB GUI for Iris Segmentation
https://www.pantechsolutions.net/image-processing-projects/matlab-
code-for-iris-segmentation
https://www.dropbox.com/s/um4zhhb5qhfj9kt/matlab_code_for_k_means
_segmentation.rar?dl=0bb
55
MATLAB SOURCE CODE FOR IMAGE SEGMENTATION
Flow Chart
%IMAGE SEGMENTATION
clc;
clear all;
close all;
%edge detection
a=imread('autumn.tif');
b=rgb2gray(a);
56
figure(1);
imshow(b);
title('original image');
%roberts operator
c=edge(b,'roberts');
figure(2);
imshow(c);
title('roberts image');
%sobel operator
d=edge(b,'sobel');
figure(3);
imshow(d);
title('sobel image');
%prewitt operator
e=edge(b,'prewitt');
figure(4);
imshow(e);
57
title('prewitt image');
%log operator
f=edge(b,'log');
figure(5);
imshow(f);
title('log image');
%canny operator
g=edge(b,'canny');
figure(6);
imshow(g);
title('canny image');
%point detection
h=[0,1,0;1,-4,1;0,1,0];
i=imfilter(b,h,'symmetric','conv');
figure(7);
imshow(i);
58
%line detection
j=im2bw(b);
w=(1/3)*[1,0,-1;1,0,-1;1,0,-1];
k=imfilter(double(j),w);
k(k<1)=0;
figure(8);
imshow(k);
59
OUTPUT FOR IMAGE SEGMENTATION
60
Fig.3. Robert image
61
Fig.5. Prewitt Image
62
Fig.6. Log image
63
Fig.8. Laplacian Point Filtering Image
64
APPLICATION
1. Machine vision
2. Deep Learning
3. Medical imaging, including volume rendered images from CT (Computed
Tomography) and MRI (Magnetic Resonance Imaging).
4. Measure tissue volumes
5. Surgery planning
6. Intra-surgery navigation
7. Locate tumors and pathologies
8. Virtual surgery simulation
9. Diagnosis, learning of anatomical structure
10.Pedestrian recognition
11.Object recognition
12.Face detection
13.Face recognition
14.Recognition Tasks
15.Fingerprint recognition
16.Iris recognition
17.Traffic controller schemes
18.Brake light detection
19.Video investigation
20.Find objects in satellite images like roads, forests, crops.
65
CONCLUSION
Since edge detection is the first step in object boundary extraction and
object recognition, it is significant to know the differences between different edge
detection operators. from this section an attempt is made to review the edge
detection methods which are based on discontinuity intensity levels. The relative
presentation of various edge detection techniques is carried out with two images
by using MATLAB. It have been experiential that that the Canny edge detector
produces greater accuracy in detection of object edges with greater entropy,
PSNR, MSE and implementation time compared with Sobel, Roberts, Prewitt,
Zero crossing and LOG.
66
CHAPTER 3.
INTENSITY TRANSFORMATION
67
Grey Scale Manipulation
• Linear:
1. Negative
2. Identity
• Logarithmic:
1. Log
2. Inverse Log
• Power-Law:
1. nth power,
2. nth root.
68
Negative Image
S = (L – 1) – r
Log Transformation
• s = c log(1+r)
• c: constant
69
a) Fourier Spectrum b) Result of apply log transformation
70
Contrast Stretching
To increase the dynamic range of the gray levels in the image is being
processed.
• The locations of r1, s1 and r2, s2 control the shape of the transformation
function.
• If r1= s1 and r2= s2 the transformation is a linear function and produces no
changes.
• If r1=r2, s1=0 and s2=L-1, the transformation becomes a thresholding
function that creates a binary image.
• Intermediate values of r1, s1 and r2, s2 produce various degrees of spread
in the gray levels of the output image, thus affecting its contrast.
• Commonly, r1≤r2 and s1≤s2 is assumed.
71
Histogram Processing
The histogram of a digital image by gray levels from 0 to L-1 is a discrete
function h(rk)=nk, anywhere:
Histogram Equalization
k nj k
sk T (rk ) pr (rj )
j 0 n j 0
72
Histogram Matching (or Specification)
73
PRACTICAL APPROACH:
clc;
clear all;
close all;
a=imread('C:\Users\natheem\Desktop\images\mri.jpg');
subplot(2,2,1);
imshow(a);
title('original image');
%negative image
b=255-a;
subplot(2,2,2);
imshow(b);
74
title('negative image');
%log transform
l=255;
d=l/log10(1+l);
e=d*log10(1+double(a));
f=uint8(e);
subplot(2,2,3);
imshow(f);
title('log transform');
gamma=1.1;
g=double(a).^gamma;
subplot(2,2,4);
imshow(uint8(g));
75
RESULT
76
MATLAB PROGRAM USING INDIVIDUAL FIGURE
clc;
clear all;
close all;
a=imread('C:\Users\natheem\Desktop\images\mri.jpg');
figure(1);
imshow(a);
title('original image');
%negative image
b=255-a;
figure(2);
imshow(b);
title('negative image');
%log transform
l=255;
d=l/log10(1+l);
77
e=d*log10(1+double(a));
f=uint8(e);
figure(3);
imshow(f);
title('log transform');
gamma=1.1;
g=double(a).^gamma;
figure(4);
imshow(uint8(g));
78
RESULT
Fig.2.1.1.Original Image
Fig.2.1.2.Negative Image
79
Fig.2.1.3.Log Transformation
80
OUTPUT FOR X-RAY IMAGE USING SUBPLOT
a) Original Image
b) Negative Image
c) Log transformation
d) Power law transformation
81
OUTPUT FOR X-RAY IMAGE USING SEPARATE FIGURE
82
Fig.2.2.3 Log Transformation
83
CHAPTER 4.
HISTOGRAM EQUALIZATION
INTRODUCTION:
84
Digital Image
Histogram of an image
85
The histogram displays us that the image contains only a fraction of the
entire range of gray levels. In this case there are 256 gray levels and the
image only has values Between 50 to 100. Therefore this image has low
contrast.
A good histogram is that which covers all the expected values in the gray
scale used. This type of histogram proposes that the image has good
contrast and that particulars in the image may be observed more easily.
86
Methods for histogram equalization
• Histogram expansion
• Local area histogram equalization (LAHE)
• Cumulative histogram equalization
• Par sectioning
• Odd sectioning
87
Histogram Processing
The histogram of a digital image with gray levels from 0 to L-1 is a discrete
function h(rk)=nk, wherever:
Histogram Equalization
k nj k
sk T (rk ) pr (rj )
j 0 n j 0
88
Fig.1 Dark Image vs High contrast histogram image
89
Fig.2. Histogram Equalized image
90
Histogram Matching (or Specification)
91
PRACTICAL APPROACH
FLOW CHART
92
MATLAB SOURCE CODE FOR HISTOGRAM EQUALIZATION
clc;
clear all;
close all;
a=imread('C:\Users\nadeem\Desktop\palace.png');
%histogram processing
subplot(3,2,1);
imshow(a);
title('original image');
%converting to gray
subplot(3,2,3);
b=rgb2gray(a);
imshow(b);
title('grayscale image');
93
%equalisation of image
subplot(3,2,4);
c=histeq(b);
imshow(c);
title('equalised image');
subplot(3,2,5);
imhist(b);
subplot(3,2,6);
imhist(c);
94
OUTPUT
95
Fig.1 Original Image (Low contrast | Dark Image)
96
Fig.4 Corresponding Histogram plot of grayscale image
97
Fig.5 Corresponding Histogram plot of Equalized image
98
HISTOGRAM EQUALIZATION USING COLOR IMAGE
clc;
clear all;
close all;
a=imread('C:\Users\natheem\Desktop\images\monky.jpg');
%histogram processing
subplot(3,3,1);
imshow(a);
title('Original image');
%converting to gray
subplot(3,3,2);
b=rgb2gray(a);
imshow(b);
title('Grayscale image');
%equalisation of image
99
subplot(3,3,3);;
c=histeq(b);
imshow(c);
title('Equalised image');
subplot(3,3,5);
imhist(b);
subplot(3,3,6);
imhist(c);
RESULT
100
OUTPUT WITH SEPARATE FIGURE
Fig.2.Grayscale image
101
Fig.3.Histogram equalized image
102
Fig.5. Histogram plot of Equalized image
103
CONCLUSION
Histogram equalization is a direct image processing technique often used
to achieve better quality images in black and white color balances in medical
applications such as X-rays, MRIs, and CT scans. All these images require high
definition and contrast of colors to determine the pathology that is being
experiential and reach a diagnosis. Though, in some type of images histogram
equalization can show noise hidden in the image after the processing is done.
104
CHAPTER 5.
INTRODUCTION
Gray level resolution: This refers to the smallest visible change in gray level.
The measurement of visible changes in gray level is a extremely subjective
procedure.
105
Functional Representation of Images
106
•
107
108
109
Image Enhancement in Spatial Domain Find gray level transformation
function T(r) to obtains (x,y) =T(f(x,y)) processed image from input
image.
REASONS
1. Contrast enhancement
2. Image understanding
3. Visual improvement
Picture Reference:
110
MATLAB SOURCE CODE FOR SPATIAL INTENSITY RESOLUTION
clc;
clear all;
close all;
a=imread('C:\Users\natheem\Desktop\images\satelite.jpg');
subplot(3,2,1);
imshow(a);
title('original image');
subplot(3,2,2);
imshow(grayslice(a,128),gray(128));
subplot(3,2,3);
imshow(grayslice(a,64),gray(64));
subplot(3,2,4);
imshow(grayslice(a,32),gray(32));
111
title('32 graylevel image');
subplot(3,2,5);
imshow(grayslice(a,16),gray(16));
subplot(3,2,6);
imshow(grayslice(a,8),gray(8));
Result
112
MATLAB PROGRAM USING INDIVIDUAL FIGURE
clc;
clear all;
close all;
a=imread('C:\Users\natheem\Desktop\images\satelite.jpg');
figure(1);
imshow(a);
title('original image');
figure(2);
imshow(grayslice(a,128),gray(128));
figure(3);
imshow(grayslice(a,64),gray(64));
figure(4);
113
imshow(grayslice(a,32),gray(32));
figure(5);
imshow(grayslice(a,16),gray(16));
%8 graylevel image
figure(6);
imshow(grayslice(a,8),gray(8));
114
Output
115
116
117
118
CHAPTER 6.
Introduction
119
Mechanics of spatial filtering
• The process contains simply of moving the filter mask from point to point
in an image.
• At each point (x,y) the response of the filter at that point is calculated using
a predefined relationship
a b
g ( x, y) w(s, t ) f ( x s, y t )
s at b
• The result is the sum of products of the mask coefficients with the
corresponding pixels directly under the mask
• The coefficient w(0,0) coincides with image value f(x,y), indicating that
the mask is centered at (x,y) when the computation of sum of products
takes place.
• For a mask of size mxn, we assume that m-2a+1 and n=2b+1, where a and
b are nonnegative integer. Then m and n are odd.
120
Nonlinear spatial filtering
• Smoothing filters are used for blurring and for noise reduction.
• Blurring is used in preprocessing stages, such as removal of small details
from an image prior to object extraction, and bridging of small gaps in
lines or curves
• Noise reduction can be accomplished by blurring
121
Smoothing Linear Filters
• Linear spatial filter is just the average of the pixels contained in the
neighborhood of the filter mask.
• Sometimes it is called “averaging filters”.
• The idea is substituting the value of every pixel in an image by the
average of the gray levels in the neighborhood defined by the filter
mask.
• The general implementation for filtering an MxN image with a
weighted averaging filter of size mxn is given by the expression
•
a b
w(s, t ) f ( x s, y t )
g ( x, y ) s at b
a b
w(s, t )
s at b
122
5x5 Smoothing Linear Filters
123
Order-Statistics Filters
124
Result of median filter
f
f ( x 1) f ( x)
• x
• Must be zero in flat segments
• Must be nonzero at the onset of a gray-level step or ramp; and
• Must be nonzero along ramps.
125
Definition for a second derivative
2 f
f ( x 1) f ( x 1) 2 f ( x).
x 2
Analyze
• The 1st-order derivative is nonzero along the whole ramp, while the 2nd-
order derivative is nonzero only at the start and end of the ramp.
• The reply at and around the point is much stronger for the 2nd- than for
the 1st-order derivative
126
MATLAB SOURCE CODE FOR IMAGE ENHANCEMENT IN SPATIAL
FILTERING
clc;
clear all;
close all;
a=imread('C:\Users\natheem\Desktop\images\mri.jpg');
b=double(a)+50;
subplot(3,3,1);
imshow(a);
title('original image');
subplot(3,3,2);
imshow(uint8(b));
title('enhanced image');
b1=double(a-70);
subplot(3,3,3);
imshow(uint8(b1));
e=a*.5;
f=a*.20;
127
subplot(3,3,4);
imshow(e);
title('increased in contrast');
subplot(3,3,5);
imshow(f);
title('decreased in contrast');
h1=1/9*ones(3,3);
h2=1/25*ones(5,5);
b1=conv2(a,h1,'same');
b2=conv2(a,h2,'same');
subplot(3,3,6);
imshow(uint8(b1));
subplot(3,3,7);
imshow(uint8(b2));
128
Result
129
MATLAB Source Code for using separate figure
130
131
132
133
CHAPTER 7.
Spatial Domain
Frequency Domain
134
Major filter categories
1) Low-pass
2) High-pass
3) Band-pass
4) Band-stop
Example
135
Low-pass filters (Smoothing Filters):
frequency time
domain domai
Example:
136
Example
Band-Stop filters
137
Frequency Domain Methods
138
Case 1: h(u,v) is stated in the frequency domain.
STEPS
139
140
Types of Low Pass (LP) Filters
4. Difference of Gaussians
141
MATLAB SOURCE CODE FOR IMAGE ENHANCEMENT IN FREQUENCY FILTERING
clc;
clear all;
close all;
a=imread('coins.png');
[m,n]=size(a);
mask=zeros(m,n);
for i=150:180
for j=210:240
mask (i,j)=i;
end
end
c=fftshift(mask);
b=fft2(a);
d=b.*c;
e=abs(ifft2(b));
figure(1);
subplot(3,3,1);
142
imshow(a);
title('original image');
subplot(3,3,2);
imshow(uint8(e));
subplot(3,3,3);
imshow(mask);
subplot(3,3,4);
imshow(c);
a=imread('coins.png');
[m,n]=size(a);
mask=ones(m,n);
for i=150:180
for j=210:240
mask (i,j)=0;
end
end
c=fftshift(mask);
143
b=fft2(a);
d=b.*c;
e=abs(ifft2(b));
subplot(3,3,5);
imshow(a);
title('original image');
subplot(3,3,6);
imshow(uint8(e));
subplot(3,3,7);
imshow(mask);
subplot(3,3,8);
imshow(c);
a=imread('coins.png');
[m,n]=size(a);
mask=zeros(m,n);
for i=160:170
for j=260:230
144
mask (i,j)=0;
end
end
c=fftshift(mask);
b=fft2(a);
d=b.*c;
e=abs(ifft2(b));
figure(2);
subplot(2,2,1);
imshow(a);
title('original image');
subplot(2,2,2);
imshow(uint8(e));
subplot(2,2,3);
imshow(uint8(mask));
subplot(2,2,4);
imshow(uint8(c));
145
a=imread('coins.png');
[m,n]=size(a);
mask=ones(m,n);
for i=150:180
for j=210:240
mask(i,j)=0;
end
end
c=fftshift(mask);
b=fft2(a);
d=b.*c;
e=abs(ifft2(b));
figure(3);
subplot(2,2,1);
imshow(a);
title('original image');
subplot(2,2,2);
imshow(uint8 (e));
subplot(2,2,3);
146
imshow(mask);
subplot(2,2,4);
imshow(c);
Result
147
148
CHAPTER 8.
1. Pseudo-color processing
149
Full color processing
Pseudo-color processing
• In the past period, color sensors and processing hardware are not
available
• Colors are assigned to a range of monochrome intensities
•
Color fundamentals
1) Color models
2) Pseudo-color image processing
3) Color transformations
4) Smoothing and sharpening
1) COLOR FUNDAMENTALS
Physical phenomenon
Psysio-psychological phenomenon
150
Visible light
151
The color that human perceive in an object = the light reflected from
the object
Radiance:
Total amount of energy that flow from the light source, measured in
watts (W)
Luminance:
Amount of energy an observer perceives from a light source,
measured in lumens far infrared light: high radiance, but 0 luminance
Brightness:
Subjective descriptor that is hard to measure, alike to the achromatic
view of intensity
152
In 1931, CIE (International Commission on Illumination) defines
specific wavelength values to the
153
Primary colors
154
Color TV
2) COLOR MODELS
155
RGB color model
Pixel depth:
The number of bits used to represent each pixel in RGB space
Full-color image:
24-bit RGB color image
(R, G, B) = (8 bits, 8 bits, 8 bits)
156
CMY model (+Black = CMYK)
157
Pseudo-color image processing
Application 1
Application 2
158
Application 3
159
Gray level to color transformation
160
3) Color transformation
g(x,y)=T[f(x,y)]
• Color transformation
Example:
• Intensity: s3 = k r3
161
3) CMY color space
162
Color image smoothing
Neighborhood processing
163
MATLAB SOURCE CODE FOR COLOR IMAGE PROCESSING
clc;
clear all;
close all;
a=imread('peppers.png');
subplot(3,3,1);
imshow(a);
title('original image');
g=rgb2gray(a);
subplot(3,3,2);
imshow(g);
title('gray conversion');
ntsc_img=rgb2ntsc(a);
subplot(3,3,3);
imshow(ntsc_img);
ycbcr_img=rgb2ycbcr(a);
subplot(3,3,4);
imshow(ycbcr_img);
164
title('ycbcr colour space conversion');
hsv_img=rgb2hsv(a);
subplot(3,3,5);
imshow(hsv_img);
cmy_img=imcomplement(a);
subplot(3,3,6);
imshow(cmy_img);
rgb=im2double(a);
r=rgb(:,:,1);
g=rgb(:,:,2);
b=rgb(:,:,3);
num=.5*((r-g)+(r-b));
den=sqrt(r-g).^2+(r-g).*(g-b);
theta=acos(num./(den +eps));
H=theta;
H(b>g)=2*pi-H(b>g);
H=H/(2*pi);
num=min(min(r,g),b);
165
den=r+g+b;
den(den==0)=eps;
S=1-3.*num./den;
H(S==0)=0;
I=(r+g+b)/3;
hsi=cat(3,H,S,I);
subplot(3,3,7);
imshow(hsi);
title('HSI');
figure(2);
subplot(2,3,1);
imshow(r);
title('red component');
subplot(2,3,2);
imshow(g);
title('green component');
subplot(2,3,3);
imshow(b);
title('blue component');
166
subplot(2,3,4);
imshow(H);
title('hue component');
subplot(2,3,5);
imshow(S);
title('saturation component');
subplot(2,3,6);
imshow(I);
title('brightness component');
167
OUTPUT
168
MATLAB PROGRAM USING INDIVIDUAL FIGURE
clc;
clear all;
close all;
a=imread('peppers.png');
figure(1);
imshow(a);
title('original image');
g=rgb2gray(a);
figure(2);
imshow(g);
title('gray conversion');
ntsc_img=rgb2ntsc(a);
figure(3);
imshow(ntsc_img);
ycbcr_img=rgb2ycbcr(a);
figure(4);
imshow(ycbcr_img);
169
title('ycbcr colour space conversion');
hsv_img=rgb2hsv(a);
figure(5);
imshow(hsv_img);
cmy_img=imcomplement(a);
figure(6);
imshow(cmy_img);
rgb=im2double(a);
r=rgb(:,:,1);
g=rgb(:,:,2);
b=rgb(:,:,3);
num=.5*((r-g)+(r-b));
den=sqrt(r-g).^2+(r-g).*(g-b);
theta=acos(num./(den +eps));
H=theta;
H(b>g)=2*pi-H(b>g);
H=H/(2*pi);
num=min(min(r,g),b);
170
den=r+g+b;
den(den==0)=eps;
S=1-3.*num./den;
H(S==0)=0;
I=(r+g+b)/3;
hsi=cat(3,H,S,I);
figure(7);
imshow(hsi);
title('HSI');
figure(8);
subplot(2,3,1);
imshow(r);
title('red component');
figure(9);
imshow(g);
title('green component');
figure(10);
imshow(b);
title('blue component');
171
figure(11);
imshow(H);
title('hue component');
figure(12);
imshow(S);
title('saturation component');
figure(13);
imshow(I);
title('brightness component');
172
OUTPUT
173
174
175
176
177
178
179
CHAPTER 9.
Fourier Transform
Periodic Signals
Fundamental period,
Fundamental frequency:
f0 = 1/T0
ω0 = 2π/T0 = 2πf0
180
Periodicity in Biology and Medicine
181
Fourier analysis
Fourier series
Fourier Transform
182
Fast Fourier Transform (FFT)
• (u, v) are the frequency coordinates while (x, y) are the spatial
coordinates
183
Fast Fourier Transform (FFT)
184
DFT in MATLAB
Let fbe a 2D image with dimension [M,N], then its 2D DFT can be
computed as follows:
Df = fft2(f,M,N);
• fft2 puts the zero-frequency component at the top-left corner.
Df = fft2(f,size(f,1), size(f,2));
Df2 = fftshift(Df);
Result
185
Examine the Fourier transform of a synthetic image
f = ones(10,20);
F = fft2(f, 500,500);
f1 = zeros(500,500);
f1(240:260,230:270) = 1;
subplot(2,2,1);
imshow(f1,[]);
S = abs(F);
subplot(2,2,2);
imshow(S,[]);
Fc = fftshift(F);
S1 = abs(Fc);
186
subplot(2,2,3);
imshow(S1,[]);
S2 = log(1+S1);
subplot(2,2,4);
imshow(S2,[]);
Result
187
Fourier transform of natural images
f = imread(‘lenna.jpg’);
subplot(1,2,1);
imshow(f);
f = double(f);
F = fft2(f);
Fc = fftshift(F);
S = log(1+abs(Fc));
Subplot(1,2,2);
imshow(S,[]);
188
Result
a. Original image
189
MATLAB SOURCE CODE FOR DFT ANALYSIS
clc;
clear all;
close all;
a=zeros(256);
[m,n]=size(a);
for i=120:145
for j=120:145
a(i,j)=225;
end;
end;
b=imrotate(a,45,'bilinear','crop');
a1=log(1+abs(fftshift(fft2(a))));
b1=log(1+abs(fftshift(fft2(b))));
subplot(2,2,1);
imshow(a);
title('Orignal Image');
subplot(2,2,2);
imshow(b);
title('Rotate Image');
190
subplot(2,2,3);
imshow(mat2gray(a1));
subplot(2,2,4);
imshow(mat2gray(b1));
RESULT
191
CHAPTER 10.
Thresholding
Types
g ( x, y) 01 if f(x,y)T
if f(x,y)T
192
• Compute a new threshold:
1
T (m 1 m 2 )
2
193
Otsu’s Method
• Based on a very simple idea: Find the threshold that minimizes the
weighted within-class variance.
• This turns out to be the same as maximizing the between-class
variance.
• Operates directly on the gray level histogram.
Assumptions
• Histogram (and the image) are bimodal.
• No usage of spatial coherence, nor any other idea of object structure.
• Assumes stationary statistics, but can be modified to be locally
adaptive. (exercises)
• Assumes uniform illumination (indirectly), so the bimodal
illumination behavior arises from object appearance changes only.
194
Multiple Threshold
Disadvantage:
It becomes too complex when number of area more than two or three.
195
Variable Thresholding
• Image partitioning
196
197
MATLAB SOURCE CODE FOR BASIC THRESHOLDING
FUNCTION
clc;
clear all;
close all;
i=imread('cell.tif');
subplot(2,2,1);
imshow(i);
title('original image');
subplot(2,2,2);
im2bw(i,0.35);
[counts,x]=imhist(i);
p=polyfit(x,counts,6);
y=polyval(p,x);
[v,ind]=sort(abs(diff(y)));
thresh=ind(3)./255;
198
subplot(2,2,3);
im2bw(i,thresh);
level=graythresh(i);
subplot(2,2,4);
im2bw(i,level);
title('otsu method');
figure;
plot(x,counts);
hold on,plot(x,y,'r');
title('graph');
199
RESULT
200
MATLAB Code for Binary Thresholding Function & Histogram
clc;
clear all;
close all;
count=0;
a=imread('cell.tif');
t=mean2(a);
done=false;
while done
count=count+1;
g=f>t;
tnext=0.5*(mean(f(g)));
mean(a(~g));
done=abs(t-tnext)<0.5;
t=tnext;
end;
count=2;
t=125.3860;
g=im2bw(a,t/255);
201
subplot(2,2,1);
imshow(a);
title('original image');
subplot(2,2,2);
imhist(a);
title('histogram image');
subplot(2,2,3);
imshow(g);
title('binary image');
OUTPUT
202
CHAPTER 11.
Image representation
Typically, a 2-D projection of the 3-D space is used, but the image can exist
in the 3-D space directly.
203
Digitalization
1. Sampling, and
2. Quantization.
Sampling
204
Quantization
205
MATLAB SOURCE CODE FOR SAMPLING AND QUANTIZATION
clc;
clear all;
close all;
a=imread('trees.tif');
f=ind2gray(a,gray(256));
f=f(1:256,1:256);
[N,M]=size(f);
m=7;
w=1/m;
F=fftshift(fft2(f));
for i=1:N
for j=1:N
r2=(i-round(N/2))^2+(j-round(N/2))^2;
if(r2>round(N/2*w)^2)F(i,j)=0;
end;
end;
end;
Id=real(ifft2(fftshift(F)));
206
Id=imresize(Id,[N/m,N/m],'nearest');
m=10;
[N,M]=size(f);
Iu=zeros(m*N,m*N);
for i=1:N
for j=1:N
Iu(m*(i-1)+1,m*(j-1)+1)=f(i,j);
end;
end;
[N,M]=size(Iu);
w=1/m;
F=fftshift(fft2(Iu));
for i=1:N
for j=1:N
r2=(i-round(N/2))^2+(j-round(N/2))^2;
if(r2>round(N/2*w)^2)F(i,j)=0;
end;
end;
end;
Iu=(m*m)*abs(ifft2(fftshift(F)));
207
figure;imshow(a);
title('original image');
figure;imshow(f);
title('gray image');
figure;imshow(Id);
title('Idown image');
figure;imshow(Iu);
title('Iup image');
208
209
210
CHAPTER 12.
IMAGE TRANSFORMATION
211
2. Discrete Cosine Transform (DCT)
Example:
212
There are two ways to compute DCT using image processing
toolbox, first method is to use dct2 function, dct2 uses an FFT-based
algorithm for quick computation with big inputs, The 2nd method is to use
the DCT transform matrix, which is returned by the function dctmtx and
might be more effectual for small square inputs, such as 8-by-8 or 16-by-
16.
Hadamard function:
213
Application of Walsh hadamard transform:
✓ Speech processing,
✓ filtering and
✓ power spectrum analysis.
Definition of IFWHT:
Applications of Transforms
• feature extraction and representation
• Enhancement and Compression
• pattern recognition,e.g.,eigen faces
dimensionality reduction
• Analyse the principal (“dominating”) components
214
Radon transform
215
MATLAB SOURCE CODE FOR IMAGE TRANSFORMATION
clc;
clear all;
close all;
a=imread('mri.tif');
figure,imshow(uint8(a));
title('original image');
a=double(a);
[s1,s2]=size(a);
bs=8;
%waslh matrix
hadamardMatrix=hadamard(bs);
M=log2(bs)+1;
hadIdx=0:bs-1;
binhadIdx=fliplr(dec2bin(hadIdx,M))-'0';
binseqIdx=zeros(bs,M-1);
for k=M:-1:2
binseqIdx(:,k)=xor(binhadIdx(:,k),binhadIdx(:,k-1));
end;
216
seqIdx=binseqIdx*pow2((M-1:-1:0)');
walshMatrix=hadamardMatrix(seqIdx+1,:);
%walsh
temp_walsh=double(zeros(size(a)));
for y=1:bs:s1-bs+1
for x=1:bs:s2-bs+1
croppedImage=a((y:y+bs-1),(x:x+bs-1));
h=walshMatrix;
m=log2(bs);
t=(1/(2^m))*h*croppedImage*h';
temp_walsh((y:y+bs-1),(x:x+bs-1))=t;
end;
end;
figure(2),subplot(1,2,1);
imshow(uint8(temp_walsh));
%inverse walsh
temp_inwalsh=double(zeros(size(a)));
for y=1:bs:s1-bs+1
for x=1:bs:s2-bs+1
croppedImage=temp_walsh((y:y+bs-1),(x:x+bs-1));
h=walshMatrix;
217
m=log2(bs);
t=(1/(2^m))*h*croppedImage*h;
%t=getInwalshtransform(croppedImage,bs);
temp_inwalsh((y:y+bs-1),(x:x+bs-1))=t;
end;
end;
subplot(1,2,2);
imshow(uint8(temp_inwalsh));
%hadamard
temp=double(zeros(size(a)));
for y=1:bs:s1-bs+1
for x=1:bs:s2-bs+1
croppedImage=a((y:y+bs-1),(x:x+bs-1));
N=bs;
h=hadamard(N);
m=log2(N);
t=(1/(2^m))*h*croppedImage*h';
%t=getInwalshtransform(croppedImage,bs);
temp((y:y+bs-1),(x:x+bs-1))=t;
end;
end;
218
figure(3),subplot(1,2,1);
imshow(uint8(temp));
%inverse hadamard
temp1=double(zeros(size(a)));
for y=1:bs:s1-bs+1
for x=1:bs:s2-bs+1
croppedImage=temp((y:y+bs-1),(x:x+bs-1));
h=hadamard(N);
m=log2(N);
t=(1/(2^m))*h'*croppedImage*h;
%t=getInvhadamardtransform(croppedImage,bs);
temp1((y:y+bs-1),(x:x+bs-1))=t;
end;
end;
subplot(1,2,2);
imshow(uint8(temp1));
%dct
temp_dct=double(zeros(size(a)));
for y=1:bs:s1-bs+1
for x=1:bs:s2-bs+1
219
croppedImage=a((y:y+bs-1),(x:x+bs-1));
s=dctmtx(N);
t=s*croppedImage*s';
%t=getdcttransform(croppedImage,bs);
temp_dct((y:y+bs-1),(x:x+bs-1))=t;
end;
end;
figure(4),subplot(1,2,1);
imshow(uint8(temp_dct));
%inverse dct
temp_invdct=double(zeros(size(a)));
for y=1:bs:s1-bs+1
for x=1:bs:s2-bs+1
croppedImage=temp_dct((y:y+bs-1),(x:x+bs-1));
s=dctmtx(N);
t=s'*croppedImage*s;
%t=getInvdcttransform(croppedImage,bs);
temp_invdct((y:y+bs-1),(x:x+bs-1))=t;
end;
end;
subplot(1,2,2);
220
imshow(uint8(temp_invdct));
%haar matrix
a=1/sqrt(N);
for i=1:N
H(1,i)=a;
end;
for k=1:N-1
p=fix(log2(k));
q=k-2^p+1;
t1=N/2^p;
sup=fix(q*t1);
mid=fix(sup-t1/2);
inft=fix(sup-t1);
t2=2^(p/2)*a;
for j=1:inft
H(k+1,j)=0;
end;
for j=inft+1:mid
H(k+1,j)=t2;
end;
for j=mid+1:sup
221
h(k+1,j)=-t2;
end;
for j=sup+1:N
H(k+1,j)=0;
end;
end;
%haar
temp_haar=double(zeros(size(a)));
for y=1:bs:s1-bs+1
for x=1:bs:s2-bs+1
croppedImage=a((y:y+bs-1),(x:x+bs-1));
h=H;
m=log2(N);
t=h*croppedImage*h';
%t=gethaartransform(croppedImage,bs);
temp_haar((y:y+bs-1),(x:x+bs-1))=t;
end;
end;
figure(5),subplot(1,2,1);
imshow(unit8(temp_haar));
%inverse haar
222
temp_invhaar=double(zeros(size(a)));
for y=1:bs:s1-bs+1
for x=1:bs:s2-bs+1
croppedImage=temp_haar((y:y+bs-1),(x:x+bs-1));
h=H;
t=h'*croppedImage*h;
%t=getInvhaartransform(croppedImage,bs);
temp_invhaar((y:y+bs-1),(x:x+bs-1))=t;
end;
end;
subplot(1,2,2);
imshow(unit8(temp_invhaar));
223
OUTPUT FOR IMAGE TRANSFORMATION\
224
225
APPENDIX
Top 100+ Image Processing Projects - Free Source Code and Abstracts
https://www.pantechsolutions.net/blog/category/image-video-
processing/amp/
https://www.pantechsolutions.net/blog/top-100-image-processing-projects-
free-source-code/amp/
https://electronicsforu.com/electronics-projects/software-projects-
ideas/image-processing-using-matlab
https://in.mathworks.com/discovery/digital-image-processing.html
https://in.mathworks.com/products/image.html
https://in.mathworks.com/products/image/code-examples.html
https://in.mathworks.com/store
226
REFERENCE:
[2] Ashley R. Clark and Colin N Eberhardt. Microscopy Techniques for Materials
Science. CRC Press, Boca Raton, Fl, 2002.
[3] Rafael Gonzalez and Richard E. Woods. Digital Image Processing. Addison-
Wesley, second edition, March, 2017.
[4] Robert M. Haralick and Linda G. Shapiro. Computer and Robot Vision. Addison-
Wesley, 1993.
[5] James D. Foley, Andries van Dam, Steven K. Feiner, John F. Hughes, and Richard
L. Phillips. Introduction to Computer Graphics. Addison-Wesley, 1994.
[7] William K. Pratt. Digital Image Processing. John Wiley and Sons, second edition,
1991.
[8] Jae S. Lim. Two-Dimensional Signal and Image Processing. Prentice Hall, 1990.
[9] Majid Rabbani and Paul W. Jones. Digital Image Compression Techniques. SPIE
Optical Engineering Press, 1991.
[10] Jean Paul Serra. Image analysis and mathematical morphology. Academic Press,
1982.
[12] Azriel Rosenfeld and Avinash C. Kak. Digital Picture Processing. Academic
Press, second edition, 1982.
227
[13] Melvin P. Siedband. Medical imaging systems. In John G. Webster, editor,
Medical instrumentation : application and design, pages 518–576. John Wiley and
Sons, 1998.
[14] Milan Sonka, Vaclav Hlavac, and Roger Boyle. Image Processing, Analysis and
Machine Vision. PWS Publishing, second edition, 1999.
[15] Dominic Welsh. Codes and Cryptography. Oxford University Press, 1989.
[17] https://www.pantechsolutions.net/blog/category/image-video-processing/amp/
[18] https://www.pantechsolutions.net/blog/top-100-image-processing-projects-free-
source-code/amp/
[19] https://in.mathworks.com/discovery/digital-image-processing.html
228
ABOUT THE AUTHOR
Arsath Natheem is an indian Biomedical Engineer and Youtuber who works primarily
in the field of Artificial intelligence, He is best known for his multimedia Presentation about
"How the Biomedical Engineers Save the life" at Velalar College of Engineering and
Technology in Tamilnadu, he was awarded the best project holder for IoT Based Voice
Recognition Robot and also presented his project at Adhiyamaan college of engineering and
Technology and won the first prize for his project. He participated project competition at
Madras institute of technology (MIT) in Chennai, now he pursuing final year BE Biomedical
Engineering at Velalar College of Engineering and Technology, He's field of interest is an
Artificial Intelligence it's specifically applicable for Medical Diagnosis.
229
ONE LAST THING…
If you enjoyed this book or found it useful I’d be very grateful if you’d post a short
review on Amazon. Your support really does make a difference and I read all the
reviews personally so I can get your feedback and make this book even better.
If you’d like to leave a review then all you need to do is click the review link on this
book’s page on Amazon here: http://amzn.to/2jXcHfL
230