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.
Size: 2.07 MB
Language: en
Added: Feb 17, 2020
Slides: 63 pages
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
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
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
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
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
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