APPLICATIONS OF
MATLAB IN ENGINEERING
Yan-Fu Kuo Fall 2015
Dept. of Bio-industrial Mechatronics Engineering
National Taiwan University
Today:
• Introduction to digital image
• Read and show images
• Image arithmetic
Applications of MATLAB in Engineering Y.-F. Kuo 2
Digital Image and Its Acquisition
• An image is an artifact
that depicts or records
visual perception
• Typically acquired by
using charge-coupled
device (CCD) or
complementary metal-
oxide-semiconductor
(CMOS) devices
Applications of MATLAB in Engineering Y.-F. Kuo 3
Types of Digital Image
• Binary: Each pixel is just black or white
• Grayscale: Each pixel is a shade of gray, normally from 0
(black) to 255 (white)
• True color or RGB: Each pixel has a particular color
described by the amount of red, green and blue in it
Applications of MATLAB in Engineering Y.-F. Kuo 4
Typical RGB Image
Applications of MATLAB in Engineering Y.-F. Kuo 5
Why RGB?
• Three kinds of light-sensitive photoreceptor cells
in the human eye (i.e., cone cells) respond most
to red, green and blue
Applications of MATLAB in Engineering Y.-F. Kuo 6
Elements of Images
Pixels
Applications of MATLAB in Engineering Y.-F. Kuo 7
Binary Image
Applications of MATLAB in Engineering Y.-F. Kuo 8
Greyscale Image
Applications of MATLAB in Engineering Y.-F. Kuo 9
Color Image
Applications of MATLAB in Engineering Y.-F. Kuo 10
Read and Show An Image
• Read an image: imread()
• Show an image: imshow()
• Example:
clear, close all
I = imread('[Link]'); %read
imshow(I); %show
Applications of MATLAB in Engineering Y.-F. Kuo 11
Image Variable in Workspace
whos
Name Size Bytes Class
I 291x240 69840 uint8
• Image matrix:
Applications of MATLAB in Engineering Y.-F. Kuo 12
Image Info: imageinfo('[Link]')
Filename C:\Program Files\MATLAB\R2014a\toolbox\images\imdata\[Link]
FileModDate 25-九月-2013 [Link]
FileSize 69004
Format tif
Width 240
Height 291
BitDepth 8
ColorType grayscale
FormatSignature [73 73 42 0]
ByteOrder little-endian
BitsPerSample 8
SamplesPerPixel 1
RowsPerStrip 34
StripByteCounts [1x9 double]
XResolution 72
YResolution 72
ResolutionUnit Inch
MaxSampleValue 255
MinSampleValue 0
Applications of MATLAB in Engineering Y.-F. Kuo 13
Image Viewer: imtool('[Link]')
• Get pixel information in image viewer
Applications of MATLAB in Engineering Y.-F. Kuo 14
Image Processing
• Any form of signal processing for which the input
is an image
Applications of MATLAB in Engineering Y.-F. Kuo 15
Image Arithmetic
imabsdiff Absolute difference of two images
imadd Add two images or add constant to image
imapplymatrix Linear combination of color channels
imcomplement Complement image
imdivide Divide one image into another or divide image
by constant
imlincomb Linear combination of images
immultiply Multiply two images or multiply image by
constant
imsubtract Subtract one image from another or subtract
constant from image
Applications of MATLAB in Engineering Y.-F. Kuo 16
Image Multiplication: immultiply()
I=imread('[Link]');
subplot(1,2,1); imshow(I);
J=immultiply(I, 1.5);
subplot(1,2,2); imshow(J);
• How to reduce the brightness of the image?
Applications of MATLAB in Engineering Y.-F. Kuo 17
Image Addition: imadd()
I=imread('[Link]');
J=imread('[Link]'); K=imadd(I,J);
subplot(1,3,1); imshow(I);
subplot(1,3,2); imshow(K);
subplot(1,3,3); imshow(J);
Applications of MATLAB in Engineering Y.-F. Kuo 18
Practice
• Adjust the “brightness” and “contrast” of
[Link] and display it on the screen
Applications of MATLAB in Engineering Y.-F. Kuo 19
Image Histogram: imhist()
imhist(I)
1600
1400
1200
1000
800
600
400
200
0 50 100 150 200 250
Applications of MATLAB in Engineering Y.-F. Kuo 20
Practice
• Plot the histograms of the images before and
after the “brightness” and “contrast” adjustment
for [Link]
Applications of MATLAB in Engineering Y.-F. Kuo 21
Histogram Equalization: histeq()
• Enhances the contrast of the image
I = imread('[Link]'); I2 = histeq(I);
subplot(1,4,1); imhist(I);
subplot(1,4,2); imshow(I);
subplot(1,4,3); imshow(I2);
subplot(1,4,4); imhist(I2);
1500 1500
1000 1000
500 500
0 0
0 100 200 0 100 200
Applications of MATLAB in Engineering Y.-F. Kuo 22
Practice
• Write your own equalization function, try it on
[Link], and display it on the screen
Applications of MATLAB in Engineering Y.-F. Kuo 23
Geometric Transformation
• Moving the coordinates (Not the gray-levels) of
the pixels in an image
Applications of MATLAB in Engineering Y.-F. Kuo 24
Geometric Transformation Matrices (2D)
Transform Example Transformation matrix
𝑥′ 1 0 𝑡𝑥 𝑥
Translation 𝑦′ = 0 1 𝑡𝑦 𝑦
1 0 0 1 1
𝑠𝑥 0 0
Scale
0 𝑠𝑦 0
imresize() 0 0 1
1 ℎ𝑥 0
Shear ℎ𝑦 1 0
0 0 1
Rotation with 𝜃 cos 𝜃 sin 𝜃 0
(clock-wise) − sin 𝜃 cos 𝜃 0
imrotate() 0 0 1
[Link]
Applications of MATLAB in Engineering Y.-F. Kuo 25
Image Rotation: imrotate()
I = imread('[Link]'); subplot(1,2,1);
imshow(I); J = imrotate(I, 35, 'bilinear');
subplot(1,2,2); imshow(J);
size(I)
size(J)
Applications of MATLAB in Engineering Y.-F. Kuo 26
Image Rotation
• In two dimensions, rotation of a point (𝑥, 𝑦) for an
angle 𝜃 “counter-clockwise” can be written as:
𝑥′ cos 𝜃 − sin 𝜃 𝑥
′ =
𝑦 sin 𝜃 cos 𝜃 𝑦
Applications of MATLAB in Engineering Y.-F. Kuo 27
Write Image: imwrite()
• Format supported: 'bmp', 'gif', 'hdf', 'jpg', 'jpeg',
'jp2', 'jpx', 'pcx', 'pnm', 'ppm', 'ras', 'tif', 'tiff', 'xwd'
• Example:
imwrite(I, '[Link]');
Applications of MATLAB in Engineering Y.-F. Kuo 28
End of Class