Dip Matlab
Dip Matlab
4,200
Open access books available
116,000
International authors and editors
125M
Downloads
154
Countries delivered to
TOP 1%
most cited scientists
12.2%
Contributors from top 500 universities
Mahmut Sіnecen
http://dx.doi.org/10.5772/63028
Abstract
The chapter relates to the Image Processing Toolbox in MATLAB. We learn about its
general information and some examples will be solved using it. After finishing this
chapter, you can use MATLAB Image Processing Toolbox and write script for
processing of images.
The image may be defined as a two‐dimensional visual information that are stored and displayed.
An image is created by photosensitive devices which capture the reflection light from two‐
dimensional surface of object in the three‐dimensional real world (Figure 1). Each image has
intensity or gray value in x – y coordinate plane. If it is finite and discrete quantities, image is
called digital image. In Figure 2, some digital images are shown.
Digital image processing (DIP) has the different techniques for processing of digital images.
DIP has been applying many fields with technological advances, such as Medicine, Geograph‐
ical Information Technologies, Space Sciences, Military Applications, Security, Industrial
Applications.
1.1. Pixel
Pixels, which are called pel or picture elements, may be defined as the smallest addressable
element in the digital image. Pixels of a color image have Red, Green, and Blue gray values
(Figure 3).
© 2016 The Author(s). Licensee InTech. This chapter is distributed under the terms of the Creative Commons
Attribution License (http://creativecommons.org/licenses/by/3.0), which permits unrestricted use, distribution,
and reproduction in any medium, provided the original work is properly cited.
2 Applications from Engineering with MATLAB Concepts
Figure 1. Image.
A pixel has three different neighbor types that are 4, 8, and diagonal. As shown in Table 1,
neighbor of a pixel (p) in the x, y point of image (f) is defined in that 4‐neighbors;
N4(p) is shown as 4‐neighbor of p pixel. Any pixel p in the image has two vertical and horizontal
neighbors, and each of them is a unit distance of p, given by
Diagonal neighbors;
Although diagonal neighbors are the same of 4‐neighbor, neighbor pixels are the corner of
pixels (p) and each of them is at Euclidean distance of p, given by
4 Applications from Engineering with MATLAB Concepts
8‐neighbors;
1.1.1.2. Adjacency
If two pixels are neighbors and their gray level values satisfy some specified criterion, then
they are connected. A set of intensity values (V) is used to define adjacency and connectivity.
There are three types of adjacency (Figure 4).
4‐adjacency
p and q pixels are 4‐adjacency if they are N4(p) with values from V.
8‐adjacency
p and q pixels are 8‐adjacency if they are N8(p) with values from V.
m‐adjacency (mixed)
• q is in ND(p) and,
1.1.1.3. Path
A path from pixel p with coordinate (x, y) to pixel q with coordinate (s, t) with values from V
is defined as 4‐ ,8‐ , or m‐paths depending on the type of adjacency specified.
According to V = {2,3,5}, If we want to find p and q pixels 4‐, 8‐ and m‐path, (Figure 5)
( x - s)
2
De ( p, q ) = + ( y - t )2
D4 ( p, q ) = x - s + y - t
D8 ( p, q ) = max( x - s , y - t )
According to V = {2,3,5}, if we want to find Dm distance from p pixel to q pixel (Figures 6, 7, 8);
Spatial resolution can be defined as the number of pixels per inch. Different spatial resolutions
of same image are shown in the Figure 10. Spatial resolution has different measuring methods
for different devices.
DPI is generally used in monitors. Sometimes it is called PPI (Pixels Per Inch). But the two
expressions have a difference. DPI is also used for measuring spatial resolution of printers. It
means DPI defines how many dots of ink on printed image per inch.
PPI is generally used in tablets, mobile phones, etc. If a and b are height and width resolutions
of image, we can calculate ppi value of any device using Equation 1.
a 2 + b2
PPI = (1)
Diagonal Size of Devices
10802 + 19202
PPI = 5.5 ≅ 401 (it is shown in apple web site)
Digital Image Processing with MATLAB 9
http://dx.doi.org/10.5772/63028
10802 + 19202
PPI = @ 401 (it is shownin
apple web site)
5.5
LPI is referred lines of dots per inch of printers. Printer has different LPI values as shown in
Tables 2.
Image file formats are important for printing, scanning, using on the Internet, etc. The different
formats are used in the world. The most common formats are jpg, tif, png, and gif (Fig‐
ures 11). In this section, the most common file formats (JPG, TIF, PNG, and GIF) are explained.
JPEG or JPG is the most common standard for compressing digital image. It is used in web
pages, document, email, etc. Because digital images have smaller size than other file formats.
However, JPEG images have very low resolution.
TIFF or TIF has the best resolution for using commercial works. Although it is very high quality,
the files have very big size.
GIF, which was used 8‐bit video for the people connecting to internet using dial‐up modem,
was designed by CompuServe.
PNG file format has smaller size than TIF and more resolution than GIF and JPG. Nowadays,
it is used in the web pages because of having transparency property.
MATLAB is a very simple software for coding. All data variable in MATLAB are thought a
matrix and matrix operations are used for analyzing them. MATLAB has the different
toolboxes according to application areas. In this section, MATLAB Image Processing Toolbox
is presented and the use of its basic functions for digital image is explained.
imread() function is used for reading image. If we run this function with requiring data, image
is converted to a two‐dimensional matrix (gray image is two‐dimensional, but, color image is
three‐dimensional) with rows and columns including gray value in the each cell.
I = imread(‘path/filename.fileextension');
imread() function only needs an image file. If the result of imread() function is equal to a
variable, a matrix variable (I) is created. File name, extension, and directory path that contains
image must be written between two single quotes. If script and image file are in the same folder,
path is not necessary.
The matrix variable of image is showed using imshow() function. If many images show with
sequence on the different figure windows, we use “figure” function for opening new window.
Digital Image Processing with MATLAB 11
http://dx.doi.org/10.5772/63028
imwrite() function is used to create an image. This function only requires a new image file
name with extension. If the new image is saved to a specific directory, the path of directory is
necessary.
Image reserve technique, each all elements of the matrix is replaced to be the top row elements
to bottom row and the bottom row elements to top row. In the other words, the image rotates
on the vertical axis.
MATLAB Image Processing Toolbox does not have function for it. Either the script is written
or flipdim function can be used (Figure 12).
12 Applications from Engineering with MATLAB Concepts
Mirroring technique is the rotating of reversed image on the horizontal axis. In MATLAB
Image Processing Toolbox has imrotate() function for rotating image. This function needs three
properties which are image matrix variable, rotating angle, and interpolation method
(Figure 13).
Interpolation method
Example
Sometimes, an image can be wanted to shift up to certain pixel value on the horizontal and
vertical axis. imtranslate() function is used to shift of an image. In the Figure 14 the image shifts
15 px right and 25 px bottom.
If an image is displayed big or small size for showing details or general view, its resolution
must be changed. These situations are called zoom‐in and zoom‐out. Digital cameras or
photosensitive devices use optic lenses for zoom‐in and zoom‐out. But, interpolation methods
are only used for digital images. Most common problem of interpolation methods is the
changing quality of image (Figures 15, 16).
I is image variable, if Resize Rate is bigger than 1, it means zoom‐in, otherwise zoom‐out.
Digital Image Processing with MATLAB 15
http://dx.doi.org/10.5772/63028
3. Image enhancement
In some cases, an image has useless or insufficient information for extracting objects because
of different defects. So that, the image must be processed using different digital image
processing techniques for removing the defects or artifacts. In this section, some principal
methods are explained for increasing the visibility and decreasing defects.
3.1. Brightness
Brightness of an image is adjusted with adding or subtracting a certain value to gray level of
each pixel.
New image (I_adjust) intensity values are between low_out and high_out gray values
(Figure 17).
16 Applications from Engineering with MATLAB Concepts
The MATLAB script above is used for gray image, but we want to change brightness of color
image, so, we must change all intensity values of R (red), G (green) and B (blue) channel of the
image (Figure 18).
3.2. Contrast
Contrast of an image can be changed by multiplying all pixel gray value by a certain value.
Digital Image Processing with MATLAB 17
http://dx.doi.org/10.5772/63028
MATLAB Image Processing Toolbox has the Contrast Adjust tool to change contrast of an
image. As shown in Figure 19, GUI allows the user for changing contrast using handling.
3.3. Negative
Intensity values of image are reversed as linear for negative image (Figure 20).
18 Applications from Engineering with MATLAB Concepts
3.4. Thresholding
Thresholding is generally used for converting image to binary image (0 and 1 gray value). The
algorithm of thresholding is defined in Equation 2.
ìï I ( x, y ) ³ T 1
G ( x, y ) = í (2)
ïî I ( x, y ) < T 0
I is original image, x and y are row and column number, T is threshold value, G is new image
fter applying threshold (Figure 21).
76 38
77 0
78 389
79 1245
80 0
81 1518
3.5. Histogram
Histogram counts the number of gray value of pixels in the images. Each pixel in the image
has gray value between 0 and 255. As shown in Table 3, counting pixels give us information
about image or objects in the image. The histogram of image is shown in Figure 22.
20 Applications from Engineering with MATLAB Concepts
1 1 1 1 1
3 3 0 0 2
3 3 2 2 2
6 4 4 2 6
6 7 7 5 5
We explain histogram equalization with an exam. You think about an image, and, its intensity
mapping is shown below. There are eight possible gray levels from 0 to 7. If we apply histogram
equalization to the image pixel gray values that are shown in Table 4, how new image
histogram will be?
Step 1: Find histogram of the image (Table 5)
I 0 1 2 3 4 5 6 7
f(I) 2 5 5 4 2 2 3 2
Table 5. Histogram.
I 01 2 3 4 5 6 7
f(I) 25 5 4 2 2 3 2
CFD 2 2 + 5 = 7 7 + 5 = 12 12 + 4 = 16 16 + 2 = 18 18 + 2 = 20 20 + 3 = 23 23 + 2 = 25
æ CFD ( v ) - CFDmin ö
h ( v ) = floor ç x ( L - 1) ÷ (3)
ç ( MxN ) - CFD ÷
è min ø
h is new gray value, v is pixel number, MxN is image row and column value, L is gray level
(in our image L is 8)
If we calculate the 4 number pixel;
h(4) = floor ((16 - 2) / ((5 x5) - 2) x(8 - 1)) @ floor (4, 26) @ 4
After all pixel gray values are calculated using Equation 3 the results of new gray values will
be like in Table 7.
I 0 1 2 3 4 5 6 7
f(I) 2 5 5 4 2 2 3 2
CFD 2 7 12 16 18 20 23 25
h 0 1 3 4 4 5 6 7
After histogram equalization is applied to the image, new gray values are shown in the
Figure 23.
22 Applications from Engineering with MATLAB Concepts
Matlab Image Processing Toolbox has the different filter types as shown in Table 8.
4. Color
Humans have very good photosensitive devices that are called eyes. Newton discovered that
the light has different color spectrum passing through the glass prism. We think human eye
is a glass prism that is called the lens. The lens focuses light to the retina of eyes. So that, humans
see visible color spectrum of light reflected from the objects. Color spectrum is shown in the
Figure 25. Human senses wavelength of light between 400 and 700 nm.
Eyes see colors as combining of primary that are Red (R), Green (G), and Blue (B). So that, all
visible colors are produced from primary colors. Secondary colors, which are produced with
adding of primary colors, are Yellow (Red + Green), Magenta (Red + Blue), and Cyan (Green
+ Blue) as shown in Figure 26.
In the MATLAB Image Processing Toolbox, a color image has three‐dimensional uint8 (8‐bit
unsigned integer) data. Each dimension corresponds to a color channel that is Red, Green, or
Blue channel. If we want, we can process each color channel. As shown in Figure 27, each color
channel splits from image.
4.1. HSI
300°. The Saturation, which shows how the color to be pure, takes value between [0, 1]. The
intensity is the dimensions of lightness or darkness. The range of intensity is between 0 (black)
and 1 (white).
MATLAB use rgb2hsv(image) or write script using Eqs. (4)–(6) for converting the color image
to HSI components. If we want to convert from HSI image to RGB image, we use hsv2rgb(hsi
image).
ì 1 ü
ì q if B £ G ï éë( R - G ) + ( R - B ) ûù ï
H =í with q -1
= cos í 2 (4)
1 ý
î360 - q if B > G ï é( R - G )2 + ( R - B )( G - B ) ù 2 ï
îë û þ
26 Applications from Engineering with MATLAB Concepts
3
S =1- é min ( R, G , B ) ûù (5)
( R + G + B) ë
1
I= [ R + G + B] (6)
3
4.2. YIQ
YIQ, which is defined by the National Television System Committee (NTSC), produces the
luminance and the chrominance. We use Equation 7 for producing of YIQ components from
RGB image (Figure 31), and Equation 8 is used for converting from YIQ to RGB.
é R ù é1 0.986 0.621 ù éY ù
êG ú = ê1 -.0272 -0.649 ú ê I ú
ê ú ê úê ú (8)
êë B úû êë1 -1.106 1.703 úû êëQ úû
Digital Image Processing with MATLAB 27
http://dx.doi.org/10.5772/63028
Gray image is produced using Equation 9 by NTSC standards. However, we can calculate
different methods, but MATLAB uses NTSC standards and it has rgb2gray(RGB image)
function (Figure 32).
28 Applications from Engineering with MATLAB Concepts
Other methods;
• The average; GI = 0.33R + 0.33G + 0.33B
• The lightness; GI = (max(R,G,B) + (min(R,G,B))/2
• The luminosity; GI = 0.21R + 0.72G + 0.07B
Digital Image Processing with MATLAB 29
http://dx.doi.org/10.5772/63028
5. Morphologic operations
MATLAB Image Processing Toolbox only use binary image for morphologic operations such
as opening, closing, etc.
5.2. Dilation
Dilation is a morphologic processing for growing an object in the binary image. It is shown
with ⊕ image (Figure 35).
C = AÅ B
C is the new image, A is the original image, and B is the structuring element.
30 Applications from Engineering with MATLAB Concepts
5.3. Erosion
Erosion is the other morphologic operator of a binary image for using eroding the pixels of
objects in the image. It is shown as ⊖ symbol.
C = A! B
Digital Image Processing with MATLAB 31
http://dx.doi.org/10.5772/63028
As shown in (Figure 36), opening and closing are the combination of erosion and dilation
operators as shown in Equations 10 and 11.
C = ( A ! B) Å B (10)
C = ( A Å B) ! B (11)
32 Applications from Engineering with MATLAB Concepts
5.5. Convolution
Convolution is generally used for modifying the spatial characteristic of an image (Figure 38).
In the convolution, a new pixel gray value is found by the weighted average pixels that are
neighbor of it. Neighbor pixels gray value is weighted by a matrix coefficient that is called
convolution kernel. According to the applications, kernel matrix has different sizes such as 3
× 3, 5 × 5, 7 × 7 (Figure 37).
j =- n i =- m
G ( x, y ) = å å k ( i, j ) F ( x - i, y - j ) = k * F
n m
(12)
F: processing image
Matlab Image Processing Toolbox has the different filter types as shown in Table 8.
Value Description
average Averaging filter
disk Circular averaging filter (pillbox)
gaussian Gaussian low‐pass filter
laplacian Approximates the two‐dimensional Laplacian operator
log Laplacian of Gaussian filter
motion Approximates the linear motion of a camera
prewitt Prewitt horizontal edge‐emphasizing filter
sobel Sobel horizontal edge‐emphasizing filter
Edge detection is used for finding the border of objects in the image (Figure 39). Common edge
detection algorithms are Sobel, Canny, Prewitt, Roberts, etc.
Digital Image Processing with MATLAB 35
http://dx.doi.org/10.5772/63028
5.7. Labeling
Pixels are assigned different labels because of belonging to different regions (or components
or objects). In Figure 40, the objects in the image have the different label values and show
different colors in the MATLAB.
6. Sample application
The last section in this chapter is a sample application that is about extraction of some
morphological features of multiple apricots in a digital image. Firstly, original and background
digital images are read (Figures 41, 42). After that, cropped original image is subtracted from
background image. Cropping process is used for extracting specific area from original image
(Figure 42). Subtracted image (Figure 44) converts to gray image as shown in Figure 45.
Thresholding process is applied to gray image for converting binary image (Figure 46).
Sometimes, some artifacts can occurred in binary images. Before labelling, connecting pixel
groups which are smaller than specific value (smaller than 50 px in this application) are
removed (Figure 47). After labelling (Figure 48), we can find all objects morphological features
as shown in Figure 49.
Author details
Mahmut Sіnecen
References
[1] Rafael C. Gonzalez, Richard E. Woods. Digital image processing. 3rd ed. United States
of America: Pearson; 2008. 943 p.
42 Applications from Engineering with MATLAB Concepts
[2] John C. Russ. The image processing handbook. 5th ed. United States of America: CRC
Press; 2007. 795 p.
[3] Henri Maitre. Image processing. 1st ed. Great Britain: Wiley‐ISTE; 2008. 359 p.
[4] Maria Petrou; Costas Petrou. Image processing the fundamentals. 2nd ed. Singapore:
Wiley; 2010. 781 p.
[5] John D. Cook. Three algorithms for converting color to grayscale [Internet]. 24.08.2009.
http://www.johndcook.com/blog/2009/08/24/algorithms‐convert‐color‐grayscale/.
Accessed 02.01.2016
[7] Sung Kim. Applications of Convolution in Image Processing with MATLAB [Internet].
20.08.2013. http://www.math.washington.edu/∼wcasper/math326/projects/
sung_kim.pdf. Accessed 12.12.2016