Basics of image processing using MATLAB

2,050 views 63 slides Feb 17, 2020
Slide 1
Slide 1 of 63
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63

About This Presentation

The students can learn about basics of image processing using matlab.
It explains the image operations with the help of examples and Matlab codes.
Students can fine sample images and .m code from the link given in slides.


Slide Content

Basics of Image Processing using MATLAB
Date:
INSTRUCTOR
DR. MOHSIN SIDDIQUE
ASSIST. PROFESSOR
DEPARTMENT OF CIVIL & ENV ENG.


Part 1: Image processing using Command line/Editor

Part 2: Image processing toolbox
Image processing
2


Part 1: Image processing using Command line/Editor
B
Install Matlab with image processing toolbox
B
Download
Image_Analysis_tutorial.zip
. It includes images and Matlab code
file (
Image_tutorial.m
) used in this lecture as shown below:

link to download: https://www.dropbox.com/s/ncv3nlah8cl4dzj/Image_Ana lysis_tutorial.zip?dl=0
Image processing
3


Read image

Syntax: imread- it read image from the graphic file

I = imread('path/filename.fileextension');
________________________________________________________________
%Let’s read onion.png
I=imread(‘onion.png');
% where onion.png is image which is stored in I
See the workspace window below:Image Processing: Read, write and show image
Read more @: https://www.mathworks.com/help/matlab/ref/imread.html
4


Show image
B
Syntax: imshow: display image
B
image: display image from array
B
imshow(figuredata);
B
image(figuredata)
________________________________________________________
figure(2) %% to show image on figure 2
subplot (1,2,1); imshow(I); title ('using imshow fun ction')
subplot (1,2,2); image(I); title ('using image func tion')
Image Processing: Read, write and show image
Read more @: https://www.mathworks.com/help/matlab/ref/imshow.html https://www.mathworks.com/help/matlab/ref/image.html
5

Image Processing: Read, write and show image
6

Image Processing: Read, write and show image
origin
rows
column
7


Write image
B
Syntax: imwirte- write image to a graphic file
B
imwrite(figuredata, file name’);
_______________________________________________________________
%Let write onion_w.png
imwrite(I, ’onion_w.png’); % it will save “onion_w. png” in working directory
%imwrite(I, c:\.....\name.png) % it will save “name .png” at specific location
Image Processing: Read, write and show image Read more @: https://www.mathworks.com/help/matlab/ref/imwrite.html
8


Image reverse: Image reserve technique, each all el ements of the matrix is
replaced to be the top row elements to bottom row a nd the bottom row
elements to top row. In the other words, the image rotates on the vertical axis.
B
Syntax: flipdim(Array, dimension) – flips array along specified
dimension B
flip(Array, dimension)
Image Processing: Image reverse
Read more @: https://www.mathworks.com/help/matlab/ref/flipdim.html https://www.mathworks.com/help/matlab/ref/flip.html
9

____________________________________________________________ I=imread('onion.png'); % read image
I_mirror=flipdim(I, 2); % mirror image
I_reverse=flipdim(I,1); % reverse image
I_mirr_rev=flipdim(I_reverse, 2); % mirror+reverse i mage
figure (5)
subplot(2,2,1); imshow (I); title('Original image');
subplot(2,2,2); imshow (I_mirror); title('Mirror ima ge');
subplot(2,2,3); imshow (I_reverse); title('Reverse i mage');
subplot(2,2,4); imshow (I_mirr_rev); title('Reverse+ Mirror image');
Image Processing: Image reverse
10

Image Processing: Image reverse
11


Image rotate
B
Syntax: imrotate – rotate image
B
imrotate(Image Matrix Variable, Angle)
B
imrotate(Image Matrix Variable, Angle, Interpolatio n Method)

Interpolation method
B
‘nearest’: Nearest‐Neighbor Interpolation
B
‘bilinear’: Bilinear Interpolation
B
‘bicubic’: Bicubic Interpolation
Image Processing: Image rotate
Read more @: https://www.mathworks.com/help/images/ref/imrotate.html
12

_________________________________________________________
I=imread('onion.png'); % read image
I_rotate=imrotate(I,60,'bilinear'); % rotate image with bilinear
figure (6)
subplot (1,2,1); imshow(I); title ('Original image' )
subplot (1,2,2); imshow(I_rotate); title ('Rotated image')
Image Processing: Image rotate
13

Image Processing: Image rotate
14


Brightness of an image is adjusted with adding or s ubtracting a certain value
to gray level of each pixel.
B
Syntax: imadjust- adjust intensity values or colormap
B
For gray-scale images
adjusted_image = imadjust(I)
adjusted_image = imadjust(I,[low_in high_in])
B
For color images
B
J = imadjust(RGB,[low_in high_in],___)
Image Enhancement: Brightness
Read more @: https://www.mathworks.com/help/images/ref/imadjust.html
15

%Gray-scale image
I=imread(‘pout.png');
grayI=rgb2gray(I);
adj_I= imadjust(grayI); % for gray-scale images
adj_I2=imadjust(grayI,[0.3 0.7],[]); % using specif ied values
figure (7)
subplot (1,3,1); imshow(grayI); title ('Original im age')
subplot (1,3,2); imshow(adj_I); title ('Adjused ima ge using default')
subplot (1,3,3); imshow(adj_I2); title ('Adjused im age using specified values')
Image Enhancement: Brightness
16

Image Enhancement: Brightness
17

%Color images
I_RGB = imread(‘football.png');
adj_I_RGB = imadjust(I_RGB, [.2 .3 0; .6 .7 1], []);
figure (8)
subplot (1,2,1); imshow(I_RGB); title ('Original im age')
subplot (1,2,2); imshow(adj_I_RGB); title ('Adjused image')
Image enhancement: Brightness
18

Image Enhancement: Brightness
19


Contrast of an image can be changed by multiplying all pixel gray value by
a certain value. 
Syntax: imcontrast- Use the imcontrast function to create adjust contra st
tool ____________________________________________________________ figure (9);
I=imread('pout.png'); % read image
grayI=rgb2gray(I); % convert to gray-scale
imshow(grayI) % show image
Imcontrast % call image tool to adjust contrast
Image Enhancement: Contrast Read more @: https://www.mathworks.com/help/images/ref/imcontrast.html
20

Image Enhancement: Contrast
21


Intensity values of image are reversed as linear fo r negative image.
B
Syntax: imcomplement– complement image
B
Imcomplement(ImageVariable)
___________________________________________________________
I=imread('pout.png'); % read image
neg_I=imcomplement(I); % create negative of image
figure (10)
subplot (1,2,1); imshow(I); title ('Original image' )
subplot (1,2,2); imshow(neg_I); title ('Negative im age')
Image Enhancement: Negative
Read more @: https://www.mathworks.com/help/images/ref/imcomplement.html
22

Image Enhancement: Negative
23


An image histogram is a chart that shows the distri bution of intensities in an
indexed or grayscale image.
B
syntax: imhist– gives histogram of image data
B
imhist(ImageVariable)
______________________________________________________________
I=imread('pout.png'); % read image
I_hist=imhist(I); % save histogram data
figure (11)
subplot (1,2,1); imshow(I); title ('Original image' )
subplot (1,2,2); imhist(I); title ('Image histogram ')
Image Enhancement: Histogram Read more @: https://www.mathworks.com/help/images/ref/imhist.html
24

Image Enhancement: Histogram
25


It automatically adjust the intensity values

Syntax: histeq– enhance contrast using histogram equalization

histeq(ImageVariable)
____________________________________________________________
I=imread('pout.png'); % read image
I_hist=imhist(I); % save histogram of image
J=histeq(I); % create a new histogram equalized ima ge
I_histeq=imhist(J); % save histogram of new histogr am equalized image
figure (12)
subplot (2,2,1); imshow(I); title ('Original image' )
subplot (2,2,2); imhist(I); title ('original Image histogram')
subplot (2,2,3); imshow(J); title ('Equalized image ')
subplot (2,2,4); imhist(J); title ('Histogram of eq ualized image')
Image Enhancement: Histogram equalization
Read more @: https://www.mathworks.com/help/images/ref/histeq.html
26

Image Enhancement: Histogram equalization
27


In the MATLAB Image Processing Toolbox, a color ima ge has three‐dimensional
uint8 (8‐bit unsigned integer) data. Each dimension correspo nds to a color
channel that is Red, Green, or Blue channel.
Image Enhancement: Color & Color conversion
28

________________________________________________________________
%Extract RGB channel data and display
I=imread('onion.png'); % read image
I_R=I(:,:,1); % store Red channel/band values in I_ R
I_G=I(:,:,2); % store Green channel/band values in I_G
I_B=I(:,:,3); % store Blue channel/band values in I _B
figure(3) %opening a figure 3
subplot(2,2,1); imshow(I); title ('original image')
subplot(2,2,2); image(I_R); title('Red')
subplot(2,2,3); image(I_G); title('Green')
subplot(2,2,4); image(I_B); title ('blue')
Image Enhancement: Extract RGB and display
29

Image Enhancement: Extract RGB and display
30


Covert image from RGB to HSV
B
Syntax -rgb2hsv– converts RGB colors to HSV
B
rgb2hsv(RBG_Image)
Image
Enhancement: Convert to HSV, extract HSV
and display
Read more @: https://www.mathworks.com/help/matlab/ref/rgb2hsv.html
31

__________________________________________________________ I=imread('onion.png');
HSV_I=rgb2hsv(I); %convert from RGB to HSI
hue=HSV_I(:,:,1); % stores Red channel/band values in HUE
sat=HSV_I(:,:,2); % stores Green channel/band value s in Saturation
val=HSV_I(:,:,3); % stores Blue channel/band values in Vlaue (intensity)
figure(31) %opening a figure 3 with three sub plots
subplot(2,2,1); imshow(I); title ('original image')
subplot(2,2,2); imshow(hue); title('HUE')
subplot(2,2,3); imshow(sat); title('SATURATION')
subplot(2,2,4); imshow(val); title ('VALUE')
Image Enhancement: Convert to HSV, extract HSV and display
32

Image Enhancement: Convert to HSV, extract HSV and display
33


Gray image is produced using followingby NTSC standa rds. However, we
can calculate different methods, but MATLAB uses NT SC standards and it has
rgb2gray(RGB image) function:

syntax –rgb2gray- to convert RGB to gray image

syntax –imbinarize– to convert gray-scale to binary image
Image
Enhancement: Create indexed & binary
image
and display
Read more @: https://www.mathworks.com/help/matlab/ref/rgb2gray.html https://www.mathworks.com/help/images/ref/imbinarize.html
34

_______________________________________________________ I=imread('onion.png');
Index_I1=0.299*I(:,:,1)+0.587*I(:,:,2)+0.114*I(:,:, 3);
Index_I2=rgb2ind(I,32); %% 32 color indexed image
gray_I=rgb2gray(I); %% 256 colors grey image
binary_I = imbinarize(gray_I); % convert gray image to binary image
figure(4)
subplot(2,2,1);imshow(Index_I1); title(' Indexed im age using equation')
subplot(2,2,2);imshow(Index_I2); title(' Indexed im age using rgb2ind')
subplot(2,2,3);imshow(gray_I); title(' gray-scale u sing rgb2gray')
subplot(2,2,4);imshow(binary_I); title(' Binary ima ge using imbinarize')
Image Enhancement: Create indexed & binary image and display
35

Image Enhancement: Create indexed & binary image and display
36


Convolution is generally used for modifying the spa tial characteristic of an
image. 
Matlab Image Processing Toolbox has the different f ilter types
Image Enhancement: Filters-Convolution
37


Syntax: fspecial: Create predefined 2-D filters
B
h = fspecial(type)
B
h = fspecial('average',hsize)
B
h = fspecial('disk',radius)
B
h = fspecial('gaussian',hsize,sigma)
B
h = fspecial('laplacian',alpha)
B
h = fspecial('log',hsize,sigma)
B
h = fspecial('motion',len,theta)
B
h = fspecial('prewitt')
B
h = fspecial('sobel')

Syntax: Imfilter: N-D filtering of multidimensional image
B
B = imfilter(I,h)
B
B = imfilter(I,h,options,...)
Image Enhancement: Filters-Convolution Read more @: https://www.mathworks.com/help/images/ref/fspecial.html https://www.mathworks.com/help/images/ref/imfilter.html
38

______________________________________________________________ I=imread('onion.png'); % read image
H=fspecial('disk', 10);
blurred=imfilter(I,H, 'replicate'); % blurred filte r
H=fspecial('motion', 20, 45);
MotionBlur=imfilter(I,H, 'replicate'); % MotionBlur filter
H=fspecial('sobel');
sobel=imfilter(I,H, 'replicate'); % sobel filter
figure(13)
subplot(2,2,1);imshow(I); title(' Original Image');
subplot(2,2,2);imshow(blurred); title(' Blur kernel ');
subplot(2,2,3);imshow(MotionBlur); title('Motion Bl ur kernel');
subplot(2,2,4);imshow(sobel); title('sobel kernel');
Image Enhancement: Filters-Convolution
39

Image Enhancement: Filters-Convolution
40


Edge detection is used for finding the border of ob jects in the image. Common
edge detection algorithms are Sobel, Canny, Prewitt , Roberts, etc. 
Syntax: edge - finds the edges in intensity images
B
BW = edge(I)
B
BW = edge(I, method)
Image Enhancement: Edge detection
Read more @: https://www.mathworks.com/help/images/ref/edge.html
41

__________________________________________________________ I=imread('pout.png'); % read image
gray_I=rgb2gray(I);
Iprewitt=edge(gray_I, 'prewitt');
Icanny=edge(gray_I, 'canny');
Isobel=edge(gray_I,'sobel');
figure(14)
subplot(2,2,1);imshow(I); title(' Original Image');
subplot(2,2,2);imshow(Iprewitt); title('Prewitt');
subplot(2,2,3);imshow(Icanny); title('Canny');
subplot(2,2,4);imshow(Isobel); title('Sobel');
Image Enhancement: Edge detection
42

Image Enhancement: Edge detection
43

Comments….
Questions….
Suggestions….
References:
https://www.mathworks.com/help https://www.intechopen.com/books/applications-from -engineering- with-matlab-concepts/digital-image-processing-with- matlab
44
Thank you !


Morphological image processingis a collection of non-linear operations
related to the shape or morphology of features in a n image. 
Morphological operations can also be applied to bin ary and greyscale
images. 
Morphological techniques probe an image with a smal l shape or template
called astructuring element. The structuring element is positioned at all
possible locations in the image and it is compared with the corresponding
neighbourhood of pixels. Some operations test wheth er the element "fits"
within the neighbourhood, while others test whether it "hits" or intersects the
neighbourhood:
Image Enhancement: Morphologic operations Morphological operation on a
binary image creates a new binary
image in which the pixel has a non-
zero value only if the test is
successful at that location in the
input image.
45


Structuring element: The structuring element is a small binary image, i.e. a
small matrix of pixels, each with a value of zero o r one:
B
The matrix dimensions specify the sizeof the structuring element.
B
The pattern of ones and zeros specifies the shapeof the structuring element.
B
Anoriginof the structuring element is usually one of its pixels, alth ough generally the
origin can be outside the structuring element.
Image Enhancement: Morphologic operations
46


Syntax: strel - represents a flat morphological structuring element ,
B
SE = strel('diamond',r)
B
SE = strel('disk',r,n)
B
SE = strel('octagon',r)
B
SE = strel('line',len,deg)
etc
Image Enhancement: Morphologic operations
Read more @: https://www.mathworks.com/help/images/ref/strel.html
47


Dilation: It is a morphologic processing for growing an objec t in the binary
image. 
The dilation of an image f by a structuring element s (denoted f s) produces
a new binary image g = f s with ones in all locat ions (x,y) of a structuring
element's origin at which that structuring element s hits the the input image f,
i.e. g(x,y) = 1 if s hits f and 0 otherwise, repeat ing for all pixel coordinates
(x,y).
Image Enhancement: Morphologic operations
48

Image Enhancement: Morphologic operations
49


syntax: imdilate – dilate image
B
J = imdilate(I,SE)
B
J = imdilate(I,nhood)
B
J = imdilate(___,packopt)
B
J = imdilate(___,shape)
Image Enhancement: Morphologic operations
Read more @: https://www.mathworks.com/help/images/ref/imdilate.html
50


I=imread('letterA.png'); % read image

gray_I=rgb2gray(I);

binary_I = imbinarize(gray_I);

se1=strel('square',3); % square structural element

dilation_sq=imdilate(binary_I,se1);

se2=strel('diamond', 3); %diamond structural element

dilation_di=imdilate(binary_I,se2);

se3=strel('line', 9,0); %line structural element

dilation_li=imdilate(binary_I,se3);

figure (15)

subplot(2,2,1);imshow(I); title(' Original Image');

subplot(2,2,2);imshow(dilation_sq); title('Dilation Ima ge (3x3) square SE');

subplot(2,2,3);imshow(dilation_di); title('Dilation Ima ge (3x3) diamond SE');

subplot(2,2,4);imshow(dilation_li); title('Dilation Ima ge (3x3) line SE');
Image Enhancement: Morphologic operations
51

Image Enhancement: Morphologic operations
52


Erosion: It is the other morphologic operator of a binary im age for using
eroding the pixels of objects in the image 
The erosion of a binary image f by a structuring el ement s (denoted f ϴs)
produces a new binary image g = f ϴ s with ones in all locations (x,y) of a
structuring element's origin at which that structur ing element s fits the input
image f, i.e. g(x,y) = 1 is s fits f and 0 otherwis e, repeating for all pixel
coordinates (x,y)
Image Enhancement: Morphologic operations
53

Image Enhancement: Morphologic operations
54


syntax: imdilate – erode image
B
J = imerode(I,SE)
B
J = imerode(I,nhood)
B
J = imerode(___,packopt,m)
B
J = imerode(___,shape)
Image Enhancement: Morphologic operations
Read more @: https://www.mathworks.com/help/images/ref/imerode.html
55


I=imread('letterA.png'); % read image

gray_I=rgb2gray(I);

binary_I = imbinarize(gray_I);

se1=strel('square',3); % square structural element

dilation_sq=imerode(binary_I,se1);

se2=strel('diamond', 3); %diamond structural element

dilation_di=imerode(binary_I,se2);

se3=strel('line', 9,0); %line structural element

dilation_li=imerode(binary_I,se3);

figure (15)

subplot(2,2,1);imshow(I); title(' Original Image');

subplot(2,2,2);imshow(dilation_sq); title('Erode Image (3 x3) square SE');

subplot(2,2,3);imshow(dilation_di); title('Erode Image (3 x3) diamond SE');

subplot(2,2,4);imshow(dilation_li); title('Erode Image (3 x3) line SE');
Image Enhancement: Morphologic operations
56

Image Enhancement: Morphologic operations
57


Opening: The opening of an image fby a structuring element s(denoted by
f o s) is an erosion followed by a dilation: 
syntax: imopen -Morphologically close image
B
J = imopen(I,SE)
B
J = imopen(I,nhood)
Image Enhancement: Morphologic operations
readmore@: https://www.mathworks.com/help/images/ref/imopen.html
58


Closing: The closing of an imagefby a structuring element s(denoted byf
• s) is a dilation followed by an erosion: 
Syntax: imclose – Morphologically close image

J = imclose(I,SE)

J = imclose(I,nhood)
Image Enhancement: Morphologic operations
f •s= (f s) s
59


I=imread('letterA.png'); % read image

gray_I=rgb2gray(I);

binary_I = imbinarize(gray_I);


se=strel('diamond',5); % structural element

open_I=imopen(binary_I,se);

close_I=imclose(binary_I,se);

close_open_I=imopen(imclose(binary_I,se),se);


figure (17)

subplot(2,2,1);imshow(I); title(' Original Image');

subplot(2,2,2);imshow(open_I); title('Open');

subplot(2,2,3);imshow(close_I); title('close');

subplot(2,2,4);imshow(close_open_I); title('close+o pen');
60

Image Enhancement: Morphologic operations

Comments….
Questions….
Suggestions….
References:
https://www.mathworks.com/help https://www.intechopen.com/books/applications-from -engineering- with-matlab-concepts/digital-image-processing-with- matlab https://www.cs.auckland.ac.nz/courses/compsci773s1c /lectures/Ima geProcessing-html/topic4.htm http://www0.cs.ucl.ac.uk/staff/G.Brostow/classes/IP G2010/L3_Morp hology.pdf
62
Thank you !


Part 2: Image processing toolbox

Basic functions will be discussed in the class
Image processing
63