Lec 5
Lec 5
• Documentation
• Misc. Useful Functions
• Graphical User Interfaces
• Simulink
• Symbolic Toolbox
• Image Processing
• Hardware Interface
2
Official Documentation
• http://www.mathworks.com/help/matlab/
Courtesy of The MathWorks, Inc. Used with permission. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their
respective holders.
Miscellaneous Matlab (1)
4
Miscellaneous Matlab (2)
MATLAB version 6. . ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n . See
.mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o t eir res e tive olders.
Draw the GUI
• Select objects from the left, and draw them where you
want them
MATLAB version 6. . ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n . See
.mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o t eir res e tive olders.
Change Object Settings
• Double-click on objects to open the Inspector. Here you can
change all the object's properties.
MATLAB version 6. . ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n . See
.mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o t eir res e tive olders.
Save the GUI
• When you have modified all the properties, you can save
the GUI
• Matlab saves the GUI as a .fig file, and generates an m-file!
10
MATLAB version 6. . ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n . See
.mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o t eir res e tive olders.
Add Functionality to M-File
• To add functionality to your buttons, add commands to the
'Callback' functions in the m-file. For example, when the
user clicks the Draw Image button, the
drawimage_Callback function will be called and executed
• All the data for the GUI is stored in the handles, so use set
and get to get data and change it if necessary
• Any time you change the handles, save it using guidata
» guidata(handles.Figure1,handles);
11
MATLAB version 6. . ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n . See
.mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o t eir res e tive olders.
Running the GUI
• To run the GUI, just type its name in the command window
and the GUI will pop up. The debugger is really helpful for
writing GUIs because it lets you see inside the GUI
12
MATLAB version 6. . ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n . See
.mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o t eir res e tive olders.
GUI Helper Functions
13
SIMULINK
14
Simulink .0. . ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n . See
.mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o t eir res e tive olders.
Getting Started
• In MATLAB,
Start
art Simulink
•Create a new
Simulink file,
similar to how
you make a new
script
15
MATLAB R2019b and Simulink 7.1. ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n .
See .mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o t eir res e tive
olders.
Simulink Library Browser
• The Library Browser contains various blocks that you can
put into your model
• Examine some blocks:
! Click on a library: “Sources”
– Drag a block into Simulink: “Band limited white noise”
! Visualize the block by going into “Sinks”
– Drag a “Scope” into Simulink
16
MATLAB version 7.6.0 and Simulink 7.1. ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks
n . See .mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o t eir
res e tive olders.
Connections
• Click on the carat/arrow on the right of the band
limited white noise box
Simulink 7.1. ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n . See
.mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o t eir res e tive
olders.
Connections, Block Specification
• To split connections, hold down ‘Ctrl’ when clicking on a
connection, and drag it to the target block; or drag
backwards from the target block
• To modify properties of a block, double-click it and fill in
the property values.
18
Simulink 7.1. ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n . See
.mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o t eir res e tive
olders.
Behind the curtain
• Go to “Simulation”->”Configuration Parameters”
at the top menu
See ode45? Change the solver type here
19
Simulink 7.1. ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n . See
.mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o t eir res e tive
olders.
Exercise: Bouncing Ball Model
v0
v
g
h0
κv
20
Exercise: Bouncing Ball Model
22
Simulink 7.1. ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n . See
.mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o t eir res e tive
olders.
Exercise: Simulink Results
• Running the model yields the balls height and velocity as a
function of time
23
Simulink 7.1. ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n . See
.mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o t eir res e tive
olders.
Toolboxes
• Math
• Takes the signal and performs a math operation
» Add, subtract, round, multiply, gain, angle
• Continuous
• Adds differential equations to the system
» Integrals, Derivatives, Transfer Functions,
State Space
• Discontinuities
• Adds nonlinearities to your system
• Discrete
• Simulates discrete difference equations
• Useful for digital systems
24
Building systems
• Sources
» Step input, white noise, custom input, sine
wave, ramp input,
• Provides input to your system
• Sinks
» Scope: Outputs to plot
» simout: Outputs to a MATLAB vector (struct) on
workspace
» Matlab mat file
25
Symbolic Toolbox
Advantages Disadvantages
• Or use syms
» syms x y real
• shorthand for x=sym('x','real'); y=sym('y','real');
27
Symbolic Expressions
» expand((a-c)^2);
• multiplies out
» factor(ans)
• factors the expression
» pretty(ans)
• makes it look nicer
28
Cleaning up Symbolic Statements
» collect(3*x+4*y-1/3*x^2-x+3/2*y)
• collects terms
» simplify(cos(x)^2+sin(x)^2)
• simplifies expressions
» subs('c^2',c,5)
• replaces variables with numbers
or expressions. To do multiple substitutions
pass a cell of variable names followed by a cell of values
» subs('c^2',c,x/7)
29
More Symbolic Operations
31
Exercise: Symbolics
» syms a b r x y
» solve('(x-a)^2+(y-b)^2=r^2','x')
» solve('(x-a)^2+(y-b)^2=r^2','y')
33
Courtesy of The MathWorks, Inc. Used with permission. MATLAB and Simulink are registered trademarks of The MathWorks, Inc.
See www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their
respective holders.
Image Processing
• Image enhancement
• Adjust image contrast, intensities, etc.
• Filtering and deblurring
• Convolution and deconvolution
• Finding edges
• Image gradient, edge
• Finding circles
• Hough transform
34
Image Processing
• Image Restoration
• Denoising
• Edge Detection
– edge
• Image Sharpening
• Image Segmentation
• Image Compression
35
• Wavelet toolbox (Chap. 3 of Gonzalez book on DIP)
Lena ima e la o . All ri ts reserved. T is ontent is e luded rom our reative ommons li ense. or more in ormation see tt s o .mit.edu el a air use
Exercise: Contrast Improvement
36
Exercise: Contrast Improvement
» % Loading the our image into the workspace
» Image = imread('pout.tif');
»
» % For comparison, it is better to have a predefined width
» width = 200;
»
» % Resizing the image using bicubic interpolation
» dim = size(Image);
» Image = imresize(Image , width * [dim(1) / dim(2) 1] , 'bicubic');
»
» % Adjusting the contrast using imadjust
» Image_imadjust = imadjust(Image);
»
» % Adjusting the contrast using histogram equalization
» Image_histeq = histeq(Image);
»
» % Adjusting the contrast using adaptive histogram equalization
» Image_adapthisteq = adapthisteq(Image);
»
37
Exercise: Contrast Improvement
» % Displaying the original image and the results in a single figure to compare with each other
» figure
» subplot(2 , 2 , 1);
» imshow(Image);
» title('Original Image');
»
» subplot(2 , 2 , 2);
» imshow(Image_imadjust);
» title('Enhanced Image using Imadjust');
»
» subplot(2 , 2 , 3);
» imshow(Image_histeq);
» title('Enhanced Image using Histeq');
»
» subplot(2 , 2 , 4);
» imshow(Image_adapthisteq);
» title('Enhanced Image using Adapthisteq');
38
Exercise: Contrast Improvement
39
ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n .
See .mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o t eir
res e tive olders.
Exercise: Edge Detection
40
Exercise: Edge Detection
» I = imread('circuit.tif');
» I1 = edge(I , 'sobel');
» I2 = edge(I , 'canny');
» I3 = edge(I , 'prewitt');
»
» figure
» subplot(2 , 2 , 1);
» imshow(I);
» title('Original Image');
»
» subplot(2 , 2 , 2);
» imshow(I1);
» title('Edges found using sobel filter');
»
» subplot(2 , 2 , 3);
» imshow(I2);
» title('Edges found using the "canny" method');
»
» subplot(2 , 2 , 4);
» imshow(I3);
» title('Edges found using prewitt filter');
41
Exercise: Edge Detection
Original Image Edges found using sobel filter
Edges found using the "canny" method Edges found using prewitt filter
42
Steve e ker and S u aat adeem. All ri ts reserved. T is ontent is e luded rom our reative ommons li ense. or more in ormation
see tt s o .mit.edu el a air use
Image Enhancement
ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n .
See .mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o t eir
res e tive olders.
Filtering and Deblurring
Pillbox filter:
f = fspecial('disk',10);
imblur = imfilter(im,f);
deconvblind(imblur,f);
44
Lena ima e la o . All ri ts reserved. T is ontent is e luded rom our reative ommons li ense. or more in ormation see tt s o .mit.edu el a air use
Finding Edges
Original
(coins.png) Sobel Laplacian Canny
oins ima e ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n .
45
See .mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o t eir
res e tive olders.
Lena ima e la o . All ri ts reserved. T is ontent is e luded rom our reative ommons li ense. or more in ormation see tt s o .mit.edu el a air use
Other Cool Stuff
• Finding circles
» im = imread('coins.png');
» [centers,radii,metric] = imfindcircles(im, [15 30]);
• Finds circles with radii within range, ordered by strength
» imshow(im)
» viscircles(centers(1:5,:), radii(1:5));
46
ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n .
See .mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o t eir
res e tive olders.
… and also Computer Vision
• http://www.mathworks.com/help/vision/index.html
47
ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n .
See .mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o
t eir res e tive olders.
… and also Computer Vision
• http://www.mathworks.com/help/vision/functionlist.html
ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n .
See .mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks
o t eir res e tive olders.
Object Detection
• Inputs to trainCascadeObjectDetector:
• Image files with bounding boxes for positive instances
• Image files of negative instances (‘background’)
• Optional: FP/TP rates, # cascade stages, feature type
• Output: An XML file with object detector parameters
» detector=vision.CascadeObjectDetector('my.xml');
50
48
ourtes o T e Mat orks n . sed it ermission. MATLAB and Simulink are re istered trademarks o T e Mat orks n .
See .mat orks. om trademarks or a list o additional trademarks. t er rodu t or rand names ma e trademarks or re istered trademarks o
t eir res e tive olders.
Hardware Interface
51
Low Level
52
GPIB