Open access peer-reviewed chapter

Digital Image Processing with MATLAB

By Mahmut Sinecen

Submitted: September 25th 2015Reviewed: March 11th 2016Published: July 7th 2016

DOI: 10.5772/63028

Downloaded: 5278


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.


  • digital
  • image
  • processing
  • Fundamental

1. Digital image processing

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

Figure 1.


Figure 2.

Digital images.

Figure 3.

Pixels of a color image.

1.1.1. Pixels relationships Neighbors of a pixel

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;

f(x - 1, y - 1)f(x - 1, y)f(x - 1, y + 1)
f(x, y - 1)pf(x, y + 1)
f(x + 1, y - 1)f(x + 1, y)f(x + 1, y + 1)

Table 1.

Neighbor of a pixel.

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

N4(p) = {f(x, y  1), f(x  1, y), f(x, y + 1), f(x + 1, y)}E101

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

ND(p) = {f(x  1, y  1), f(x  1, y + 1), f(x + 1, y + 1), f(x + 1, y  1)}E1102


8‐neighbors is a combination of N4(p) and ND(p) and shown as N8(p).

N8(p) = {f(x  1, y  1), f(x  1, y + 1), f(x + 1, y + 1), f(x + 1, y  1), f(x, y  1), f(x  1,y), f(x, y + 1), f(x + 1, y)}E103 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).

Figure 4.

Pixel adjacency.


p and q pixels are 4‐adjacency if they are N4(p) with values from V.


p and q pixels are 8‐adjacency if they are N8(p) with values from V.

m‐adjacency (mixed)

p and q pixels are m‐adjacency if;

  • q is in N4(p) or,

  • q is in ND(p) and,

  • N4(p) ⋂ N4(q) = ⦰ with values from V. 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)

Figure 5.

Finding 4‐, 8‐, m‐path between p and q pixels. Distance measures of pixels

  • Euclidean Distance (De)


  • City‐block Distance (D4)


  • Chessboard Distance (D8)


  • Dm Distance; it is defined as the shortest m‐path.

According to V = {2,3,5}, if we want to find Dm distance from p pixel to q pixel (Figures 6, 7, 8);

Figure 6.

Distance between p and q pixels.

Figure 7.

Example about the shortest m‐path.

Figure 8.

Solving example in the Figure 7.

Dm is 5 because orange path is shorter than blue path.

Figure 9.

Distance measuring types.

1.2. Spatial resolution

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.

Figure 10

.Different spatial resolutions of same image.

1.2.1. Dots per inch (DPI)

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.

1.2.2. Pixels per inch (PPI)

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.

PPI=a2+b2Diagonal Size of DevicesE1

For example; 1080 × 1920 pixels, 5.5 inch Iphone 6s Plus PPI value;

PPI=10802+192025.5401 (it is shown in apple web site)E10000

1.2.3. Lines per inch (LPI)

LPI is referred lines of dots per inch of printers. Printer has different LPI values as shown in Tables 2.

PrinterLPI value
Screen printing45–65 LPI
Laser printing (300 dpi)65 LPI
Laser printing (600 dpi)85–105 LPI

Table 2.

LPI Value of Printer.

1.3. Image file formats

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 (Figures 11). In this section, the most common file formats (JPG, TIF, PNG, and GIF) are explained.

Figure 11.

Image formats.

1.3.1. JPG (Joint Photographic Expert Group)

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.

1.3.2. TIF (Tagged Image File Format)

TIFF or TIF has the best resolution for using commercial works. Although it is very high quality, the files have very big size.

1.3.3. GIF (Graphics Interchange Format)

GIF, which was used 8‐bit video for the people connecting to internet using dial‐up modem, was designed by CompuServe.

1.3.4. PNG (Portable Network Graphics)

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.

2. Basic image processing with MATLAB

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.

2.1. Read, write, and show image

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.

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.

2.2. Image reverse

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

Figure 12.

Vertical and horizontal reverse.

2.3. Image mirroring

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

Figure 13.

Image rotate.

I_rotate = imrotate(Image Matrix Variable, Angle, Interpolation Method)

Interpolation method

  • ‘nearest’: Nearest‐Neighbor Interpolation

  • ‘bilinear’: Bilinear Interpolation

  • ‘bicubic’: Bicubic Interpolation


2.4. Image shift

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.

Figure 14.

Image shift.

2.5. Image resize

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

Figure 15.

Zoom‐in and zoom‐out.

I_resize = imresize(I, Resize Rate, Interpolation Method)

I is image variable, if Resize Rate is bigger than 1, it means zoom‐in, otherwise zoom‐out.

Figure 16.

Image resize.

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.

G(i,j)= F(i,j)+b  b>0 Brightness incereaseb<0 Brightness decreaseE10000

I_adjust = imadjust(I, [low_in; hig_in], [low_out;high_out])

New image (I_adjust) intensity values are between low_out and high_out gray values (Figure 17).

Figure 17.

Changing brightness of the image.

Figure 18.

Adjusting Brightness of Color Image.

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.

G(i,j)=c*F(i,j) c>0 contrast increasec<0 contrast decreaseE10000

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.

Figure 19.

Adjust contrast tool.

3.3. Negative

Intensity values of image are reversed as linear for negative image (Figure 20).

Figure 20.

Negative Image.

Figure 21.


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.

G(x,y)={I(x,y)T 1I(x,y)<T 0E2

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

Figure 22.

Histogram of the image.

Gray valueCounting of pixel number

Table 3.

Histogram of Specific Gray Values.

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.

The histogram of image is calculated using imhist(image) function in the MATLAB.

3.6. Histogram equalization

Histogram equalization is defined a technique for adjusting contrast of an image using all gray values to equalize as much as possible. Some situations work fine and image are shown very well; sometimes, it is not good and new image is darker than original image (Figure 24).


Table 4.

An Image Pixel Gray Values.

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)


Table 5.


Step 2: Calculate Cumulative Frequency Distribution (CFD)

CFD22 + 5 = 77 + 5 = 1212 + 4 = 1616 + 2 = 1818 + 2 = 2020 + 3 = 2323 + 2 = 25

Table 6.

Calculate Cumulative Frequency Distrubiton (CFD).

Step 3: Calculate new pixel gray value using Equation 3


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((162)/((5x5)2) x(81))floor(4,26)4E10000

Figure 23.

Original and new image gray values.

After all pixel gray values are calculated using Equation 3 the results of new gray values will be like in Table 7.


Table 7.

New Gray Values.

After histogram equalization is applied to the image, new gray values are shown in the Figure 23.

Figure 24.

Histogram equalization.

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.

Figure 25.

Color spectrum.

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.

Figure 26.

Primary and secondary colors.

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.

Figure 27.

R, G, B channel values in the MATLAB workspace.

Figure 28.

R, G, B channel.

4.1. HSI

As shown in Figure 29 each color represents three components as H (Hue), S (Saturation), I (Intensity). The Hue, which can be defined rate of pure color, is an angle form between 0° and 360°. Red, Green, Blue are 0°, 120°, and 360°, and Yellow, Cyan, and Magenta are 60°, 180°, 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).

Figure 29.

HSI components.

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

H={θ360θ if BGif B>G with θ= cos1{12[(RG)+(RB)][(RG)2+(RB)(GB)]12}E4

Figure 30.

HIS of the image.

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.


Figure 31.

YIQ of the Image.

4.3. Gray image

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


Figure 32.

Gray image.

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

5. Morphologic operations

MATLAB Image Processing Toolbox only use binary image for morphologic operations such as opening, closing, etc.

5.1. Structuring element

Structuring element (SE) is a shape that has different sizes (3 × 3, 4 × 4, 5 × 5, etc.) and shapes (Figure 33). SE is applied to an image for drawing results on how the objects change in the image (Figure 34). SE is generally used for dilation, erosion, opening, closing operations.

Figure 33.

Different structuring elements.

5.2. Dilation

Dilation is a morphologic processing for growing an object in the binary image. It is shown with ⊕ image (Figure 35).


C is the new image, A is the original image, and B is the structuring element.

Figure 34.


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.


Figure 35.


5.4. Opening and closing

As shown in (Figure 36), opening and closing are the combination of erosion and dilation operators as shown in Equations 10 and 11.


Figure 36.

Opening and closing.

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

Mathematical definition of convolution is shown in Equation 12;


k: convolution kernel matrix

F: processing image

if w and h are row and column of image ⇒(m = (w - 1)/2)|(n = (h - 1)/2)

Figure 37.

Kernel matrices.

Matlab Image Processing Toolbox has the different filter types as shown in Table 8.

averageAveraging filter
diskCircular averaging filter (pillbox)
gaussianGaussian low‐pass filter
laplacianApproximates the two‐dimensional Laplacian operator
logLaplacian of Gaussian filter
motionApproximates the linear motion of a camera
prewittPrewitt horizontal edge‐emphasizing filter
sobelSobel horizontal edge‐emphasizing filter

Table 8.

Matlab Image Processing Toolbox Filter Types.

Figure 38.

Applying different filters.

5.6. Edge detection

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.

Figure 39.

Edge detection.

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.

Figure 40.


[Boundary,Labels] = bwboundaries(binary image, ‘noholes') function uses for labeling. Firstly, the image must be binary image, if it is not, you must convert to binary image. Secondly, all objects must be white (1) and background must be black (0) for using ‘noholes’ method. We use this function with two variables. One of them is address of boundary pixels, and other one is label numbers and their addresses.

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.

Figure 41.

Original digital image.

Figure 42.

Background image.

Figure 43.

Background image.

Figure 44.

Subtracting from background to cropped image.

Figure 45.

Converting gray image.

Figure 46.


Figure 47.

Remove artifacts.

Figure 48.


Figure 49.

Show morphological features.

© 2016 The Author(s). Licensee IntechOpen. This chapter is distributed under the terms of the Creative Commons Attribution 3.0 License, which permits unrestricted use, distribution, and reproduction in any medium, provided the original work is properly cited.

How to cite and reference

Link to this chapter Copy to clipboard

Cite this chapter Copy to clipboard

Mahmut Sinecen (July 7th 2016). Digital Image Processing with MATLAB, Applications from Engineering with MATLAB Concepts, Jan Valdman, IntechOpen, DOI: 10.5772/63028. Available from:

chapter statistics

5278total chapter downloads

3Crossref citations

More statistics for editors and authors

Login to your personal dashboard for more detailed statistics on your publications.

Access personal reporting

Related Content

This Book

Next chapter

Information Entropy

By Jan Urban

Related Book

First chapter

Distributionally Robust Optimization

By Jian Gao, Yida Xu, Julian Barreiro-Gomez, Massa Ndong, Michalis Smyrnakis and Hamidou Tembine

We are IntechOpen, the world's leading publisher of Open Access books. Built by scientists, for scientists. Our readership spans scientists, professors, researchers, librarians, and students, as well as business professionals. We share our knowledge and peer-reveiwed research papers with libraries, scientific and engineering societies, and also work with corporate R&D departments and government entities.

More About Us