Computer Vision System Toolbox™ User's Guide
Computer Vision System Toolbox™ User's Guide
Users Guide
R2014a
Web
Newsgroup
www.mathworks.com/contact_TS.html Technical Support
www.mathworks.com
comp.soft-sys.matlab
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
508-647-7000 (Phone)
508-647-7001 (Fax)
The MathWorks, Inc.
3 Apple Hill Drive
Natick, MA 01760-2098
For contact information about worldwide offices, see the MathWorks Web site.
Computer Vision System Toolbox Users Guide
COPYRIGHT 20002014 by The MathWorks, Inc.
The software described in this document is furnished under a license agreement. The software may be used
or copied only under the terms of the license agreement. No part of this manual may be photocopied or
reproduced in any form without prior written consent from The MathWorks, Inc.
FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation
by, for, or through the federal government of the United States. By accepting delivery of the Program
or Documentation, the government hereby agrees that this software or documentation qualifies as
commercial computer software or commercial computer software documentation as such terms are used
or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and
conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and govern
the use, modification, reproduction, release, performance, display, and disclosure of the Program and
Documentation by the federal government (or other entity acquiring for or through the federal government)
and shall supersede any conflicting contractual terms or conditions. If this License fails to meet the
governments needs or is inconsistent in any respect with federal procurement law, the government agrees
to return the Program and Documentation, unused, to The MathWorks, Inc.
Trademarks
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.
Patents
MathWorks products are protected by one or more U.S. patents. Please see
www.mathworks.com/patents for more information.
Revision History
July 2004
October 2004
March 2005
September 2005
November 2005
March 2006
September 2006
March 2007
September 2007
March 2008
October 2008
March 2009
September 2009
March 2010
September 2010
April 2011
September 2011
March 2012
September 2012
March 2013
September 2013
March 2014
First printing
Second printing
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Contents
Input, Output, and Conversions
1
Export to Video Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting Block Parameters for this Example . . . . . . . . . . . .
Configuration Parameters . . . . . . . . . . . . . . . . . . . . . . . . . .
1-2
1-3
1-3
1-4
1-5
1-6
1-7
1-8
1-9
1-10
1-11
1-12
1-12
1-15
1-16
1-17
1-17
1-17
1-18
1-18
1-19
1-21
1-21
1-23
1-25
1-27
1-28
1-40
1-45
Data Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Video Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Video Data Stored in Column-Major Format . . . . . . . . . . .
Image Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1-50
1-50
1-51
1-51
1-28
1-33
vi
Contents
Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
View Streaming Video in MATLAB . . . . . . . . . . . . . . . . . . .
Preview Video in MATLAB using MPlay Function . . . . . . .
View Video with Simulink Blocks . . . . . . . . . . . . . . . . . . . .
View Video with MPlay . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
MPlay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-2
2-2
2-2
2-3
2-4
2-7
Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Abandoned Object Detection . . . . . . . . . . . . . . . . . . . . . . . .
Abandoned Object Detection . . . . . . . . . . . . . . . . . . . . . . . .
Annotate Video Files with Frame Numbers . . . . . . . . . . . .
Draw Shapes and Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-25
2-25
2-27
2-31
2-33
3
Feature Detection, Extraction, and Matching . . . . . . . .
Measuring Planar Objects with a Calibrated Camera . . . .
Digit Classification Using HOG Features . . . . . . . . . . . . . .
Detect Edges in Images . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Detect Lines in Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Video Stabilization Using Point Feature Matching . . . . . .
Video Stabilization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Measure Angle Between Lines . . . . . . . . . . . . . . . . . . . . . . .
3-2
3-2
3-8
3-15
3-23
3-26
3-34
3-37
............
3-47
3-52
3-52
3-52
3-53
3-58
3-60
3-63
3-64
3-70
3-73
3-73
3-74
3-74
3-76
3-83
3-91
vii
Object Detection
4
Label Images for Classification Model Training . . . . . .
Training Image Labeler . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4-2
4-2
4-2
4-4
4-24
viii
Contents
Object Tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Face Detection and Tracking Using the KLT Algorithm . .
Using Kalman Filter for Object Tracking . . . . . . . . . . . . . .
Motion-Based Multiple Object Tracking . . . . . . . . . . . . . . .
5-2
5-2
5-6
5-16
Video Mosaicking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5-28
Video Mosaicking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5-32
Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5-35
Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5-40
5-41
Panorama Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5-47
Geometric Transformations
6
Rotate an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6-2
Resize an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6-8
Crop an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6-12
Interpolation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Nearest Neighbor Interpolation . . . . . . . . . . . . . . . . . . . . . .
Bilinear Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Bicubic Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6-17
6-17
6-18
6-19
Video Stabilization
................................
6-21
Video Stabilization
................................
6-25
7
Histogram Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7-2
.............
7-3
7-8
.........
7-13
ix
Sharpen an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7-19
8
Find the Histogram of an Image . . . . . . . . . . . . . . . . . . . . .
8-2
8-8
.........................
8-15
Fixed-Point Design
Contents
9-2
9-2
9-2
9-4
9-4
9-5
9-6
Arithmetic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Modulo Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Twos Complement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Addition and Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . .
Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Casts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9-10
9-10
9-11
9-12
9-13
9-16
9-21
9-21
9-3
9-22
9-23
9-25
9-25
9-28
9-29
9-40
Code Generation
10
Code Generation for Computer Vision Processing in
MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10-2
10-4
11
Define Basic System Objects . . . . . . . . . . . . . . . . . . . . . . . .
11-3
11-5
11-8
xi
. . . . . . . . . . . . . . . . . . . . . . . 11-38
. . . . . . . . . . . . . . . . . . 11-47
. . . . . . . . . . . . . . . . . . . . . . . . . . 11-49
xii
Contents
. . 11-72
Methods Timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setup Method Call Sequence . . . . . . . . . . . . . . . . . . . . . . . .
Step Method Call Sequence . . . . . . . . . . . . . . . . . . . . . . . . .
Reset Method Call Sequence . . . . . . . . . . . . . . . . . . . . . . . .
Release Method Call Sequence . . . . . . . . . . . . . . . . . . . . . . .
11-75
11-75
11-76
11-76
11-77
xiii
xiv
Contents
1
Input, Output, and
Conversions
Learn how to import and export videos, and perform color space and video
image conversions.
Export to Video Files on page 1-2
Import from Video Files on page 1-4
Batch Process Image Files on page 1-7
Display a Sequence of Images on page 1-9
Partition Video Frames to Multiple Image Files on page 1-12
Combine Video and Audio Streams on page 1-17
Import MATLAB Workspace Variables on page 1-19
Transmit Audio and Video Content Over Network on page 1-21
Resample Image Chroma on page 1-23
Convert Intensity to Binary Images on page 1-28
Convert RGB to Intensity Images on page 1-40
Process Multidimensional Color Video Signals on page 1-45
Data Formats on page 1-50
By increasing the red, green, and blue color values, you increase the contrast
of the video. The To Multimedia File block exports the video data from the
Simulink model to a multimedia file that it creates in your current folder.
This example manipulated the video stream and exported it from a Simulink
model to a multimedia file. For more information, see the To Multimedia File
block reference page.
1-2
Parameter
Gain
To Multimedia File
Same as input
Configuration Parameters
You can locate the Model Configuration Parameters by selecting Model
Configuration Parameters from the Simulation menu. For this example,
the parameters on the Solver pane, are set as follows:
Stop time = 20
Type = Fixed-step
Solver = Discrete (no continuous states)
1-3
Note The video that is displayed in the To Video Display window runs at
the frame rate that corresponds to the input sample time. To run the video
as fast as Simulink processes the video frames, use the Video Viewer block.
You have now imported and displayed a multimedia file in the Simulink
model. In the Export to Video Files on page 1-2 example you can manipulate
your video stream and export it to a multimedia file.
1-4
For more information on the blocks used in this example, see the From
Multimedia File and To Video Display block reference pages.
Parameter
From Multimedia
File
To Video Display
1-5
Configuration Parameters
You can locate the Model Configuration Parameters by selecting Model
Configuration Parameters from the Simulation menu. For this example,
the parameters on the Solver pane, are set as follows:
Stop time = 20
Type = Fixed-step
Solver = Discrete (no continuous states)
1-6
I = imread(fileNames{1});
imshow(I);
text(size(I,2),size(I,1)+15, ...
'Image files courtesy of Alan Partin', ...
'FontSize',7,'HorizontalAlignment','right');
text(size(I,2),size(I,1)+25, ....
'Johns Hopkins University', ...
'FontSize',7,'HorizontalAlignment','right');
3 Use a for loop to create a variable that stores the entire image sequence.
You can use this variable to import the sequence into Simulink.
for i = 1:length(fileNames)
my_video(:,:,i) = imread(fileNames{i});
end
For additional information about batch processing, see the Batch Processing
Image Files in Parallel example in the Image Processing Toolbox.
1-7
Configuration Parameters
You can locate the Model Configuration Parameters by selecting Model
Configuration Parameters from the Simulation menu. For this example,
the parameters on the Solver pane, are set as follows:
Stop time = 10
Type = Fixed-step
Solver = Discrete (no continuous states)
1-8
workspace. The Signal parameter is set to the name of the variable for the
stored images. For this example, it is set to my_video.
2 The Video Viewer block displays the sequence of images.
3 Run your model. You can view the image sequence in the Video Viewer
window.
1-9
4 Because the Video From Workspace blocks Sample time parameter is set
Pre-loading Code
To find or modify the pre-loaded code, select File > Model Properties >
Model Properties. Then select the Callbacks tab. For more details on how
to set-up callbacks, see Callbacks for Customized Model Behavior.
1-10
Configuration Parameters
You can locate the Model Configuration Parameters by selecting Model
Configuration Parameters from the Simulation menu. For this example,
the parameters on the Solver pane, are set as follows:
Stop time = 10
Type = Fixed-step
Solver = Discrete (no continuous states)
1-11
command prompt:
mplay output1.avi
mplay output2.avi
mplay output3.avi
For more information on the blocks used in this example, see the From
Multimedia File, Insert Text, Enabled Subsystem, and To Multimedia File
block reference pages.
1-12
Block
Parameter
From Multimedia
File
Insert Text
To Multimedia
File
Counter
1-13
Block
Parameter
Bias
Compare to
Constant
Compare to
Constant1
Compare to
Constant2
1-14
Block
Parameter
Compare to
Constant3
Compare to
Constant4
1-15
Configuration Parameters
You can locate the Model Configuration Parameters by selecting Model
Configuration Parameters from the Simulation menu. For this example,
the parameters on the Solver pane, are set as follows:
Solver pane, Type = Fixed-step
Solver pane, Solver = Discrete (no continuous states)
1-16
1-17
the frame period is also defined as the frame size divided by frequency, you
can calculate the frame period of the audio signal by dividing the frame size of
the audio signal (735 samples per frame) by the frequency (22050 samples per
second) to get 0.0333 seconds per frame.
frame period = (frame size)/(frequency)
frame period = (735 samples per frame)/(22050 samples per second)
frame period = 0.0333 seconds per frame
Alternatively, you can verify that the frame period of the audio and video
signals is the same using a Simulink Probe block.
Configuration Parameters
You can locate the Configuration Parameters by selecting Model
Configuration Parameters from the Simulation menu. The parameters,
on the Solver pane, are set as follows:
Stop time = 10
Type = Fixed-step
Solver = Discrete (no continuous states)
1-18
1-19
Use the Signal parameter to specify the MATLAB workspace variable from
which to read. For more information about how to use this block, see the
Video From Workspace block reference page.
1-20
1-21
sendReceive = 'send';
url = 'mms://127.0.0.1:81';
filename = 'vipmen.avi';
1-22
1-23
1-24
This command reads in an RGB image from a TIF file. The image I is a
206-by-345-by-3 array of 8-bit unsigned integer values. Each plane of this
array represents the red, green, or blue color values of the image.
2 To view the image this array represents, at the MATLAB command
prompt, type:
imshow(I)
3 Configure Simulink to display signal dimensions next to each signal line.
Parameter
Image from
Workspace
Image Pad
1-25
Block
Parameter
Add row to = No padding
The Image Pad block adds one column to the right of
each plane of the array by repeating its border values.
This padding minimizes the effect of the pixels outside
the image on the processing of the image.
Note When you process video streams, be aware that it
is computationally expensive to pad every video frame.
You should change the dimensions of the video stream
before you process it with Computer Vision System
Toolbox blocks.
Selector,
Selector1,
Selector2
1-26
Block
Parameter
Index 1 = Select all
Index 2 = Select all
Index 3 = Index vector (dialog) and Index = 3
Color Space
Conversion
Chroma
Resampling
Chroma
Resampling1
Color Space
Conversion1
Video Viewer
Configuration Parameters
Open the Configuration dialog box by selecting Model Configuration
Parameters from the Simulation menu. Set the parameters as follows:
Solver pane, Stop time = 0
Solver pane, Type = Fixed-step
Solver pane, Solver = Discrete (no continuous states)
1-27
following table.
Block
Library
Quantity
Video Viewer
Relational Operator
Constant
2 Use the Image from File block to import your image. In this example the
1-28
3 Use the Video Viewer1 block to view the original intensity image. Accept
Operator block. Since the pixel values range from 0 to 255, set the
Constant value parameter to 128. This value is image dependent.
5 Use the Relational Operator block to perform a thresholding operation
that converts your intensity image to a binary image. Set the Relational
Operator parameter to >. If the input to the Relational Operator block
is greater than 128, its output is a Boolean 1; otherwise, its output is
a Boolean 0.
6 Use the Video Viewer block to view the binary image. Accept the default
parameters.
7 Connect the blocks as shown in the following figure.
1-29
1-30
1-31
1-32
ex_vision_autothreshold
1 If the model you created in Thresholding Intensity Images Using
Relational Operators on page 1-28 is not open on your desktop, you can
open the model by typing
ex_vision_thresholding_intensity
1-33
2 Use the Image from File block to import your image. In this example the
1-34
1-35
7 Double-click the Image From File block. On the Data Types pane, set the
1-36
1-37
In the model window, the Display block shows the threshold value,
calculated by the Autothreshold block, that separated the rice grains from
the background.
1-38
1-39
this array represents the red, green, or blue color values of the image.
2 To view the image this matrix represents, at the MATLAB command
prompt, type
imshow(I)
1-40
3 Create a new Simulink model, and add to it the blocks shown in the
following table.
Block
Library
Quantity
Image From
Workspace
Color Space
Conversion
Video Viewer
4 Use the Image from Workspace block to import your image from the
1-41
5 Use the Color Space Conversion block to convert the input values from the
image using the Video Viewer1 block. Accept the default parameters.
7 Connect the blocks so that your model is similar to the following figure.
1-42
The image displayed in the Video Viewer window is the intensity version
of the greens.jpg image.
1-43
In this topic, you used the Color Space Conversion block to convert color
information from the RGB color space to intensity. For more information on
this block, see the Color Space Conversion block reference page.
1-44
1-45
1-46
You can also choose to work with the individual color planes of images or
video signals. For example, the following model passes a color image from a
source block to a sink block using three separate color planes.
ex_vision_process_individual
1-47
To process the individual color planes of an image or video signal, set the
Image signal parameter to Separate color signals in both the Image
From File and Video Viewer block dialog boxes.
1-48
1-49
Data Formats
In this section...
Video Formats on page 1-50
Video Data Stored in Column-Major Format on page 1-51
Image Formats on page 1-51
Video Formats
Video data is a series of images over time. Video in binary or intensity format
is a series of single images. Video in RGB format is a series of matrices
grouped into sets of three, where each matrix represents an R, G, or B plane.
White or Saturation
of Color
Fixed point
Floating point
Data Type
Note The Computer Vision System Toolbox software considers any data
type other than double-precision floating point and single-precision floating
point to be fixed point.
1-50
Data Formats
For example, for an intensity image whose image values are 8-bit unsigned
integers, 0 is black and 255 is white. For an intensity image whose image
values are double-precision floating point, 0 is black and 1 is white. For an
intensity image whose image values are 16-bit signed integers, -32768 is
black and 32767 is white.
For an RGB image whose image values are 8-bit unsigned integers, 0 0 0
is black, 255 255 255 is white, 255 0 0 is red, 0 255 0 is green, and 0 0 255
is blue. For an RGB image whose image values are double-precision
floating point, 0 0 0 is black, 1 1 1 is white, 1 0 0 is red, 0 1 0 is green,
and 0 0 1 is blue. For an RGB image whose image values are 16-bit
signed integers, -32768 -32768 -32768 is black, 32767 32767 32767 is
white, 32767 -32768 -32768 is red, -32768 32767 -32768 is green, and
-32768 -32768 32767 is blue.
Image Formats
In the Computer Vision System Toolbox software, images are real-valued
ordered sets of color or intensity data. The blocks interpret input matrices as
images, where each element of the matrix corresponds to a single pixel in the
displayed image. Images can be binary, intensity (grayscale), or RGB. This
section explains how to represent these types of images.
1-51
Binary Images
Binary images are represented by a Boolean matrix of 0s and 1s, which
correspond to black and white pixels, respectively.
For more information, see Binary Images in the Image Processing Toolbox
documentation.
Intensity Images
Intensity images are represented by a matrix of intensity values. While
intensity images are not stored with colormaps, you can use a gray colormap
to display them.
For more information, see Grayscale Images in the Image Processing
Toolbox documentation.
RGB Images
RGB images are also known as a true-color images. With Computer Vision
System Toolbox blocks, these images are represented by an array, where the
first plane represents the red pixel intensities, the second plane represents
the green pixel intensities, and the third plane represents the blue pixel
intensities. In the Computer Vision System Toolbox software, you can
pass RGB images between blocks as three separate color planes or as one
multidimensional array.
For more information, see Truecolor Images in the Image Processing
Toolbox documentation.
1-52
2
Display and Graphics
Display on page 2-2
Graphics on page 2-25
Display
In this section...
View Streaming Video in MATLAB on page 2-2
Preview Video in MATLAB using MPlay Function on page 2-2
To Video Display Block on page 2-3
View Video with MPlay on page 2-4
MPlay on page 2-7
2-2
Display
The MPlay GUI enables you to view videos directly from files without having
to load all the video data into memory at once. The following procedure shows
you how to use the MPlay GUI to load and view a video one frame at a time:
1 On the MPlay GUI, click open file icon,
2 Use the Connect to File dialog box to navigate to the multimedia file you
video stream.
2-3
vipmplaytut
2-4
Display
2-5
element,
button.
MPlay window. You can also use multiple MPlay GUIs to display different
Simulink signals.
Note During code generation, the Simulink Coder does not generate code for
the MPlay GUI.
2-6
Display
MPlay
The following figure shows the MPlay GUI containing an image sequence.
The following sections provide descriptions of the MPlay GUI toolbar buttons
and equivalent menu options.
2-7
Toolbar Buttons
GUI
Menu
Equivalent
Shortcut Keys
and Accelerators
Description
Ctrl+N
Ctrl+P
Ctrl+P
2-8
Ctrl+O
Display
GUI
Menu
Equivalent
Shortcut Keys
and Accelerators
Description
File >
Import from
Workspace
Ctrl+I
File >
Connect to
Simulink
Signal
Ctrl+E
N/A
N/A
2-9
GUI
Menu
Equivalent
Shortcut Keys
and Accelerators
Description
N/A
N/A
Tools >
Maintain Fit
to Window
N/A
N/A
N/A
2-10
Menu
Equivalent
Shortcut Keys
and Accelerators
Description
Playback >
Go to First
F, Home
Playback >
Rewind
Up arrow
Playback >
Step Back
Playback >
Stop
Playback >
Play
P, Space bar
Playback >
Pause
P, Space bar
Display
GUI
Menu
Equivalent
Shortcut Keys
and Accelerators
Description
Playback
> Step
Forward
Playback >
Fast Forward
Down arrow
Playback >
Go to Last
L, End
Playback >
Jump to
Playback >
Playback
Modes >
Repeat
Playback >
Playback
Modes >
Forward play
Playback >
A
Playback
Modes >
Backwardplay
Playback >
Playback
Modes >
AutoReverse
play
2-11
2-12
Menu
Equivalent
Shortcut Keys
and Accelerators
Description
Simulation >
Stop
Simulation >
Start
P, Space bar
Simulation >
Pause
P, Space bar
Simulation
> Step
Forward
Simulation
> Simulink
Snapshot
N/A
View >
Highlight
Simulink
Signal
Ctrl+L
Simulation
> Floating
Signal
Connection
(not selected)
N/A
Simulation
> Floating
Signal
N/A
Display
GUI
Menu
Equivalent
Shortcut Keys
and Accelerators
Connection
(selected)
Description
Configuration
The MPlay Configuration dialog box enables you to change the behavior and
appearance of the GUI as well as the behavior of the playback shortcut keys.
To open the Configuration dialog box, select File > Configuration
Set > Edit.
To save the configuration settings for future use, select
File > Configuration Set > Save as.
Note By default, the MPlay GUI uses the configuration settings from the
file mplay.cfg. Create a backup copy of the file to store your configuration
settings.
To load a preexisting configuration set, select File > Configuration
Set > Load.
Configuration Core Pane
The Core pane controls the graphic user interface (GUI) general and source
settings.
2-13
General UI
Click General UI, and then select the Options button to open the General
UI Options dialog box.
If you select the Display the full source path in the title bar check box,
the full Simulink path appears in the title bar. Otherwise, the title bar
displays a shortened name.
Use the Message log opens parameter to control when the Message log
window opens. You can use this window to debug issues with video playback.
Your choices are for any new messages, for warn/fail messages, only
for fail messages, or manually.
2-14
Display
Source UI
Click Source UI, and then click the Options button to open the Source UI
Options dialog box.
Using the right keyboard arrow key moves the video backward, and
using the left keyboard arrow key moves the video forward.
With MPlay set to play backwards, the keyboard forward performs
forward with the direction the video is playing.
To disconnect the keyboard behavior from the MPlay playback settings, clear
the check box.
Use the Recently used sources list parameter to control the number of
sources you see in the File menu.
2-15
Click File, and then click the Options button to open the Sources:File
Options dialog box.
Use the Default open file path parameter to control the folder that is
displayed in the Connect to File dialog box. The Connect to File dialog
box becomes available when you select File > Open.
2-16
Display
Click Simulink, and then click the Options button to open the
Sources:Simulink Options dialog box.
You can have the Simulink model associated with an MPlay GUI to open
with MPlay. To do so, select the Load Simulink model if not open check
box.
Select Signal lines only to sync the video display only when you select
a signal line. If you select a block the video display will not be affected.
Select Signal lines or blocks to sync the video display to the signal line or
block you select. The default is Signal lines only.
Configuration Visuals Pane
The Visuals pane contains the name of the visual type and its description.
2-17
2-18
Display
Click Image Tool, and then click the Options button to open the Image
Tool Options dialog box.
Select the Open new Image Tool window for export check box if you want
to open a new Image Tool for each exported frame.
Pixel Region
Select the Pixel Region check box to display and enable the pixel region GUI
button. For more information on working with pixel regions, see Getting
Information about the Pixels in an Image.
Image Navigation Tools
Select the Image Navigation Tools check box to enable the pan-and-zoom
GUI button.
Instrumentation Set
Select the Instrumentation Set check box to enable the option to load and
save viewer settings. The option appears in the File menu.
Video Information
The Video Information dialog box lets you view basic information about the
video. To open this dialog box, select Tools > Video Information or click
the information button
2-19
Use the Colormap parameter to specify the colormap to apply to the intensity
video.
2-20
Display
Sometimes, the pixel values do not use the entire data type range. In such
cases, you can select the Specify range of displayed pixel values check
box. You can then enter the range for your data. The dialog box automatically
displays the range based on the data type of the pixel values.
Frame Rate
The Frame Rate dialog box displays the frame rate of the source. It also lets
you change the rate at which the MPlay GUI plays the video and displays the
actual playback rate.
Note This dialog box becomes available when you use the MPlay GUI to
view a video signal.
The playback rate is the number of frames the GUI processes per second. You
can use the Desired playback rate parameter to decrease or increase the
playback rate. To open this dialog box, select Playback > Frame Rate or
press T.
To increase the playback rate when system hardware cannot keep pace
with the desired rate, select the Allow frame drop to achieve desired
playback rate check box. This parameter enables the MPlay GUI to achieve
2-21
the playback rate by dropping video frames. Dropped video frames sometimes
cause lower quality playback.
You can refine further the quality of playback versus hardware burden, by
controlling the number of frames to drop per frame or frames displayed.
For example, suppose you set the Desired playback rate to 80 frames/sec.
One way to achieve the desired playback rate is to set the Playback schedule
to Show 1 frame, Drop 3 frames. Change this playback schedule, by
setting the refresh rates (which is how often the GUI updates the screen), to:
Maximum refresh rate: 21 frames/sec
Minimum refresh rate: 20 frames/sec
MPlay can achieve the desired playback rate (in this case, 80 frames/sec)
by using these parameter settings.
In general, the relationship between the Frame Drop parameters is:
2-22
Display
2-23
The Type parameter allows you to select either All, Info, Warn, or Fail
message logs.
The Category parameter allows you to select either Configuration or
Extension message summaries.
The Configuration message indicates a new configuration file loaded.
The Extension message indicates a registered component. For example,
a Simulink message, indicating a registered component, available for
configuration.
Status Bar
Along the bottom of the MPlay viewer is the status bar. It displays
information, such as video status, Type of video playing (I or RGB), Frame size,
Percentage of frame rate, Frame rate, and Current frame: Total frames.
Note A minus sign (-) for Current frame indicates reverse video playback.
2-24
Graphics
Graphics
In this section...
Abandoned Object Detection on page 2-25
Abandoned Object Detection on page 2-27
Annotate Video Files with Frame Numbers on page 2-31
Draw Shapes and Lines on page 2-33
The following figure shows the Abandoned Object Detection example model.
This example uses the first frame of the video as the background. To improve
accuracy, the example uses both intensity and color information for the
background subtraction operation. During this operation, Cb and Cr color
channels are stored in a complex array.
2-25
Detect Subsystem
The Detect subsystem contains the main algorithm. Inside this subsystem,
the Luminance Segmentation and Color Segmentation subsystems perform
background subtraction using the intensity and color data. The example
combines these two segmentation results using a binary OR operator. The
Blob Analysis block computes statistics of the objects present in the scene.
Abandoned Object Tracker subsystem, shown below, uses the object statistics
to determine which objects are stationary. To view the contents of this
subsystem, right-click the subsystem and select Look Under Mask. To
view the tracking algorithm details, double-click the Abandoned Object
Tracker block. The MATLAB code in this block is an example of how to
implement your custom code to augment Computer Vision System Toolbox
functionality.
The All Objects window marks the region of interest (ROI) with a yellow box
and all detected objects with green boxes.
2-26
Graphics
Use these next sections of code to initialize the required variables and System
objects.
Rectangular ROI [x y width height], where [x y] is the uppef left corner of
the ROI
2-27
areaChangeFraction = 13;
% Maximum allowable change in object area in p
centroidChangeFraction = 18; % Maximum allowable change in object centroid
% Minimum ratio between the number of frames in which an object is detected
% and the total number of frames, for that object to be tracked.
minPersistenceRatio = 0.7;
% Offsets for drawing bounding boxes in original input video
PtsOffset = int32(repmat([roi(1), roi(2), 0, 0],[maxNumObj 1]));
Create a BlobAnalysis System object to find the area, centroid, and bounding
box of the objects in the video.
2-28
Graphics
2-29
The Abandoned Objects window highlights the abandoned objects with a red
box. The All Objects window marks the region of interest (ROI) with a
yellow box and all detected objects with green boxes. The Threshold window
shows the result of the background subtraction in the ROI.
2-30
Graphics
2-31
Color Formatting
For this example, the color format for the video was set to Intensity, and
therefore the color value for the text was set to a scaled value. If instead, you
set the color format to RGB, then the text value must satisfy this format, and
requires a 3-element vector.
Inserting Text
Use the Insert Text block to annotate the video stream with a running frame
count. Set the block parameters as follows:
2-32
Graphics
Configuration Parameters
Set the configuration parameters. Open the Configuration dialog box by
selecting Model Configuration Parameters from the Simulation menu.
Set the parameters as follows:
Solver pane, Stop time = inf
Solver pane, Type = Fixed-step
Solver pane, Solver = Discrete (no continuous states)
2-33
Rectangle
Shape
PTS input
Single Rectangle
Drawn Shape
M Rectangles
M-by-4 matrix
x1
x
2
xM
y1
y2
width1
width2
yM
widthM
heightM
height1
height2
2-34
Graphics
PTS input
Single Line
Drawn Shape
M Lines
M-by-4 matrix
x11
x
21
xM1
y11
y21
x12
x22
yM 1
xM 2
y12
y22
yM 2
2-35
Shape
PTS input
Drawn Shape
2L-by-N matrix
x11
x
21
xM1
y11
y21
x12
x22
y12
y22
yM 1
xM 2
yM 2
x1 L
x2 L
xML
y1 L
y2 L
yML
2-36
Graphics
Polygon
You can draw one or more polygons.
Shape
PTS input
Drawn Shape
2-37
Shape
PTS input
Drawn Shape
M-by-2L matrix
x11
x
21
xM1
y11
y21
x12
x22
y12
y22
x1 L
x2 L
yM 1
xM 2
yM 2 xML
yML
y1 L
y2 L
2-38
Graphics
Circle
You can draw one or more circles.
2-39
Shape
Single Circle
PTS input
Drawn Shape
M Circles
M-by-3 matrix
x1
x
2
xM
y1
y2
yM
radius1
radius2
radiusM
2-40
3
Registration and Stereo
Vision
Feature Detection, Extraction, and Matching on page 3-2
Uncalibrated Stereo Image Rectification on page 3-47
Find Camera Parameters with the Camera Calibrator on page 3-52
Stereo Calibration and Scene Reconstruction on page 3-76
Sparse 3-D Reconstruction From Multiple Views on page 3-83
This example shows how to calibrate a camera, and then use it to measure the
size of planar objects, such as coins. An example application of this approach
is measuring parts on a conveyor belt for quality control.
Calibrate the Camera
3-2
numImages = 9;
files = cell(1, numImages);
for i = 1:numImages
files{i} = fullfile(matlabroot, 'toolbox', 'vision', 'visiondemos', ...
'calibration', 'slr', sprintf('image%d.jpg', i));
end
% Display one of the calibration images
magnification = 25;
figure; imshow(files{1}, 'InitialMagnification', magnification);
title('One of the Calibration Images');
3-3
The bar graph indicates the accuracy of the calibration. Each bar shows
the mean reprojection error for the corresponding calibration image. The
reprojection errors are the distances between the corner points detected in the
image, and the corresponding ideal world points projected into the image.
Read the Image Of Objects to be Measured
Load the image containing objects to be measured. This image includes the
calibration pattern, and the pattern is in the same plane as the objects you
want to measure. In this example, both the pattern and the coins are on the
same table top.
Alternatively, you could use two separate images: one containing the pattern,
and the other containing the objects to be measured. Again, the objects
and the pattern must be in the same plane. Furthermore, images must be
captured from exactly the same view point, meaning that the camera must
be fixed in place.
Use the cameraParameters object to remove lens distortion from the image.
This is necessary for accurate measurement.
im = undistortImage(imOrig, cameraParams);
d = abs(rgb2gray(im) - rgb2gray(imOrig));
figure; imshow(d, [], 'InitialMagnification', magnification);
title('Difference Between the Original and the Undistorted Images');
Note that this image exhibits very little lens distortion. The undistortion step
is far more important if you use a wide-angle lens, or a low-end webcam.
Segment Coins
3-4
In this case, the coins are colorful on white background. Use the saturation
component of the HSV representation of the image to segment them out.
% Convert the image to the HSV color space.
imHSV = rgb2hsv(im);
% Get the saturation channel.
saturation = imHSV(:, :, 2);
% Threshold the image
t = graythresh(saturation);
imCoin = (saturation > t);
figure; imshow(imCoin, 'InitialMagnification', magnification);
title('Segmented Coins');
Detect Coins
We can assume that the two largest connected components in the segmented
image correspond to the coins.
% Find connected components.
blobAnalysis = vision.BlobAnalysis('AreaOutputPort', true,...
'CentroidOutputPort', false,...
'BoundingBoxOutputPort', true,...
'MinimumBlobArea', 200, 'ExcludeBorderBlobs', true);
[areas, boxes] = step(blobAnalysis, imCoin);
% Sort connected components in descending order by area
[~, idx] = sort(areas, 'Descend');
% Get the two largest components.
boxes = boxes(idx(1:2), :);
% Reduce the size of the image for display.
scale = magnification / 100;
imDetectedCoins = imresize(im, scale);
3-5
Compute Extrinsics
To measure the first coin we convert the top-left and the top-right corners of
the bounding box into world coordinates. Then we compute the Euclidean
distance between them in millimeters. Note that the actual diameter of a
US penny is 19.05 mm.
In the equation below is a point in the image. is the corresponding point in
the world coordinates, which lies on the same plane as the calibration pattern.
To find , we need to solve the following equation in homogeneous coordinates:
3-6
Measure the second coin the same way as the first coin.
% Get the top-left and the top-right corners.
box2 = double(boxes(2, :));
imagePoints2 = [box2(1:2); ...
box2(1) + box2(3), box2(2)];
% Apply the inverse transformation from image to world
worldPoints2 = transformPointsInverse(tform, imagePoints2);
3-7
References
For training, synthetic images are created using the insertText function from
the Computer Vision System Toolbox. The training images each contain a
digit surrounded by other digits, which mimics how digits are normally seen
3-8
subplot(2,3,4); imshow(testImages{2,2});
subplot(2,3,5); imshow(testImages{5,4});
subplot(2,3,6); imshow(testImages{8,9});
Note that prior to training and testing a classifier the following pre-processing
step is applied to images from this dataset:
function J = preProcess(I)
lvl = graythresh(I);
J
= im2bw(I,lvl);
3-9
end
imshow(exTrainImage);
imshow(preProcess(exTrainImage));
imshow(exTestImage);
imshow(preProcess(exTestImage));
The data used to train the SVM classifier are HOG feature vectors extracted
from the training images. Therefore, it is important to make sure the HOG
feature vector encodes the right amount of information about the object. The
extractHOGFeatures function returns a visualization output that can help
form some intuition about just what the "right amount of information" means.
By varying the HOG cell size parameter and visualizing the result, you can
see the effect the cell size parameter has on the amount of shape information
encoded in the feature vector:
img = imread(trainingImages{4,3});
% Extract
[hog_2x2,
[hog_4x4,
[hog_8x8,
3-10
subplot(2,3,5);
plot(vis4x4);
title({'CellSize = [4 4]'; ['Feature length = ' num2str(length(hog_4x4))]})
subplot(2,3,6);
plot(vis8x8);
title({'CellSize = [8 8]'; ['Feature length = ' num2str(length(hog_8x8))]})
The visualization shows that a cell size of [8 8] does not encode much shape
information, while a cell size of [2 2] encodes a lot of shape information but
increases the dimensionality of the HOG feature vector significantly. A good
compromise is a 4-by-4 cell size. This size setting encodes enough spatial
information to visually identify a digit shape while limiting the number
of dimensions in the HOG feature vector, which helps speed up training.
In practice, the HOG parameters should be varied with repeated classifier
training and testing to identify the optimal parameter settings.
cellSize = [4 4];
hogFeatureSize = length(hog_4x4);
Train the Classifier
3-11
for d = 1:numel(digits)
% Pre-allocate trainingFeatures array
numTrainingImages = size(trainingImages,1);
trainingFeatures = zeros(numTrainingImages,hogFeatureSize,'single');
% Extract HOG features from each training image. trainingImages
% contains both positive and negative image samples.
for i = 1:numTrainingImages
img = imread(trainingImages{i,d});
img = preProcess(img);
trainingFeatures(i,:) = extractHOGFeatures(img,'CellSize',cellSize)
end
Now the SVM classifiers can be tested using the handwritten digit images
shown earlier.
% Run each SVM classifier on the test images
for d = 1:numel(digits)
% Pre-allocate testFeatures array
numImages
= size(testImages,1);
testFeatures = zeros(numImages, hogFeatureSize, 'single');
% Extract features from each test image
for i = 1:numImages
img = imread(testImages{i,d});
3-12
img = preProcess(img);
testFeatures(i,:) = extractHOGFeatures(img,'CellSize',cellSize);
end
digit | svm(0)
svm(1)
svm(2)
svm(3)
svm(4)
svm(5)
svm(6)
svm
--------------------------------------------------------------------------0
| 6
0
0
0
0
0
6
0
1
| 3
10
0
0
0
0
0
2
2
| 0
2
8
0
0
0
1
1
3
| 0
0
0
7
0
0
4
0
4
| 0
0
0
0
9
0
0
0
5
| 0
0
0
0
0
4
7
0
6
| 0
0
0
0
2
0
6
0
7
| 0
0
0
1
0
0
0
5
8
| 0
0
0
1
0
0
0
1
9
| 0
1
0
1
1
1
0
0
The columns of the table contain the classification results for each SVM
classifier. Ideally, the table would be a diagonal matrix, where each diagonal
element equals the number of images per digit (12 in this example). Based
on this data set, digit 1, 2, 3, and 4 are easier to recognize compared to digit
6, where there are many false positives. Using more representative data
sets like MNIST [2] or SVHN [3], which contain thousands of handwritten
characters, is likely to produce a better classifier compared with the one
created using this example data set.
3-13
Summary
This example illustrated the basic procedure for creating an object classifier
using the extractHOGfeatures function from the Computer Vision System
Toolbox and the svmclassify and svmtrain functions from the Statistics
Toolbox. Although HOG features and SVM classifiers were used here, other
features and machine learning algorithms can be used in the same way.
For instance, you can explore using different feature types for training the
classifier; or you can see the effect of using other machine learning algorithms
available in the Statistics Toolbox such as k-nearest neighbors.
References
function displayTable(labels)
colHeadings = arrayfun(@(x)sprintf('svm(%d)',x),0:9,'UniformOutput'
format = repmat('%-9s',1,11);
header = sprintf(format,'digit |',colHeadings{:});
fprintf('\n%s\n%s\n',header,repmat('-',size(header)));
for idx = 1:numel(digits)
fprintf('%-9s', [digits(idx) '
|']);
fprintf('%-9d', sum(labels(:,:,idx)));
fprintf('\n')
end
end
end
3-14
3-15
Block
Parameter setting
Edge Detection
3-16
3-17
The Video Viewer1 window displays the edges of the rice grains in white
and the background in black.
3-18
The Video Viewer2 window displays the intensity image of the vertical
gradient components of the image. You can see that the vertical edges of the
rice grains are darker and more well defined than the horizontal edges.
3-19
The Video Viewer3 window displays the intensity image of the horizontal
gradient components of the image. In this image, the horizontal edges of
the rice grains are more well defined.
3-20
The Edge Detection block convolves the input matrix with the Sobel kernel.
This calculates the gradient components of the image that correspond to the
horizontal and vertical edge responses. The block outputs these components
at the Gh and Gv ports, respectively. Then the block performs a thresholding
operation on the gradient components to find the binary image. The binary
image is a matrix filled with 1s and 0s. The nonzero elements of this matrix
correspond to the edge pixels and the zero elements correspond to the
background pixels. The block outputs the binary image at the Edge port.
The matrix values at the Gv and Gh output ports of the Edge Detection block
are double-precision floating-point. These matrix values need to be scaled
between 0 and 1 in order to display them using the Video Viewer blocks. This
is done with the Statistics and Math Operation blocks.
3-21
Run the model faster by double-clicking the Edge Detection block and clear
the Edge thinning check box.
Your model runs faster because the Edge Detection block is more efficient
when you clear the Edge thinning check box. However, the edges of rice
grains in the Video Viewer window are wider.
3-22
3-23
The Video Viewer blocks display the original image, the image with all edges
found, and the image with the longest line annotated.
The Edge Detection block finds the edges in the intensity image. This process
improves the efficiency of the Hough Lines block by reducing the image area
over which the block searches for lines. The block also converts the image to a
binary image, which is the required input for the Hough Transform block.
For additional examples of the techniques used in this section, see the
following list of examples. You can open these examples by typing the title
at the MATLAB command prompt:
Example
MATLAB
Simulink
model-based
Lane Departure
Warning System
videoldws
vipldws
Rotation Correction
videorotationcorrection
viphough
Parameter setting
Hough Transform
3-24
Block
Parameter setting
in the Hough matrix. The block
parameters are set as follows:
Maximum number of local
maxima = 1
Input is Hough matrix
spanning full theta range
Selector
3-25
Block
Parameter setting
Hough Lines
Draw Shapes
Configuration Parameters
Set the configuration parameters. Open the Configuration dialog box by
selecting Model Configuration Parameters from the Simulation menu.
Set the parameters as follows:
Solver pane, Stop time = 0
Solver pane, Type = Fixed-step
Solver pane, Solver = discrete (no continuous states)
Solver pane, Fixed-step size (fundamental sample time): = 0.2
3-26
Here we read in the first two frames of a video sequence. We read them as
intensity images since color is not necessary for the stabilization algorithm,
and because using grayscale images improves speed. Below we show both
frames side by side, and we produce a red-cyan color composite to illustrate
the pixel-wise difference between them. There is obviously a large vertical
and horizontal offset between the two frames.
filename = 'shaky_car.avi';
hVideoSrc = vision.VideoFileReader(filename, 'ImageColorSpace', 'Intensity'
imgA = step(hVideoSrc); % Read first frame into imgA
imgB = step(hVideoSrc); % Read second frame into imgB
figure; imshowpair(imgA, imgB, 'montage');
title(['Frame A', repmat(' ',[1 70]), 'Frame B']);
figure; imshowpair(imgA,imgB,'ColorChannels','red-cyan');
title('Color composite (frame A = red, frame B = cyan)');
Our goal is to determine a transformation that will correct for the distortion
between the two frames. We can use the estimateGeometricTransform
3-27
function for this, which will return an affine transform. As input we must
provide this function with a set of point correspondences between the two
frames. To generate these correspondences, we first collect points of interest
from both frames, then select likely correspondences between them.
In this step we produce these candidate points for each frame. To have the
best chance that these points will have corresponding points in the other
frame, we want points around salient image features such as corners. For
this we use the detectFASTFeatures function, which implements one of the
fastest corner detection algorithms.
The detected points from both frames are shown in the figure below. Observe
how many of them cover the same image features, such as points along the
tree line, the corners of the large road sign, and the corners of the cars.
ptThresh = 0.1;
pointsA = detectFASTFeatures(imgA, 'MinContrast', ptThresh);
pointsB = detectFASTFeatures(imgB, 'MinContrast', ptThresh);
% Display corners found in images A and B.
figure; imshow(imgA); hold on;
plot(pointsA);
title('Corners in A');
figure; imshow(imgB); hold on;
plot(pointsB);
title('Corners in B');
Next we pick correspondences between the points derived above. For each
point, we extract a Fast Retina Keypoint (FREAK) descriptor centered around
it. The matching cost we use between points is the Hamming distance since
FREAK descriptors are binary. Points in frame A and frame B are matched
putatively. Note that there is no uniqueness constraint, so points from frame
B can correspond to multiple points in frame A.
3-28
Match features which were found in the current and the previous frames.
Since the FREAK descriptors are binary, the matchFeatures function uses
the Hamming distance to find the corresponding points.
indexPairs = matchFeatures(featuresA, featuresB);
pointsA = pointsA(indexPairs(:, 1), :);
pointsB = pointsB(indexPairs(:, 2), :);
The image below shows the same color composite given above, but added are
the points from frame A in red, and the points from frame B in green. Yellow
lines are drawn between points to show the correspondences selected by the
above procedure. Many of these correspondences are correct, but there is
also a significant number of outliers.
figure; showMatchedFeatures(imgA, imgB, pointsA, pointsB);
legend('A', 'B');
Many of the point correspondences obtained in the previous step are incorrect.
But we can still derive a robust estimate of the geometric transform between
the two images using the M-estimator SAmple Consensus (MSAC) algorithm,
which is a variant of the RANSAC algorithm. The MSAC algorithm is
implemented in the estimateGeometricTransform function. This function,
when given a set of point correspondences, will search for the valid inlier
correspondences. From these it will then derive the affine transform that
makes the inliers from the first set of points match most closely with the
inliers from the second set. This affine transform will be a 3-by-3 matrix
of the form:
[a_1 a_3 0;
a_2 a_4 0;
t_x t_y 1]
3-29
The parameters define scale, rotation, and sheering effects of the transform,
while the parameters are translation parameters. This transform can be
used to warp the images such that their corresponding features will be moved
to the same image location.
A limitation of the affine transform is that it can only alter the imaging plane.
Thus it is ill-suited to finding the general distortion between two frames taken
of a 3-D scene, such as with this video taken from a moving car. But it does
work under certain conditions that we shall describe shortly.
[tform, pointsBm, pointsAm] = estimateGeometricTransform(...
pointsB, pointsA, 'affine');
imgBp = imwarp(imgB, tform, 'OutputView', imref2d(size(imgB)));
pointsBmp = transformPointsForward(tform, pointsBm.Location);
3-30
Given a set of video frames , we can now use the above procedure to estimate
the distortion between all frames and as affine transforms, . Thus the
cumulative distortion of a frame relative to the first frame will be the product
of all the preceding inter-frame transforms, or
We could use all the six parameters of the affine transform above, but, for
numerical simplicity and stability, we choose to re-fit the matrix as a simpler
scale-rotation-translation transform. This has only four free parameters
compared to the full affine transforms six: one scale factor, one angle, and
two translations. This new transform matrix is of the form:
[s*cos(ang)
s*sin(ang)
t_x
s*-sin(ang)
s*cos(ang)
t_y
0;
0;
1]
3-31
figure(2), clf;
imshowpair(imgBold,imgBsRt,'ColorChannels','red-cyan'), axis image;
title('Color composite of affine and s-R-t transform outputs');
Now we apply the above steps to smooth a video sequence. For readability,
the above procedure of estimating the transform between two images has
been placed in the MATLAB function cvexEstStabilizationTform. The
function cvexTformToSRT also converts a general affine transform into a
scale-rotation-translation transform.
At each step we calculate the transform between the present frames. We fit
this as an s-R-t transform, . Then we combine this the cumulative transform,
, which describes all camera motion since the first frame. The last two frames
of the smoothed video are shown in a Video Player as a red-cyan composite.
With this code, you can also take out the early exit condition to make the
loop process the entire video.
% Reset the video source to the beginning of the file.
reset(hVideoSrc);
hVPlayer = vision.VideoPlayer; % Create video viewer
% Process all frames in the video
movMean = step(hVideoSrc);
imgB = movMean;
imgBp = imgB;
correctedMean = imgBp;
ii = 2;
Hcumulative = eye(3);
while ~isDone(hVideoSrc) && ii < 10
% Read in new frame
imgA = imgB; % z^-1
imgAp = imgBp; % z^-1
imgB = step(hVideoSrc);
movMean = movMean + imgB;
3-32
During computation, we computed the mean of the raw video frames and of
the corrected frames. These mean values are shown side-by-side below. The
left image shows the mean of the raw input frames, proving that there was
a great deal of distortion in the original video. The mean of the corrected
frames on the right, however, shows the image core with almost no distortion.
While foreground details have been blurred (as a necessary result of the cars
forward motion), this shows the efficacy of the stabilization algorithm.
figure; imshowpair(movMean, correctedMean, 'montage');
title(['Raw input mean', repmat(' ',[1 50]), 'Corrected sequence mean']);
References
[1] Tordoff, B; Murray, DW. "Guided sampling and consensus for motion
estimation." European Conference n Computer Vision, 2002.
3-33
[2] Lee, KY; Chuang, YY; Chen, BY; Ouhyoung, M. "Video Stabilization using
Robust Feature Trajectories." National Taiwan University, 2009.
[3] Litvin, A; Konrad, J; Karl, WC. "Probabilistic video stabilization using
Kalman filtering and mosaicking." IS&T/SPIE Symposium on Electronic
Imaging, Image and Video Communications and Proc., 2003.
[4] Matsushita, Y; Ofek, E; Tang, X; Shum, HY. "Full-frame Video
Stabilization." Microsoft Research Asia. CVPR 2005.
Video Stabilization
This example shows how to remove the effect of camera motion from a video
stream.
Introduction
In this example we first define the target to track. In this case, it is the back
of a car and the license plate. We also establish a dynamic search region,
whose position is determined by the last known target location. We then
search for the target only within this search region, which reduces the number
of computations required to find the target. In each subsequent video frame,
we determine how much the target has moved relative to the previous frame.
We use this information to remove unwanted translational camera motions
and generate a stabilized video.
Initialization
Create a System object to read video from a multimedia file. We set the
output to be of intensity only video.
% Input video file which needs to be stabilized.
filename = 'shaky_car.avi';
hVideoSource = vision.VideoFileReader(filename, ...
'ImageColorSpace', 'Intensity',...
'VideoOutputDataType', 'double');
3-34
Create a template matcher System object to compute the location of the best
match of the target in the video frame. We use this location to find translation
between successive video frames.
hTM = vision.TemplateMatcher('ROIInputPort', true, ...
'BestMatchNeighborhoodOutputPort', true);
Create a System object to display the original video and the stabilized video.
hVideoOut = vision.VideoPlayer('Name', 'Video Stabilization');
hVideoOut.Position(1) = round(0.4*hVideoOut.Position(1));
hVideoOut.Position(2) = round(1.5*(hVideoOut.Position(2)));
hVideoOut.Position(3:4) = [650 350];
3-35
This is the main processing loop which uses the objects we instantiated above
to stabilize the input video.
while ~isDone(hVideoSource)
input = step(hVideoSource);
% Find location of Target in the input video frame
if firstTime
Idx = int32(pos.template_center_pos);
MotionVector = [0 0];
firstTime = false;
else
IdxPrev = Idx;
ROI = [SearchRegion, pos.template_size+2*pos.search_border];
Idx = step(hTM, input, Target, ROI);
MotionVector = double(Idx-IdxPrev);
end
[Offset, SearchRegion] = updatesearch(sz, MotionVector, ...
SearchRegion, Offset, pos);
% Translate video frame to offset the camera motion
Stabilized = step(hTranslate, input, fliplr(Offset));
Target = Stabilized(TargetRowIndices, TargetColIndices);
% Add black border for display
Stabilized(:, BorderCols) = 0;
Stabilized(BorderRows, :) = 0;
3-36
Release
Here you call the release method on the objects to close any open files and
devices.
release(hVideoSource);
Conclusion
following table.
3-37
Block
Library
Quantity
Color Space
Conversion
Edge Detection
Hough Transform
Hough Lines
Find Local
Maxima
Draw Shapes
Video Viewer
Submatrix
Selector
MATLAB
Function
Terminator
Display
2 Use the Image From File block to import an image into the Simulink model.
3-38
YCbCr color space. You perform this conversion to separate the luma
information from the color information. Accept the default parameters.
Note In this example, you segment the image using a thresholding
operation that performs best on the Cb channel of the YCbCr color space.
4 Use the Selector and Selector1 blocks to separate the Y (luminance) and
to a particular region of interest (ROI). This ROI contains two beams that
are at an angle to each other. Set the parameters as follows:
Starting row = Index
3-39
image. This block outputs a binary image. Set the Threshold scale
factor parameter to 1.
7 Use the Hough Transform block to calculate the Hough matrix, which gives
matrix. These values represent potential lines in the input image. Set
the parameters as follows:
Neighborhood size = [11 11]
Select the Input is Hough matrix spanning full theta range
checkbox.
Uncheck the Output variable size signal checkbox.
Because you are expecting two lines, leave the Maximum number of
local maxima (N) parameter set to 2.
9 Use the Submatrix2 block to find the indices that correspond to the theta
values of the two peak values in the Hough matrix. Set the parameters as
follows:
Starting row = Index
3-40
values of the two peak values in the Hough matrix. Set the parameters as
follows:
Ending row = Index
Ending row index = 1
The Idx port of the Find Local Maxima block outputs a matrix whose first
row represents the One-based indices of the rho values that correspond to
the peaks in the Hough matrix. Now that you have these indices, you can
use a Selector block to extract the corresponding rho values from the vector
output of the Hough Transform block.
11 Use the Selector2 and Selector3 blocks to find the theta and rho values
that correspond to the peaks in the Hough matrix. These values, output
by the Hough Transform block, are located at the indices output by the
Submatrix2 and Submatrix3 blocks. Set both block parameters as follows:
Index mode = One-based
1 Index Option = Index vector (port)
Input port size = -1
You set the Index mode to One-based because the Find Local Maxima
block outputs One-based indices at the Idx port.
12 Use the Hough Lines block to find the Cartesian coordinates of lines that
are described by rho and theta pairs. Set the Sine value computation
method parameter to Trigonometric function.
3-41
13 Use the Draw Shapes block to draw the lines on the luminance portion of
lines. Copy and paste the following code into the block:
function angle = compute_angle(theta)
% Compute the angle value in degrees
angle = abs(theta(1)-theta(2))*180/pi;
% Always return an angle value less than 90 degrees
if (angle>90)
angle = 180-angle;
end
15 Use the Display block to view the angle between the two lines. Accept the
default parameters.
16 Use the Video Viewer blocks to view the original image, the ROI, and the
3-42
3-43
The Video Viewer1 window displays the ROI where two beams intersect.
3-44
The Video Viewer2 window displays the ROI that has been annotated with
two white lines.
3-45
The Display block shows a value of 58, which is the angle in degrees
between the two lines on the annotated ROI.
You have now annotated an image with two lines and measured the angle
between them. For additional information, see the Hough Transform, Find
Local Maxima, Hough Lines, and Draw Shapes block reference pages.
3-46
Read in two color images of the same scene, which were taken from different
positions. Then, convert them to grayscale. Colors are not required for the
matching process.
I1 = rgb2gray(imread('yellowstone_left.png'));
I2 = rgb2gray(imread('yellowstone_right.png'));
3-47
Visualize the location and scale of the thirty strongest SURF features in I1
and I2. Notice that not all of the detected features can be matched because
they were either not detected in both images or because some of them were
not present in one of the images due to camera motion.
figure; imshow(I1); hold on;
plot(blobs1.selectStrongest(30));
title('Thirty strongest SURF features in I1');
figure; imshow(I2); hold on;
plot(blobs2.selectStrongest(30));
title('Thirty strongest SURF features in I2');
3-48
matchedPoints1 = validBlobs1(indexPairs(:,1),:);
matchedPoints2 = validBlobs2(indexPairs(:,2),:);
Show matching points on top of the composite image, which combines stereo
images. Notice that most of the matches are correct, but there are still some
outliers.
The correctly matched points must satisfy epipolar constraints. This means
that a point must lie on the epipolar line determined by its corresponding
point. You will use the estimateFundamentalMatrix function to compute the
fundamental matrix and find the inliers that meet the epipolar constraint.
[fMatrix, epipolarInliers, status] = estimateFundamentalMatrix(...
matchedPoints1, matchedPoints2, 'Method', 'RANSAC', ...
'NumTrials', 10000, 'DistanceThreshold', 0.1, 'Confidence', 99.99);
if status ~= 0 || isEpipoleInImage(fMatrix, size(I1)) ...
|| isEpipoleInImage(fMatrix', size(I2))
error(['Either not enough matching points were found or '...
'the epipoles are inside the images. You may need to '...
'inspect and improve the quality of detected features ',...
'and/or improve the quality of your images.']);
end
inlierPoints1 = matchedPoints1(epipolarInliers, :);
inlierPoints2 = matchedPoints2(epipolarInliers, :);
figure; showMatchedFeatures(I1, I2, inlierPoints1, inlierPoints2);
legend('Inlier points in I1', 'Inlier points in I2');
3-49
Rectify the images using projective transformations, tform1 and tform2. Show
a color composite of the rectified images demonstrating point correspondences.
I1Rect = imwarp(I1, tform1, 'OutputView', imref2d(size(I1)));
I2Rect = imwarp(I2, tform2, 'OutputView', imref2d(size(I2)));
% transform the points to visualize them together with the rectified images
pts1Rect = transformPointsForward(tform1, inlierPoints1.Location);
pts2Rect = transformPointsForward(tform2, inlierPoints2.Location);
figure; showMatchedFeatures(I1Rect, I2Rect, pts1Rect, pts2Rect);
legend('Inlier points in rectified I1', 'Inlier points in rectified I2');
Crop the overlapping area of the rectified images. You can use red-cyan stereo
glasses to see the 3D effect.
Irectified = cvexTransformImagePair(I1, tform1, I2, tform2);
figure, imshow(Irectified);
title('Rectified Stereo Images (Red - Left Image, Cyan - Right Image)');
The parameters used in the above steps have been set to fit the two
particular stereo images. To process other images, you can use the
cvexRectifyStereoImages function, which contains additional logic to
automatically adjust the rectification parameters. The image below shows the
result of processing a pair of images using this function.
3-50
cvexRectifyImages('parkinglot_left.png', 'parkinglot_right.png');
References
3-51
Calibration Workflow
3-52
1 Load images.
2 Calibrate the images.
3 Evaluate results.
4 Make desired improvements, and recalibrate.
5 Export the parameters to an XML file.
In some cases, the default values work well, and you do not need to make
any improvements before exporting parameters. The foundation suite of
Camera Calibrator MATLAB functions on page 3-74 used by the calibrator,
provide the workflow for calibration and can be used directly in the MATLAB
workspace.
3-53
3-54
Do not change zoom settings between images, otherwise the focal length
changes.
Capture Images
For best results, use between 10 and 20 images. The calibrator requires at
least three images. For greater calibration accuracy:
Take the images of the pattern at a distance roughly equal to the distance
from your camera to the objects of interest. For example, if you plan to
measure objects of interest at a distance of 2 meters, keep your pattern
approximately 2 meters from the camera.
Place the checkerboard at an angle less than 45 degrees relative to the
camera plane.
Do not modify the images. For example, do not crop them.
Do not use auto-focus or change the zoom between images.
Capture the images of a checkerboard pattern at different orientations
relative to the camera.
Capture enough different images of the pattern so that you have covered
as much of the image frame as possible. Lens distortion increases radially
from the center of the image and sometimes is not uniform across the
image frame. To capture this lens distortion, the pattern must appear
close to the edges.
3-55
3-56
3-57
3-58
3-59
Load Images
To begin calibration, load the images of the checkerboard by clicking the Add
images icon. You can add images from multiple folders by clicking the Add
images button for each folder. After you select the images, a prompt appears
requesting the size of the checkerboard square. Enter the length of one side of
a square from the checkerboard pattern.
If any of the images are rejected, the Detection Results window appears,
which contains diagnostic information. The results indicate how many total
images were processed, how many were accepted, how many were rejected,
and, if any, how many duplicate images were skipped.
3-60
To view the rejected images, click the view images button. The calibrator
rejects duplicate images. It also rejects images where the entire checkerboard
could not be detected. Possible reasons for no detection are a blurry image or
an extreme angle of the pattern. Detection takes longer with larger images or
with patterns containing a greater number of squares.
Detected Points
A list of images with IDs appear in the Data Browser window. The images
listed, are the ones that contain a detected pattern. Select the image from the
list in the Data Browser to view it in the Image pane.
3-61
3-62
The Image pane displays the checkerboard image with green circles indicating
detected points. You can verify the corners were detected correctly using the
zoom controls on the View tab. The square indicates the (0,0) origin, and the
x and y arrows indicate the checkerboard axes orientation.
Calibrate
Once you are satisfied with the accepted images, click the Calibrate button
in the toolbar.
x
w y X
1
R
Z 1 K
t
3-63
1 Solve for the intrinsics and extrinsics in closed form, assuming that lens
distortion is zero.[1]
2 Estimate all parameters simultaneously including the distortion coefficient
3-64
Reprojection Errors
The reprojection errors are the distances in pixels between the detected and
the reprojected points. The camera calibrator calculates reprojection errors
by projecting the checkerboard points from world coordinates (defined by the
checkerboard), into image coordinates. These reprojected points are compared
to the corresponding detected points. As a rule of thumb, reprojection errors
should be less than one pixel.
The camera calibrator displays the reprojection errors as a bar graph and as
a scatter plot. Both of these visuals are expressed in pixels. You can toggle
between them using the button on the display. You can identify the images
that adversely contribute to the calibration from either one of the graphs. You
can then select and remove those images from the list in the Data Browser
window.
3-65
The bar graph displays the mean reprojection error per image along with
the overall mean error. The bar labels correspond to the image IDs. The
highlighted bar corresponds to the selected image.
The scatter plot displays the reprojection errors for each point. The plus
markers correspond to the points in the selected image. A good calibration
typically results in a compact cloud of points. Outliers indicate potential
issues with the corresponding images. To improve accuracy, consider
removing those images.
3-66
3-67
3-68
3-69
Improve Calibration
To improve the calibration, you can remove high-error images, add more
images, or modify the calibrator settings.
Add and Delete Images
You can improve calibration by adding more images. Consider adding more
images if:
You have less than 10 images.
The patterns do not cover enough of the image frame.
The patterns in your images do not have enough variation in orientation
with respect to the camera.
You can also improve calibration by removing images that:
Have a high mean reprojection error
Are blurry
Contain a checkerboard at an angle greater than 45 degrees relative to
the camera plane
Contain incorrectly detected checkerboard points
Change the Number of Radial Distortion Coefficients
You can specify 2 or 3 radial distortion coefficients by selecting the
corresponding radio button. Radial distortion occurs when light rays bend a
greater amount near the edges of a lens than they do at its optical center. The
smaller the lens, the greater the distortion.
3-70
The radial distortion coefficients model this type of distortion. The distorted
points are denoted as (xdistorted, ydistorted).
xdistorted = x(1 + k1*r2 + k2*r4 + k3*r6)
ydistorted= y(1 + k1*r2 + k2*r4 + k3*r6)
where
x, y: undistorted pixel locations
k1, k2, and k3: radial distortion coefficients of the lens
r 2 : x2 + y2
Typically, two coefficients are sufficient for calibration. For severe distortion,
as in the case of a wide-angle lens, you can include k3. The undistorted pixel
locations are in normalized image coordinates, with the origin at the optical
center. The coordinates are expressed in world units.
Compute Skew
When you select the Compute Skew check box, the calibrator estimates
the image axes skew. Some camera sensors contain imperfections which
cause the x and y axis to not be perpendicular, which results from pixels
3-71
not being rectangular. This defect can be modeled using a skew parameter.
If you do not select the check box, the image axes are assumed to be exactly
perpendicular, which is the case for most modern cameras.
Compute Tangential Distortion
Tangential distortion occurs when the lens and the image plane are not
parallel. The tangential distortion coefficients model this type of distortion.
3-72
File Management
New Session
You can use the new session feature to start a new calibration session, such
as when you want to clear your session to calibrate another camera. Click the
New Session button to name and save, or cancel the current session.
Open Session
Use the open session feature to browse and open a previously saved
calibration session.
3-73
Save Session
Use the save session feature to save the current calibration session.
Add Images
To begin calibration, load the images of the checkerboard by clicking the
Add images button. You can add images from multiple folders by clicking
the Add images button for each folder. See the Load Images on page 3-60
section for details.
References
[1] Zhang, Z. A flexible new technique for camera calibration. IEEE
Transactions on Pattern Analysis and Machine Intelligence, 22(11):13301334.
3-74
See Also
Related
Examples
External
Web Sites
3-75
3-76
To calibrate the stereo system, you must take multiple pairs of images of a
calibration pattern from different angles. The entire pattern must be visible
in each image. You should store the images in a format that does not use lossy
compression, such as PNG. Formats that do use lossy compression, such as
JPEG, cause image artifacts, which reduce calibration accuracy.
A typical calibration pattern is an asymmetric checkerboard. One side should
contain an even number of squares, and the other side should contain an odd
number of squares. This property allows the detectCheckerboardPoints
function to determine the orientation of the checkerboard unambiguously.
The calibration pattern must be affixed to a flat surface. You should place the
pattern at approximately the same distance from the camera as the objects
you want to measure. You must measure the size of a square in world units,
for example millimeters, as precisely as possible.
Please see Find Camera Parameters with the Camera Calibrator for more
information on how to take calibration images.
Specify Calibration Images
Create two cell arrays containing file names of calibration images taken with
each camera.
numImagePairs = 10;
imageFiles1 = cell(numImagePairs, 1);
imageFiles2 = cell(numImagePairs, 1);
imageDir = fullfile(matlabroot, 'toolbox', 'vision', 'visiondemos', ...
'calibration', 'stereoWebcams');
for i = 1:numImagePairs
imageFiles1{i} = fullfile(imageDir, sprintf('left%02d.png', i));
imageFiles2{i} = fullfile(imageDir, sprintf('right%02d.png', i));
end
Try to Detect Checkerboards
3-77
Mask out the wall on the right. You can determine the coordinates of the
region containing the problematic texture using imtool or imrect.
images1 = cast([], 'uint8');
images2 = cast([], 'uint8');
for i = 1:numel(imageFiles1)
im = imread(imageFiles1{i});
im(3:700, 1247:end, :) = 0;
images1(:, :, :, i) = im;
im = imread(imageFiles2{i});
im(1:700, 1198:end, :) = 0;
images2(:, :, :, i) = im;
end
Detect Checkerboards in the Modified Images
Detect the checkerboard in all stereo pairs of images. You must pass in the
images as two sets, one from each camera. The images in both sets must be in
the same order to form stereo pairs. The detectCheckerboardPoints function
returns imagePoints, a 4-D array, such that imagePoints(:,:,:,1) are
points from camera 1, and imagePoints(:,:,:,2) are points from camera 2.
[imagePoints, boardSize] = detectCheckerboardPoints(images1, images2);
% Display one masked image with the correctly detected checkerboard
3-78
3-79
Plot the 3-D points by calling the plot3 function. We have to call the function
in a loop for each color, because it can only plot points of a single color at a
time. To minimize the number of iteration of the loop, reduce the number of
colors in the image by calling rgb2ind.
% Reduce the number of colors in the image to 128.
[reducedColorImage, reducedColorMap] = rgb2ind(J1, 128);
3-80
= 1:size(reducedColorMap, 1)
Find the pixels of the current color.
= X(reducedColorImage == i-1);
= Y(reducedColorImage == i-1);
= Z(reducedColorImage == i-1);
if isempty(x)
continue;
end
% Eliminate invalid values.
idx = isfinite(x);
x = x(idx);
y = y(idx);
z = z(idx);
% Plot points between 3 and 7 meters away from the camera.
maxZ = 7;
minZ = 3;
x = x(z > minZ & z < maxZ);
y = y(z > minZ & z < maxZ);
z = z(z > minZ & z < maxZ);
plot3(hAxes, x, y, z, '.', 'MarkerEdgeColor', reducedColorMap(i, :));
hold on;
end
% Set up the view.
grid on;
cameratoolbar show;
axis vis3d
3-81
axis equal;
set(hAxes,'YDir','reverse', 'ZDir', 'reverse');
camorbit(-20, 25, 'camera', [0 1 0]);
Summary
This example showed how to calibrate a stereo camera system and then use it
to reconstruct a 3-D scene.
References
3-82
This example shows how to reconstruct a 3-D scene from multiple 2-D images.
This type of reconstruction can be used to develop 3-D models of objects or
build 3-D world maps. The steps of a reconstruction process are shown and
explained using a pair of images. The last step shows the same process
using additional views of the object. This example uses a collection of images
captured with a standard digital camera. The camera was calibrated using
the cameraCalibrator
Read a Pair of Images
Load images into the workspace and save them to a structure. This structure
will then be used to store other information associated with each of the images.
files = {'Globe01.jpg';'Globe02.jpg'};
for i = 1:numel(files)
files{i}=fullfile(matlabroot, 'toolbox', 'vision', 'visiondemos', ...
'sparseReconstructionImages', files{i});
images(i).image = imread(files{i});
end
figure;
montage(files); title('Pair of Original Images')
3-83
K = cameraParams.IntrinsicMatrix;
Remove Lens Distortion
Lens distortion can affect the accuracy of the final reconstruction. For this
reason, the distortion is removed from each of the images using calibration
information. This process straightens the lines that are bent by the radial
distortion of the lens.
figure
distortedImage = images(1).image;
title('Globe01 Before and After Image Distortion is Removed')
for i=1:numel(images) % remove lens distortion from both images
images(i).image = undistortImage(images(i).image, cameraParams);
end
imshowpair(distortedImage, images(1).image,'montage');
Black regions appear on the edges of the image. The black regions are added
by the undistortImage routine to keep the output size the same as the input
size.
Calculate Extrinsics and Build Camera Matrices for Images
3-84
You need to find a set of points common to both images in order to calculate
their corresponding 3-D locations. Detect and extract point feature
descriptors. In this example, SURF descriptors are used because they are
robust with respect to both scale and rotation.
% Detect feature points and extract SURF descriptors in images
for i = 1:numel(images)
%detect SURF feature points
images(i).points = detectSURFFeatures(rgb2gray(images(i).image),...
'MetricThreshold',600);
%extract SURF descriptors
[images(i).featureVectors,images(i).points] = ...
extractFeatures(rgb2gray(images(i).image),images(i).points);
end
% Visualize several extracted SURF features from the Globe01 image
figure; imshow(images(1).image);
title('1500 Strongest Feature Points from Globe01');
hold on;
plot(images(1).points.selectStrongest(1500));
These features are matched between the images using the matchFeatures
function.
indexPairs = ...
3-85
matchFeatures(images(1).featureVectors, images(2).featureVectors,...
'Prenormalized', true,'MaxRatio',0.4) ;
matchedPoints1 = images(1).points(indexPairs(:, 1));
matchedPoints2 = images(2).points(indexPairs(:, 2));
figure;
% Visualize correspondences
showMatchedFeatures(images(1).image,images(2).image,matchedPoints1,matchedP
title('Original Matched Features from Globe01 and Globe02');
From the previous image, it is clear that not all of the matched features
correspond across image views. Some of these noisy matches can be removed
by utilizing the epipolar constraint. To satisfy the epipolar constraint, a point
from one image must match a point on the corresponding epipolar line in the
other image. If the matched point was not on the epipolar line, then it can
be safely discarded as an outlier. The point falls on the epipolar line when
the epipolar constraint equation is satisfied: where and are corresponding
points in two images in homogeneous coordinates. is the fundamental matrix
relating those images [1].
3-86
3-87
% Compute the 3-D location using the smallest singular value from the
% singular value decomposition of the matrix A
[~,~,V]=svd(A);
X = V(:,end);
X = X/X(end);
% Store location
points3D(i,:) = X';
% Store pixel color for visualization
y = round(pointInImage1(1));
x = round(pointInImage1(2));
color(i,:) = squeeze(doubleimage(x,y,:))';
end
Draw the 3-D Point Cloud
The 3-D points can now be plotted with the same units as the calibration.
Using the scatter3 function, draw a point cloud representation of each feature
location and its corresponding colors.
% add green point representing the origin
points3D(end+1,:) = [0,0,0,1];
color(end+1,:) = [0,1,0];
% show images
figure('units','normalized','outerposition',[0 0 .5 .5])
subplot(1,2,1); montage(files,'Size',[1,2]); title('Original Images')
% plot point-cloud
hAxes = subplot(1,2,2); hold on; grid on;
scatter3(points3D(:,1),points3D(:,2),points3D(:,3),50,color,'fill')
xlabel('x-axis (mm)');ylabel('y-axis (mm)');zlabel('z-axis (mm)')
view(20,24);axis equal;axis vis3d
set(hAxes,'XAxisLocation','top','YAxisLocation','left',...
'ZDir','reverse','Ydir','reverse');
grid on
title('Reconstructed Point Cloud');
3-88
imageFileNames = {'Globe01.jpg';'Globe02.jpg';'Globe03.jpg';'Globe04.jpg';.
'Globe05.jpg';'Globe06.jpg';'Globe07.jpg';'Globe08.jpg'};
load sparseReconstructionCameraParameters.mat % Calibration results
for i =1:numel(imageFileNames)
imageFileNames{i}=fullfile(matlabroot, 'toolbox', 'vision', 'visiondemo
'sparseReconstructionImages', imageFileNames{i});
end
% Perform reconstruction using all of these images
[points3D,color]=helperReconstructFromImages(imageFileNames, cameraParams,
squareSize);
% add origin as green
points3D(end+1,:) = [0,0,0,1];
color(end+1,:) = [0,1,0];
figure('units','normalized','outerposition',[0 0 .5 .5])
% show images
subplot(1,2,1); montage(imageFileNames); title('Original Images')
% show pointcloud
hAxes = subplot(1,2,2); hold on; grid on;
scatter3(points3D(:,1),points3D(:,2),points3D(:,3),50,color,'fill')
3-89
References
3-90
3-91
3-92
4
Object Detection
Label Images for Classification Model Training on page 4-2
Train a Cascade Object Detector on page 4-8
Detecting Cars Using Gaussian Mixture Models on page 4-24
Object Detection
trainingImageLabeler
4-2
4-3
Object Detection
App Controls
You can add an unlimited number of images to the Data Browser. You can
then select, remove, and create ROIs, and save your session. When you are
done, you can export the ROI information to an XML file.
Add Images
Use the Add Images icon to select and add images to the Data Browser.
You can add more images at any time during your editing session. The source
images remain in the folder where they were originally located. The app
does not make copies of the original images or move them. If you rotate the
images in the Data Browser, the app overwrites the images in their original
location with the modified orientation.
The app provides a list of image thumbnails that you loaded for the session.
Next to each thumbnail, you see the file name and number of ROIs created in
that image.
4-4
4-5
Object Detection
New Session
When you start a new session, you can save the current session before
clearing it.
Open Session
You can open a new session to replace or add to the current session. The app
loads the selected .MAT session file. To replace your current session, from
the Open Session options, select Open an existing session. To combine
multiple sessions, select Add session to the current session.
Save Session
You can name and save your session to a .MAT file. The default name is
LabelingSession. The saved session file contains all the required information
to reload the session in the state that you saved it. It contains the paths to the
original images, the coordinates for the ROI bounding boxes for each image,
file names, and logical information to record the state of the files.
Export ROIs
When you click the Export ROIs icon, the app exports the ROI information
to the MATLAB workspace as a 1-by-N structure of N ROIs. The structure
contains two fields for storing the image location and the corresponding ROI
information for each image. You are prompted to name the exported file or to
accept the default ROIBoundingBoxes name. The first field, ImageFileName,
contains the full path and file name of the images. The app does not copy and
resave images, so the stored path refers to the original image and folder that
you loaded the images from. The second field, BoundingBoxes, contains the
ROI [x, y, width, height] information.
4-6
See Also
trainCascadeObjectDetector | trainingImageLabeler |
vision.CascadeObjectDetector | insertObjectAnnotation | imrect
Concepts
External
Web Sites
4-7
Object Detection
4-8
4-9
Object Detection
each stage may have a high false positive rate. Even if it incorrectly labels a
nonobject as positive, the mistake can be corrected by subsequent stages.
The overall false positive rate of the cascade classifier is fs, where f is the
false positive rate per stage in the range (0 1), and s is the number of stages.
Similarly, the overall true positive rate is ts, where t is the true positive rate
per stage in the range (0 1]. Thus, you can see that adding more stages reduces
the overall false-postive rate, but it also reduces the overall true positive rate.
4-10
4-11
Object Detection
Then...
4-12
4-13
Object Detection
are used as negative samples. In this way, each new stage of the cascade is
trained to correct mistakes made by previous stages.
As more stages are added, the detectors overall false positive rate decreases,
causing generation of negative samples to be more difficult. For this reason,
it is helpful to supply as many negative images as possible. To improve
training accuracy, supply negative images that contain backgrounds typically
associated with the objects of interest. Also, include negative images that
contain nonobjects similar in appearance to the objects of interest. For
example, if you are training a stop-sign detector, the negative images should
contain other road signs and shapes.
4-14
requires. Also, increasing the number of stages increases the false negative
rate. This results in a greater chance of rejecting a positive sample by
mistake. You should set the false positive rate (FalseAlarmRate) and the
number of stages, (NumCascadeStages) to yield an acceptable overall false
positive rate. Then, you can tune these two parameters experimentally.
There are cases when the training may terminate early. For example,
training may stop after seven stages, even though you set the number of
stages parameter to 20. This can happen when the function cannot generate
enough negative samples. Note, that if you run the function again and set the
number of stages to seven, you will not get the same result. This is because
the number of positive and negative samples to use for each stage will be
recalculated for the new number of stages.
Troubleshooting
What to do if you run out of positive samples?
The trainCascadeObjectDetector function automatically determines the
number of positive samples to use to train each stage. The number is based on
the total number of positive samples supplied by the user and the values of
the TruePositiveRate and NumCascadeStages parameters.
The number of available positive samples used to train each stage depends
on the true positive rate. The rate specifies what percentage of positive
samples the function may classify as negative. If a sample is classified as a
negative by any stage, it never reaches subsequent stages. For example,
suppose you set the TruePositiveRate to 0.9, and all of the available
samples are used to train the first stage. In this case, 10% of the positive
samples may be rejected as negatives, and only 90% of the total positive
samples are available for training the second stage. If training continues,
then each stage is trained with fewer and fewer samples. Each subsequent
stage must solve an increasingly more difficult classification problem with
fewer positive samples. With each stage getting fewer samples, the later
stages are likely to over-fit the data.
Ideally, you want to use the same number of samples to train each stage. To
do so, the number of positive samples used to train each stage must be less
than the total number of available positive samples. The only exception is
4-15
Object Detection
4-16
approach is to add more negative images. Alternatively, you can try to reduce
the number of stages, or increase the false positive rate.
Examples
Train a Five-Stage Stop-Sign Detector
This example shows how to set up and train a five-stage, stop-sign detector,
using 86 positive samples. The default value for TruePositiveRate is
0.995.
Step 1: Load the positive samples data from a MAT file. File names and
bounding boxes are contained in the array of structures labeled 'data'.
load('stopSigns.mat');
imDir = fullfile(matlabroot,'toolbox','vision','visiondemos','stopSignImage
addpath(imDir);
negativeFolder = fullfile(matlabroot,'toolbox','vision','visiondemos','non_
trainCascadeObjectDetector('stopSignDetector.xml',data,negativeFolder,'Fals
4-17
Object Detection
Notice that all 86 positive samples were used to train each stage. This is
because the true-positive rate is very high relative to the number of positive
samples.
Train a Five-Stage Stop-Sign Detector with a Decreased True-Positive
Rate
This example shows you how to train a stop-sign detector on the same data set
as the first example, (steps 14), but decreased TruePositiveRate to 0.98.
Step 4: Train the detector.
trainCascadeObjectDetector('stopSignDetector_tpr0_98.xml',data,negativeFold
4-18
Notice that only 79 of the total 86 positive samples were used to train each
stage. This is because the true-positive rate was low enough for the function
to start rejecting some of the positive samples as false-negatives.
Train a Ten-stage Stop-Sign Detector
This example shows you how to train a stop-sign detector on the same data
set as the first example, (steps 14), but increased the number of stages to 10.
Step 4: Train the detector.
trainCascadeObjectDetector('stopSignDetector_10stages.xml',data,negativeFol
4-19
4-20
Object Detection
trainCascadeObjectDetector('stopSignDetector_10stages_far0_5.xml',data,nega
4-21
4-22
Object Detection
Notice that now the function was able to train 8 stages before it stopped
because the threshold reached the overall false alarm rate of 0.000587108.
Concepts
External
Web Sites
4-23
Object Detection
Detecting and counting cars can be used to analyze traffic patterns. Detection
is also a first step prior to performing more sophisticated tasks such as
tracking or categorization of vehicles by their type.
This example shows how to use the foreground detector and blob analysis
to detect and count cars in a video sequence. It assumes that the camera
is stationary. The example focuses on detecting objects. To learn more
about tracking objects, see the example titled Motion-Based Multiple Object
Tracking.
Step 1 - Import Video and Initialize Foreground Detector
Rather than immediately processing the entire video, the example starts by
obtaining an initial video frame in which the moving objects are segmented
from the background. This helps to gradually introduce the steps used to
process the video.
The foreground detector requires a certain number of video frames in order to
initialize the Gaussian mixture model. This example uses the first 50 frames
to initialize three Gaussian modes in the mixture model.
foregroundDetector = vision.ForegroundDetector('NumGaussians', 3, ...
'NumTrainingFrames', 50);
videoReader = vision.VideoFileReader('visiontraffic.avi');
for i = 1:150
frame = step(videoReader); % read the next video frame
foreground = step(foregroundDetector, frame);
end
After the training, the detector begins to output more reliable segmentation
results. The two figures below show one of the video frames and the
foreground mask computed by the detector.
4-24
The number of bounding boxes corresponds to the number of cars found in the
video frame. We display the number of found cars in the upper left corner of
the processed video frame.
numCars = size(bbox, 1);
result = insertText(result, [10 10], numCars, 'BoxOpacity', 1, ...
'FontSize', 14);
figure; imshow(result); title('Detected Cars');
4-25
Object Detection
end
release(videoReader); % close the video file
4-26
The output video displays the bounding boxes around the cars. It also displays
the number of cars in the upper left corner of the video.
4-27
4-28
Object Detection
5
Motion Estimation and
Tracking
Object Tracking on page 5-2
Video Mosaicking on page 5-28
Video Mosaicking on page 5-32
Pattern Matching on page 5-35
Pattern Matching on page 5-40
Track an Object Using Correlation on page 5-41
Panorama Creation on page 5-47
Object Tracking
In this section...
Face Detection and Tracking Using the KLT Algorithm on page 5-2
Using Kalman Filter for Object Tracking on page 5-6
Motion-Based Multiple Object Tracking on page 5-16
5-2
Object Tracking
videoFrame
bbox
= step(videoFileReader);
= step(faceDetector, videoFrame);
To track the face over time, this example uses the Kanade-Lucas-Tomasi
(KLT) algorithm. While it is possible to use the cascade object detector on
every frame, it is computationally expensive. It may also fail to detect the face,
when the subject turns or tilts his head. This limitation comes from the type of
trained classification model used for detection. The example detects the face
only once, and then the KLT algorithm tracks the face across the video frames.
Identify Facial Features To Track
The KLT algorithm tracks a set of feature points across the video frames.
Once the detection locates the face, the next step in the example identifies
feature points that can be reliably tracked. This example uses the standard,
"good features to track" proposed by Shi and Tomasi.
% Detect feature points in the face region.
points = detectMinEigenFeatures(rgb2gray(videoFrame), 'ROI', bbox);
% Display the detected points.
figure, imshow(videoFrame), hold on, title('Detected features');
plot(points);
5-3
With the feature points identified, you can now use the vision.PointTracker
System object to track them. For each point in the previous frame, the
point tracker attempts to find the corresponding point in the current frame.
Then the estimateGeometricTransform function is used to estimate the
translation, rotation, and scale between the old points and the new points.
This transformation is applied to the bounding box around the face.
% Create a point tracker and enable the bidirectional error constraint to
% make it more robust in the presence of noise and clutter.
pointTracker = vision.PointTracker('MaxBidirectionalError', 2);
% Initialize the tracker with the initial point locations and the initial
% video frame.
points = points.Location;
initialize(pointTracker, points, videoFrame);
Initialize a Video Player to Display the Results
5-4
Object Tracking
Summary
In this example, you created a simple face tracking system that automatically
detects and tracks a single face. Try changing the input video, and see if you
are still able to detect and track a face. Make sure the person is facing the
camera in the initial frame for the detection step.
5-5
References
Viola, Paul A. and Jones, Michael J. "Rapid Object Detection using a Boosted
Cascade of Simple Features", IEEE CVPR, 2001.
Bruce D. Lucas and Takeo Kanade. An Iterative Image Registration
Technique with an Application to Stereo Vision. International Joint
Conference on Artificial Intelligence, 1981.
Carlo Tomasi and Takeo Kanade. Detection and Tracking of Point Features.
Carnegie Mellon University Technical Report CMU-CS-91-132, 1991.
Jianbo Shi and Carlo Tomasi. Good Features to Track. IEEE Conference on
Computer Vision and Pattern Recognition, 1994.
Zdenek Kalal, Krystian Mikolajczyk and Jiri Matas. Forward-Backward
Error: Automatic Detection of Tracking Failures. International Conference
on Pattern Recognition, 2010
5-6
Object Tracking
Before showing the use of Kalman filter, let us first examine the challenges of
tracking an object in a video. The following video shows a green ball moving
from left to right on the floor.
showDetections();
The white region over the ball highlights the pixels detected using
vision.ForegroundDetector, which separates moving objects from the
background. The background subtraction only finds a portion of the ball
because of the low contrast between the ball and the floor. In other words, the
detection process is not ideal and introduces noise.
To easily visualize the entire object trajectory, we overlay all video frames
onto a single image. The "+" marks indicate the centroids computed using
blob analysis.
showTrajectory();
Using the video which was seen earlier, the trackSingleObject function
shows you how to:
Create vision.KalmanFilter by using configureKalmanFilter
5-7
=
=
=
=
=
[];
[];
[];
'';
[];
%
%
%
%
%
A video frame
The detected location
The tracked location
Label for the ball
Utilities used to process the video
5-8
Object Tracking
param.motionNoise, param.measurementNoise);
isTrackInitialized = true;
trackedLocation = correct(kalmanFilter, detectedLocation);
label = 'Initial';
else
trackedLocation = [];
label = '';
end
else
% Use the Kalman filter to track the ball.
if isObjectDetected % The ball was detected.
% Reduce the measurement noise by calling predict followed by
% correct.
predict(kalmanFilter);
trackedLocation = correct(kalmanFilter, detectedLocation);
label = 'Corrected';
else % The ball was missing.
% Predict the ball's location.
trackedLocation = predict(kalmanFilter);
label = 'Predicted';
end
end
annotateTrackedObject();
end % while
showTrajectory();
end
There are two distinct scenarios that the Kalman filter addresses:
When the ball is detected, the Kalman filter first predicts its state at the
current video frame, and then uses the newly detected object location to
correct its state. This produces a filtered location.
When the ball is missing, the Kalman filter solely relies on its previous
state to predict the balls current location.
5-9
You can see the balls trajectory by overlaying all video frames.
param = getDefaultParameters();
trackSingleObject(param);
5-10
Object Tracking
Notice that the ball emerged in a spot that is quite different from the
predicted location. From the time when the ball was released, it was subject
to constant deceleration due to resistance from the carpet. Therefore, constant
acceleration model was a better choice. If you kept the constant velocity
model, the tracking results would be sub-optimal no matter what you selected
for the other values.
Typically, you would set the InitialLocation input to the location where
the object was first detected. You would also set the InitialEstimateError
vector to large values since the initial state may be very noisy given that it is
derived from a single detection. The following figure demonstrates the effect
of misconfiguring these parameters.
With the misconfigured parameters, it took a few steps before the locations
returned by the Kalman filter align with the actual trajectory of the object.
The values for MeasurementNoise should be selected based on the
detectors accuracy. Set the measurement noise to larger values for a less
accurate detector. The following example illustrates the noisy detections of a
misconfigured segmentation threshold. Increasing the measurement noise
causes the Kalman filter to rely more on its internal state rather than the
incoming measurements, and thus compensates for the detection noise.
param = getDefaultParameters();
param.segmentationThreshold = 0.0005; % smaller value resulting in noisy de
param.measurementNoise
= 12500; % increase the value to compensate
% for the increase in measurement noi
trackSingleObject(param); % visualize the results
5-11
5-12
Object Tracking
Utility functions were used for detecting the objects and displaying the results.
This section illustrates how the example implemented these functions.
Get default parameters for creating Kalman filter and for segmenting the ball.
function param = getDefaultParameters
param.motionModel
= 'ConstantAcceleration';
param.initialLocation
= 'Same as first detection';
param.initialEstimateError = 1E5 * ones(1, 3);
param.motionNoise
= [25, 10, 1];
param.measurementNoise
= 25;
param.segmentationThreshold = 0.05;
end
5-13
end % while
% Close the window which was used to show individual video frame.
uiscopes.close('All');
end
Show trajectory of the ball by overlaying all video frames on top of each other.
5-14
Object Tracking
function showTrajectory
% Close the window which was used to show individual video frame.
uiscopes.close('All');
% Create a figure to show the processing results for all video frames.
figure; imshow(utilities.accumulatedImage/2+0.5); hold on;
plot(utilities.accumulatedDetections(:,1), ...
utilities.accumulatedDetections(:,2), 'k+');
if ~isempty(utilities.accumulatedTrackings)
plot(utilities.accumulatedTrackings(:,1), ...
utilities.accumulatedTrackings(:,2), 'r-o');
legend('Detection', 'Tracking');
end
end
function accumulateResults()
utilities.accumulatedImage
= max(utilities.accumulatedImage, frame);
utilities.accumulatedDetections ...
= [utilities.accumulatedDetections; detectedLocation];
utilities.accumulatedTrackings ...
= [utilities.accumulatedTrackings; trackedLocation];
end
For illustration purposes, select the initial location used by the Kalman filter.
function loc = computeInitialLocation(param, detectedLocation)
if strcmp(param.initialLocation, 'Same as first detection')
loc = detectedLocation;
else
loc = param.initialLocation;
end
end
Create utilities for reading video, detecting moving objects, and displaying
the results.
5-15
5-16
Object Tracking
to predict the tracks location in each frame, and determine the likelihood of
each detection being assigned to each track.
Track maintenance becomes an important aspect of this example. In any
given frame, some detections may be assigned to tracks, while other detections
and tracks may remain unassigned.The assigned tracks are updated using
the corresponding detections. The unassigned tracks are marked invisible.
An unassigned detection begins a new track.
Each track keeps count of the number of consecutive frames, where it
remained unassigned. If the count exceeds a specified threshold, the example
assumes that the object left the field of view and it deletes the track.
This example is a function with the main body at the top and helper routines
in the form of nested functions below.
function multiObjectTracking()
% Create system objects used for reading video, detecting moving objects,
% and displaying the results.
obj = setupSystemObjects();
tracks = initializeTracks(); % Create an empty array of tracks.
nextId = 1; % ID of the next track
% Detect moving objects, and track them across video frames.
while ~isDone(obj.reader)
frame = readFrame();
[centroids, bboxes, mask] = detectObjects(frame);
predictNewLocationsOfTracks();
[assignments, unassignedTracks, unassignedDetections] = ...
detectionToTrackAssignment();
updateAssignedTracks();
updateUnassignedTracks();
deleteLostTracks();
createNewTracks();
displayTrackingResults();
5-17
end
Create System objects used for reading the video frames, detecting foreground
objects, and displaying results.
5-18
Object Tracking
5-19
The detectObjects function returns the centroids and the bounding boxes of
the detected objects. It also returns the binary mask, which has the same size
as the input frame. Pixels with a value of 1 correspond to the foreground, and
pixels with a value of 0 correspond to the background.
The function performs motion segmentation using the foreground detector.
It then performs morphological operations on the resulting binary mask to
remove noisy pixels and to fill the holes in the remaining blobs.
function [centroids, bboxes, mask] = detectObjects(frame)
% Detect foreground.
mask = obj.detector.step(frame);
5-20
Object Tracking
Use the Kalman filter to predict the centroid of each track in the current
frame, and update its bounding box accordingly.
function predictNewLocationsOfTracks()
for i = 1:length(tracks)
bbox = tracks(i).bbox;
% Predict the current location of the track.
predictedCentroid = predict(tracks(i).kalmanFilter);
% Shift the bounding box so that its center is at
% the predicted location.
predictedCentroid = int32(predictedCentroid) - bbox(3:4) / 2;
tracks(i).bbox = [predictedCentroid, bbox(3:4)];
end
end
Assign Detections to Tracks
5-21
The value for the cost of not assigning a detection to a track depends
on the range of values returned by the distance method of the
vision.KalmanFilter. This value must be tuned experimentally. Setting
it too low increases the likelihood of creating a new track, and may result
in track fragmentation. Setting it too high may result in a single track
corresponding to a series of separate moving objects.
The assignDetectionsToTracks function uses the Munkres version of the
Hungarian algorithm to compute an assignment which minimizes the total
cost. It returns an M x 2 matrix containing the corresponding indices of
assigned tracks and detections in its two columns. It also returns the indices
of tracks and detections that remained unassigned.
function [assignments, unassignedTracks, unassignedDetections] = ...
detectionToTrackAssignment()
nTracks = length(tracks);
nDetections = size(centroids, 1);
% Compute the cost of assigning each detection to each track.
cost = zeros(nTracks, nDetections);
for i = 1:nTracks
cost(i, :) = distance(tracks(i).kalmanFilter, centroids);
end
% Solve the assignment problem.
costOfNonAssignment = 20;
[assignments, unassignedTracks, unassignedDetections] = ...
assignDetectionsToTracks(cost, costOfNonAssignment);
end
Update Assigned Tracks
5-22
Object Tracking
5-23
The deleteLostTracks function deletes tracks that have been invisible for
too many consecutive frames. It also deletes recently created tracks that have
been invisible for too many frames overall.
function deleteLostTracks()
if isempty(tracks)
return;
end
invisibleForTooLong = 20;
ageThreshold = 8;
% Compute the fraction of the track's age for which it was visible.
ages = [tracks(:).age];
totalVisibleCounts = [tracks(:).totalVisibleCount];
visibility = totalVisibleCounts ./ ages;
% Find the indices of 'lost' tracks.
lostInds = (ages < ageThreshold & visibility < 0.6) | ...
[tracks(:).consecutiveInvisibleCount] >= invisibleForTooLong;
% Delete lost tracks.
tracks = tracks(~lostInds);
end
Create New Tracks
Create new tracks from unassigned detections. Assume that any unassigned
detection is a start of a new track. In practice, you can use other cues to
eliminate noisy detections, such as size, location, or appearance.
function createNewTracks()
centroids = centroids(unassignedDetections, :);
bboxes = bboxes(unassignedDetections, :);
for i = 1:size(centroids, 1)
centroid = centroids(i,:);
bbox = bboxes(i, :);
5-24
Object Tracking
5-25
reliableTracks = tracks(reliableTrackInds);
% Display the objects. If an object has not been detected
% in this frame, display its predicted bounding box.
if ~isempty(reliableTracks)
% Get bounding boxes.
bboxes = cat(1, reliableTracks.bbox);
% Get ids.
ids = int32([reliableTracks(:).id]);
% Create labels for objects indicating the ones for
% which we display the predicted rather than the actual
% location.
labels = cellstr(int2str(ids'));
predictedTrackInds = ...
[reliableTracks(:).consecutiveInvisibleCount] > 0;
isPredicted = cell(size(labels));
isPredicted(predictedTrackInds) = {' predicted'};
labels = strcat(labels, isPredicted);
% Draw the objects on the frame.
frame = insertObjectAnnotation(frame, 'rectangle', ...
bboxes, labels);
% Draw the objects on the mask.
mask = insertObjectAnnotation(mask, 'rectangle', ...
bboxes, labels);
end
end
% Display the mask and the frame.
obj.maskPlayer.step(mask);
obj.videoPlayer.step(frame);
end
Summary
5-26
Object Tracking
detect and track objects. Try modifying the parameters for the detection,
assignment, and deletion steps.
The tracking in this example was solely based on motion with the assumption
that all objects move in a straight line with constant speed. When the motion
of an object significantly deviates from this model, the example may produce
tracking errors. Notice the mistake in tracking the person labeled #12, when
he is occluded by the tree.
The likelihood of tracking errors can be reduced by using a more complex
motion model, such as constant acceleration, or by using multiple Kalman
filters for every object. Also, you can incorporate other cues for associating
detections over time, such as size, shape, and color.
end
5-27
Video Mosaicking
This example shows how to create a mosaic from a video sequence. Video
mosaicking is the process of stitching video frames together to form a
comprehensive view of the scene. The resulting mosaic image is a compact
representation of the video data, which is often used in video compression
and surveillance applications.
Introduction
5-28
Video Mosaicking
Create two VideoPlayer System objects, one to display the corners of each
frame and other to draw the mosaic.
hVideo1 = vision.VideoPlayer('Name', 'Corners');
hVideo1.Position(1) = hVideo1.Position(1) - 350;
hVideo2 = vision.VideoPlayer('Name', 'Mosaic');
hVideo2.Position(1) = hVideo1.Position(1) + 400;
hVideo2.Position([3 4]) = [750 500];
Create a processing loop to create mosaic from the input video. This loop uses
the System objects you instantiated above.
while ~isDone(hsrc)
% Save the points and features computed from the previous image
pointsPrev
= points;
featuresPrev = features;
% To speed up mosaicking, select and process every 5th image
for i = 1:5
rgb = step(hsrc);
if isDone(hsrc)
break;
end
end
% Convert the image from RGB to intensity.
I = rgb2gray(rgb);
% Detect corners in the image
corners = detectFASTFeatures(I);
5-29
% Check if there are enough corresponding points in the current and the
% previous images
if size(indexPairs, 1) > 2
matchedPoints
= points(indexPairs(:, 1), :);
matchedPointsPrev = pointsPrev(indexPairs(:, 2), :);
5-30
Video Mosaicking
The Corners window displays the input video along with the detected corners
and Mosaic window displays the mosaic created from the input video.
5-31
Video Mosaicking
This example shows how to create a mosaic from a video sequence. Video
mosaicking is the process of stitching video frames together to form a
comprehensive view of the scene. The resulting mosaic image is a compact
representation of the video data. The Video Mosaicking block is often used in
video compression and surveillance applications.
This example illustrates how to use the Corner Detection block, the Estimate
Geometric Transformation block, the Projective Transform block, and the
Compositing block to create a mosaic image from a video sequence.
Example Model
The Input subsystem loads a video sequence from either a file, or generates
a synthetic video sequence. The choice is user defined. First, the Corner
Detection block finds points that are matched between successive frames
by the Corner Matching subsystem. Then the Estimate Geometric
Transformation block computes an accurate estimate of the transformation
matrix. This block uses the RANSAC algorithm to eliminate outlier input
points, reducing error along the seams of the output mosaic image. Finally,
the Mosaicking subsystem overlays the current video frame onto the output
image to generate a mosaic.
Input Subsystem
The Input subsystem can be configured to load a video sequence from a file, or
to generate a synthetic video sequence.
If you choose to use a video sequence from a file, you can reduce computation
time by processing only some of the video frames. This is done by setting the
downsampling rate in the Frame Rate Downsampling subsystem.
5-32
Video Mosaicking
If you choose a synthetic video sequence, you can set the speed of translation
and rotation, output image size and origin, and the level of noise. The output
of the synthetic video sequence generator mimics the images captured by a
perspective camera with arbitrary motion over a planar surface.
Corner Matching Subsystem
The subsystem finds corner features in the current video frame in one of three
methods. The example uses Local intensity comparison (Rosen & Drummond),
which is the fastest method. The other methods available are the Harris
corner detection (Harris & Stephens) and the Minimum Eigenvalue (Shi &
Tomasi).
The Corner Matching Subsystem finds the number of corners, location, and
their metric values. The subsystem then calculates the distances between all
features in the current frame with those in the previous frame. By searching
for the minimum distances, the subsystem finds the best matching features.
Mosaicking Subsystem
The subsystem is reset when the video sequence rewinds or when the
Estimate Geometric Transformation block does not find enough inliers.
Video Mosaicking Using Synthetic Video
The Corners window shows the corner locations in the current video frame.
5-33
The Corners window shows the corner locations in the current video frame.
5-34
Pattern Matching
Pattern Matching
This example shows how to use the 2-D normalized cross-correlation for
pattern matching and target tracking. The example uses predefined or user
specified target and number of similar targets to be tracked. The normalized
cross correlation plot shows that when the value exceeds the set threshold,
the target is identified.
Introduction
In this example you use normalized cross correlation to track a target pattern
in a video. The pattern matching algorithm involves the following steps:
The input video frame and the template are reduced in size to minimize the
amount of computation required by the matching algorithm.
Normalized cross correlation, in the frequency domain, is used to find a
template in the video frame.
The location of the pattern is determined by finding the maximum cross
correlation value.
Initialization
Initialize required variables such as the threshold value for the cross
correlation and the decomposition level for Gaussian Pyramid decomposition.
threshold = single(0.99);
level = 2;
Create three gaussian pyramid System objects for decomposing the target
template and decomposing the Image under Test(IUT). The decomposition is
done so that the cross correlation can be computed over a small region instead
of the entire original size of the image.
5-35
hGaussPymd1 = vision.Pyramid('PyramidLevel',level);
hGaussPymd2 = vision.Pyramid('PyramidLevel',level);
hGaussPymd3 = vision.Pyramid('PyramidLevel',level);
Create two 2-D FFT System objects one for the image under test and the
other for the target.
hFFT2D1 = vision.FFT;
hFFT2D2 = vision.FFT;
Create 2-D convolution System object to average the image energy in tiles
of the same dimension of the target.
hConv2D = vision.Convolver('OutputSize','Valid');
5-36
Pattern Matching
Create a System object to calculate the local maximum value for the
normalized cross correlation.
hFindMax = vision.LocalMaximaFinder( ...
'Threshold', single(-1), ...
'MaximumNumLocalMaxima', numberOfTargets, ...
'NeighborhoodSize', floor(size(target_image_gp)/2)*2 - 1);
sz = get(0,'ScreenSize');
pos = [20 sz(4)-400 400 300];
hROIPattern = vision.VideoPlayer('Name', 'Overlay the ROI on the target', .
'Position', pos);
Initialize figure window for plotting the normalized cross correlation value
5-37
Create a processing loop to perform pattern matching on the input video. This
loop uses the System objects you instantiated above. The loop is stopped when
you reach the end of the input file, which is detected by the VideoFileReader
System object.
while ~isDone(hVideoSrc)
Im = step(hVideoSrc);
Im_gp = step(hGaussPymd3, Im);
% Frequency domain convolution.
Im_p(1:ri, 1:ci) = Im_gp;
% Zero-pad
img_fft = step(hFFT2D2, Im_p);
corr_freq = img_fft .* target_fft;
corrOutput_f = step(hIFFFT2D, corr_freq);
corrOutput_f = corrOutput_f(rt:ri, ct:ci);
% Calculate image energies and block run tiles that are size of
% target template.
IUT_energy = (Im_gp).^2;
IUT = step(hConv2D, IUT_energy, C_ones);
IUT = sqrt(IUT);
% Calculate normalized cross correlation.
norm_Corr_f = (corrOutput_f) ./ (IUT * target_energy);
xyLocation = step(hFindMax, norm_Corr_f);
% Calculate linear indices.
linear_index = sub2ind([ri-rt, ci-ct]+1, xyLocation(:,2),...
xyLocation(:,1));
norm_Corr_f_linear = norm_Corr_f(:);
norm_Corr_value = norm_Corr_f_linear(linear_index);
detect = (norm_Corr_value > threshold);
target_roi = zeros(length(detect), 4);
ul_corner = (gain.*(xyLocation(detect, :)-1))+1;
target_roi(detect, :) = [ul_corner, fliplr(target_size(detect, :))];
5-38
Pattern Matching
Summary
This example shows use of Computer Vision System Toolbox to find a user
defined pattern in a video and track it. The algorithm is based on normalized
frequency domain cross correlation between the target and the image under
test. The video player window displays the input video with the identified
target locations. Also a figure displays the normalized correlation between
the target and the image which is used as a metric to match the target. As
can be seen whenever the correlation value exceeds the threshold (indicated
by the blue line), the target is identified in the input video and the location is
marked by the green bounding box.
Appendix
5-39
Pattern Matching
This example shows how to use the 2-D normalized cross-correlation for
pattern matching and target tracking.
Double-click the Edit Parameters block to select the number of similar targets
to detect. You can also change the pyramiding factor. By increasing it, you
can match the target template to each video frame more quickly. Changing
the pyramiding factor might require you to change the Threshold value.
Additionally, you can double-click the Correlation Method switch to specify
the domain in which to perform the cross-correlation. The relative size of the
target to the input video frame and the pyramiding factor determine which
domain computation is faster.
Example Model
The Match metric window shows the variation of the target match metrics.
The model determines that the target template is present in a video frame
when the match metric exceeds a threshold (cyan line).
5-40
following table.
Block
Library
Quantity
2-D Correlation
2-D Maximum
Draw Shapes
Video Viewer
Constant
Mux
5-41
2 Use the Read Binary File block to import a binary file into the model. Set
which is the object you want to track. Set the block parameters as follows:
Main pane, File name = cat_target.png
Data Types pane, Output data type = single
5 Use the 2-D Correlation block to determine the portion of each video frame
that best matches the image of the cat sculpture. Set the block parameters
as follows:
Output size = Valid
Select the Normalized output check box.
Because you chose Valid for the Output size parameter, the block
outputs only those parts of the correlation that are computed without the
zero-padded edges of any input.
6 Use the 2-D Maximum block to find the index of the maximum value in
each input matrix. Set the Mode parameter to Index. This block outputs
the zero-based location of the maximum value as a two-element vector of
32-bit unsigned integers at the Idx port.
7 Use the Data Type Conversion block to change the index values from 32-bit
5-42
9 Use the Mux block to concatenate the location of the maximum value and
the size of the image of the cat sculpture into a single vector. You use this
vector to define a rectangular region of interest (ROI) that you pass to the
Draw Shapes block.
10 Use the Draw Shapes block to draw a rectangle around the portion of each
video frame that best matches the image of the cat sculpture. Accept the
default parameters.
11 Use the Video Viewer block to display the video stream with the ROI
5-43
5-44
As the video plays, you can watch the rectangular ROI follow the sculpture
as it moves.
In this example, you used the 2-D Correlation, 2-D Maximum, and Draw
Shapes blocks to track the motion of an object in a video stream. For more
information about these blocks, see the 2-D Correlation, 2-D Maximum, and
Draw Shapes block reference pages.
5-45
Note This example model does not provide an indication of whether or not
the sculpture is present in each video frame. For an example of this type of
model, type vippattern at the MATLAB command prompt.
5-46
Panorama Creation
Panorama Creation
This example shows how to create a panorama from a video sequence. The
model calculates the motion vector between two adjacent video frames and
uses it to find the portion of each frame that best matches the previous frame.
Then it selects the matching portion and concatenates it with the previous
frame. By repeating this process, it builds a panoramic image out of the
video sequence.
Example Model
This model computes the Sum of Absolute Differences (SAD) using Template
Matching block to estimate the motion between consecutive video frames.
Then it computes the motion vector of a particular block in the current frame
with respect to the previous frame. The model uses this motion vector to align
consecutive frames of the video to form a panoramic picture.
The model takes the video sequence in the Input window and creates a
panorama, which it displays in the Panorama window. Note that this method
of panoramic picture creation assumes there is no zooming or rotational
variation in the video.
5-47
5-48
6
Geometric Transformations
Rotate an Image on page 6-2
Resize an Image on page 6-8
Crop an Image on page 6-12
Interpolation Methods on page 6-17
Video Stabilization on page 6-21
Video Stabilization on page 6-25
Geometric Transformations
Rotate an Image
You can use the Rotate block to rotate your image or video stream by a
specified angle. In this example, you learn how to use the Rotate block to
continuously rotate an image.
Note Running this example requires a DSP System Toolbox license.
ex_vision_rotate_image
1 Define an RGB image in the MATLAB workspace. At the MATLAB
array represents the red, green, or blue color values of the image.
2 To view the image this matrix represents, at the MATLAB command
prompt, type
imshow(I)
6-2
Rotate an Image
3 Create a new Simulink model, and add to it the blocks shown in the
following table.
Block
Library
Quantity
Image From
Workspace
Rotate
Video Viewer
Gain
Display
Counter
4 Use the Image From Workspace block to import the RGB image from the
6-3
Geometric Transformations
Each plane of the array represents the red, green, or blue color values
of the image.
5 Use the Video Viewer block to display the original image. Accept the
default parameters.
The Video Viewer block automatically displays the original image in
the Video Viewer window when you run the model. Because the image
is represented by double-precision floating-point values, a value of 0
corresponds to black and a value of 1 corresponds to white.
6 Use the Rotate block to rotate the image. Set the block parameters as
follows:
Rotation angle source = Input port
Sine value computation method = Trigonometric function
The Angle port appears on the block. You use this port to input a steadily
increasing angle. Setting the Output size parameter to Expanded to fit
rotated input image ensures that the block does not crop the output.
7 Use the Video Viewer1 block to display the rotating image. Accept the
default parameters.
8 Use the Counter block to create a steadily increasing angle. Set the block
parameters as follows:
Count event = Free running
Counter size = 16 bits
Output = Count
Clear the Reset input check box.
Sample time = 1/30
The Counter block counts upward until it reaches the maximum value
that can be represented by 16 bits. Then, it starts again at zero. You can
view its output value on the Display block while the simulation is running.
The Counter blocks Count data type parameter enables you to specify
its output data type.
6-4
Rotate an Image
9 Use the Gain block to convert the output of the Counter block from degrees
6-5
Geometric Transformations
6-6
Rotate an Image
In this example, you used the Rotate block to continuously rotate your image.
For more information about this block, see the Rotate block reference page
in the Computer Vision System Toolbox Reference. For more information
about other geometric transformation blocks, see the Resize and Shear block
reference pages.
Note If you are on a Windows operating system, you can replace the Video
Viewer block with the To Video Display block, which supports code generation.
6-7
Geometric Transformations
Resize an Image
You can use the Resize block to change the size of your image or video stream.
In this example, you learn how to use the Resize block to reduce the size
of an image:
ex_vision_resize_image
1 Create a new Simulink model, and add to it the blocks shown in the
following table.
Block
Library
Quantity
Resize
Video Viewer
2 Use the Image From File block to import the intensity image. Set the File
parameter to 50. This shrinks the image to half its original size.
5 Use the Video Viewer1 block to display the modified image. Accept the
default parameters.
6 Connect the blocks as shown in the following figure.
6-8
Resize an Image
6-9
Geometric Transformations
6-10
Resize an Image
In this example, you used the Resize block to shrink an image. For more
information about this block, see the Resize block reference page. For more
information about other geometric transformation blocks, see the Rotate,
Apply Geometric Transformation, Estimate Geometric Transformation, and
Translate block reference pages.
6-11
Geometric Transformations
Crop an Image
You can use the Selector block to crop your image or video stream. In this
example, you learn how to use the Selector block to trim an image down to a
particular region of interest:
ex_vision_crop_image
1 Create a new Simulink model, and add to it the blocks shown in the
following table.
Block
Library
Quantity
Video Viewer
Selector
2 Use the Image From File block to import the intensity image. Set the File
follows:
Number of input dimensions = 2
1
Index = 140
Output Size = 70
6-12
Crop an Image
Index = 200
Output Size = 70
The Selector block starts at row 140 and column 200 of the image and
outputs the next 70 rows and columns of the image.
5 Use the Video Viewer1 block to display the cropped image. This block
6-13
Geometric Transformations
6-14
Crop an Image
The cropped image appears in the Video Viewer1 window. The following
image is shown at its true size.
6-15
Geometric Transformations
In this example, you used the Selector block to crop an image. For more
information about the Selector block, see the Simulink documentation. For
information about the imcrop function, see the Image Processing Toolbox
documentation.
6-16
Interpolation Methods
Interpolation Methods
In this section...
Nearest Neighbor Interpolation on page 6-17
Bilinear Interpolation on page 6-18
Bicubic Interpolation on page 6-19
1 2 3
4 5 6
7 8 9
represents your input image. You want to translate this image 1.7 pixels in
the positive horizontal direction using nearest neighbor interpolation. The
Translate blocks nearest neighbor interpolation algorithm is illustrated by
the following steps:
1 Zero pad the input matrix and translate it by 1.7 pixels to the right.
Translated zero-padded matrix
1.7 pixels
Original zero-padded matrix
6-17
Geometric Transformations
2 Create the output matrix by replacing each input pixel value with the
0 0 1 2 3
0 0 4 5 6
0 0 7 8 9
Note You wanted to translate the image by 1.7 pixels, but this method
translated the image by 2 pixels. Nearest neighbor interpolation is
computationally efficient but not as accurate as bilinear or bicubic
interpolation.
Bilinear Interpolation
For bilinear interpolation, the block uses the weighted average of two
translated pixel values for each output pixel value.
For example, suppose this matrix,
1 2 3
4 5 6
7 8 9
represents your input image. You want to translate this image 0.5 pixel in
the positive horizontal direction using bilinear interpolation. The Translate
blocks bilinear interpolation algorithm is illustrated by the following steps:
1 Zero pad the input matrix and translate it by 0.5 pixel to the right.
6-18
Interpolation Methods
0.5 pixel
2 Create the output matrix by replacing each input pixel value with the
0 .5 1 .5 2 .5 1 .5
2 4 .5 5 .5 3
3 .5 7 .5 8 .5 4 .5
Bicubic Interpolation
For bicubic interpolation, the block uses the weighted average of four
translated pixel values for each output pixel value.
For example, suppose this matrix,
1 2 3
4 5 6
7 8 9
represents your input image. You want to translate this image 0.5 pixel in
the positive horizontal direction using bicubic interpolation. The Translate
blocks bicubic interpolation algorithm is illustrated by the following steps:
1 Zero pad the input matrix and translate it by 0.5 pixel to the right.
6-19
Geometric Transformations
0.5 pixel
2 Create the output matrix by replacing each input pixel value with the
weighted average of the two translated values on either side. The result is
the following matrix where the output matrix has one more column than
the input matrix:
0.375 1.5
3
1.625
1.875 4.875 6.375 3.125
3.375 8.25 9.75 4.625
6-20
Video Stabilization
Video Stabilization
This example shows how to remove the effect of camera motion from a video
stream.
Introduction
In this example we first define the target to track. In this case, it is the back
of a car and the license plate. We also establish a dynamic search region,
whose position is determined by the last known target location. We then
search for the target only within this search region, which reduces the number
of computations required to find the target. In each subsequent video frame,
we determine how much the target has moved relative to the previous frame.
We use this information to remove unwanted translational camera motions
and generate a stabilized video.
Initialization
Create a System object to read video from a multimedia file. We set the
output to be of intensity only video.
% Input video file which needs to be stabilized.
filename = 'shaky_car.avi';
hVideoSource = vision.VideoFileReader(filename, ...
'ImageColorSpace', 'Intensity',...
'VideoOutputDataType', 'double');
Create a template matcher System object to compute the location of the best
match of the target in the video frame. We use this location to find translation
between successive video frames.
hTM = vision.TemplateMatcher('ROIInputPort', true, ...
'BestMatchNeighborhoodOutputPort', true);
6-21
Geometric Transformations
Create a System object to display the original video and the stabilized video.
hVideoOut = vision.VideoPlayer('Name', 'Video Stabilization');
hVideoOut.Position(1) = round(0.4*hVideoOut.Position(1));
hVideoOut.Position(2) = round(1.5*(hVideoOut.Position(2)));
hVideoOut.Position(3:4) = [650 350];
This is the main processing loop which uses the objects we instantiated above
to stabilize the input video.
while ~isDone(hVideoSource)
input = step(hVideoSource);
% Find location of Target in the input video frame
if firstTime
6-22
Video Stabilization
Idx = int32(pos.template_center_pos);
MotionVector = [0 0];
firstTime = false;
else
IdxPrev = Idx;
ROI = [SearchRegion, pos.template_size+2*pos.search_border];
Idx = step(hTM, input, Target, ROI);
MotionVector = double(Idx-IdxPrev);
end
[Offset, SearchRegion] = updatesearch(sz, MotionVector, ...
SearchRegion, Offset, pos);
% Translate video frame to offset the camera motion
Stabilized = step(hTranslate, input, fliplr(Offset));
Target = Stabilized(TargetRowIndices, TargetColIndices);
% Add black border for display
Stabilized(:, BorderCols) = 0;
Stabilized(BorderRows, :) = 0;
6-23
Geometric Transformations
Release
Here you call the release method on the objects to close any open files and
devices.
release(hVideoSource);
Conclusion
6-24
Video Stabilization
Video Stabilization
This example shows how to remove the effect of camera motion from a video
stream. In the first video frame, the model defines the target to track. In
this case, it is the back of a car and the license plate. It also establishes a
dynamic search region, whose position is determined by the last known target
location. The model only searches for the target within this search region,
which reduces the number of computations required to find the target. In
each subsequent video frame, the model determines how much the target has
moved relative to the previous frame. It uses this information to remove
unwanted translational camera motions and generate a stabilized video.
Example Model
The model uses the Template Matching block to move the target over the
search region and compute the Sum of Absolute Differences (SAD) at each
location. The location with the lowest SAD value corresponds to the location
of the target in the video frame. Based on the location information, the model
computes the displacement vector between the target and its original location.
The Translate block in the Stabilization subsystem uses this vector to shift
each frame so that the camera motion is removed from the video stream.
The model uses the Resize, Compositing, and Insert Text blocks to embed the
enlarged target and its displacement vector on the original video.
6-25
Geometric Transformations
The figure on the left shows the original video. The figure on the right shows
the stabilized video.
6-26
7
Filters, Transforms, and
Enhancements
Histogram Display on page 7-2
Adjust the Contrast of Intensity Images on page 7-3
Adjust the Contrast of Color Images on page 7-8
Remove Salt and Pepper Noise from Images on page 7-13
Sharpen an Image on page 7-19
Histogram Display
This example shows how to use the Histogram block to calculate the
histograms of R, G, and B values in each video frame.
Example Model
7-2
following table.
Block
Library
Quantity
Contrast
Adjustment
Histogram
Equalization
Video Viewer
2 Place the blocks listed in the table above into your new model.
3 Use the Image From File block to import the first image into the Simulink
Accept the default parameters. This block enhances the contrast of images
by transforming the values in an intensity image so that the histogram of
the output image approximately matches a specified histogram.
7-3
7 Use the Video Viewer blocks to view the original and modified images.
7-4
7-5
7-6
In this example, you used the Contrast Adjustment block to linearly scale the
pixel values in pout.tif between new upper and lower limits. You used the
Histogram Equalization block to transform the values in tire.tif so that the
histogram of the output image approximately matches a uniform histogram.
For more information, see the Contrast Adjustment and Histogram
Equalization reference pages.
7-7
following table.
Block
Library
Quantity
Image From
Workspace
Color Space
Conversion
Histogram
Equalization
Video Viewer
Constant
Divide
Product
3 Place the blocks listed in the table above into your new model.
4 Use the Image From Workspace block to import the RGB image from the
MATLAB workspace into the Simulink model. Set the block parameters as
follows:
7-8
Value = shadow
Image signal = Separate color signals
5 Use the Color Space Conversion block to separate the luma information
This block enhances the contrast of images by transforming the luma values
in the color image so that the histogram of the output image approximately
matches a specified histogram. Accept the default parameters.
9 Use the Product block to scale the values back to be between the 0 to 100
each block, set the Image signal parameter to Separate color signals
from the file menu.
12 Connect the blocks as shown in the following figure.
7-9
7-10
As shown in the following figure, the model displays the original image in
the Video Viewer1 window.
7-11
As the next figure shows, the model displays the enhanced contrast image
in the Video Viewer window.
7-12
The model provided with this example already includes this code in
file>Model Properties>Model Properties>InitFcn, and executes it
prior to simulation.
2 To view the image this matrix represents, at the MATLAB command
prompt, type
imshow(I)
7-13
The intensity image contains noise that you want your model to eliminate.
3 Create a Simulink model, and add the blocks shown in the following table.
Block
Library
Quantity
Image From
Workspace
Median Filter
Video Viewer
4 Use the Image From Workspace block to import the noisy image into your
7-14
5 Use the Median Filter block to eliminate the black and white speckles in
7-15
7-16
The original noisy image appears in the Video Viewer window. To view
the image at its true size, right-click the window and select Set Display
To True Size.
The cleaner image appears in the Video Viewer1 window. The following
image is shown at its true size.
7-17
You have used the Median Filter block to remove noise from your image. For
more information about this block, see the Median Filter block reference page
in the Computer Vision System Toolbox Reference.
7-18
Sharpen an Image
Sharpen an Image
To sharpen a color image, you need to make the luma intensity transitions
more acute, while preserving the color information of the image. To do this,
you convert an RGB image into the YCbCr color space and apply a highpass
filter to the luma portion of the image only. Then, you transform the image
back to the RGB color space to view the results. To blur an image, you apply
a lowpass filter to the luma portion of the image. This example shows how
to use the 2-D FIR Filter block to sharpen an image. The prime notation
indicates that the signals are gamma corrected.
ex_vision_sharpen_image
1 Define an RGB image in the MATLAB workspace. To read in an RGB
image from a PNG file and cast it to the double-precision data type, at the
MATLAB command prompt, type
I= im2double(imread('peppers.png'));
The model provided with this example already includes this code in
file>Model Properties>Model Properties>InitFcn, and executes it
prior to simulation.
2 To view the image this array represents, type this command at the
7-19
Now that you have defined your image, you can create your model.
3 Create a new Simulink model, and add to it the blocks shown in the
following table.
7-20
Sharpen an Image
Block
Library
Quantity
Image From
Workspace
Color Space
Conversion
Video Viewer
4 Use the Image From Workspace block to import the RGB image from the
the RGB color space to the YCbCr color space. Set the Image signal
parameter to Separate color signals
6 Use the 2-D FIR Filter block to filter the luma portion of the image. Set the
from the YCbCr color space to the RGB color space. Set the block
parameters as follows:
Conversion = Y'CbCr to R'G'B'
7-21
image in the Video Viewer window when you run the model. Set the Image
signal parameter to Separate color signals, by selecting File > Image
Signal.
9 Connect the blocks as shown in the following figure.
7-22
Sharpen an Image
To blur the image, double-click the 2-D FIR Filter block. Set Coefficients
parameter to fspecial('gaussian',[15 15],7) and then click OK. The
fspecial('gaussian',[15 15],7) command creates two-dimensional
Gaussian lowpass filter coefficients. This lowpass filter blurs the image by
removing the high frequency noise in it.
In this example, you used the Color Space Conversion and 2-D FIR Filter
blocks to sharpen an image. For more information, see the Color Space
Conversion and 2-D FIR Filter, and fspecial reference pages.
7-23
7-24
8
Statistics and
Morphological Operations
Find the Histogram of an Image on page 8-2
Correct Nonuniform Illumination on page 8-8
Count Objects in an Image on page 8-15
following table.
Block
Library
Quantity
Video Viewer
Matrix
Concatenate
Vector Scope
Histogram
2 Use the Image From File block to import an RGB image. Set the block
parameters as follows:
Sample time = inf
Image signal = Separate color signals
Output port labels: = R|G|B
8-2
in the viewer window when you run the model. Set the Image signal
parameter to Separate color signals from the File menu.
4 Use the Histogram blocks to calculate the histogram of the R, G, and B
values in the image. Set the Main tab block parameters for the three
Histogram blocks as follows:
Lower limit of histogram: 0
Upper limit of histogram: 1
Number of bins: = 256
Find the histogram over: = Entire Input
The R, G, and B input values to the Histogram block are double-precision
floating point and range between 0 and 1. The block creates 256 bins
between the maximum and minimum input values and counts the number
of R, G, and B values in each bin.
5 Use the Matrix Concatenate block to concatenate the R, G, and B column
vectors into a single matrix so they can be displayed using the Vector Scope
block. Set the Number of inputs parameter to 3.
6 Use the Vector Scope block to display the histograms of the R, G, and B
8-3
8-4
8-5
The scaled histogram of the image appears in the Vector Scope window.
8-6
You have now used the 2-D Histogram block to calculate the histogram of the
R, G, and B values in an RGB image. To open a model that illustrates how to
use this block to calculate the histogram of the R, G, and B values in an RGB
video stream, type viphistogram at the MATLAB command prompt.
8-7
following table.
Block
Library
Quantity
Opening
Video Viewer
Constant
Sum
2 Use the Image From File block to import the intensity image. Set the File
parameters.
8-8
parameter to 80.
9 Use the Data Type Conversion block to convert the offset value to an 8-bit
unsigned integer. Set the Output data type mode parameter to uint8.
10 Use the second Sum block to lighten the image so that it has the same
default parameters.
12 Connect the blocks as shown in the following figure.
8-9
8-10
8-11
The image without the estimated background appears in the Video Viewer2
window.
8-12
The preceding image is too dark. The Constant block provides an offset
value that you used to brighten the image.
The corrected image, which has even lighting, appears in the Video Viewer3
window. The following image is shown at its true size.
8-13
In this section, you have used the Opening block to remove irregular
illumination from an image. For more information about this block, see
the Opening reference page. For related information, see the Top-hat block
reference page. For more information about STREL objects, see the strel
function in the Image Processing Toolbox documentation.
8-14
following table.
Block
Library
Quantity
Opening
Label
Video Viewer
Constant
Relational Operator
Display
8-15
2 Use the Image From File block to import your image. Set the File name
parameters.
5 Use the Relational Operator block to perform a thresholding operation
that converts your intensity image to a binary image. Set the Relational
Operator parameter to <.
If the input to the Relational Operator block is less than 200, its output is
1; otherwise, its output is 0. You must threshold your intensity image
because the Label block expects binary input. Also, the objects it counts
must be white.
6 Use the Opening block to separate the spokes from the rim and from each
parameters.
8 Use the Label block to count the number of spokes in the input image. Set
8-16
8-17
The original image appears in the Video Viewer1 window. To view the
image at its true size, right-click the window and select Set Display To
True Size.
The opened image appears in the Video Viewer window. The following
image is shown at its true size.
8-18
As you can see in the preceding figure, the spokes are now separate white
objects. In the model, the Display block correctly indicates that there are
24 distinct spokes.
8-19
You have used the Opening and Label blocks to count the number of spokes
in an image. For more information about these blocks, see the Opening and
Label block reference pages in the Computer Vision System Toolbox Reference.
If you want to send the number of spokes to the MATLAB workspace, use the
To Workspace block in Simulink or the Signal to Workspace block in DSP
System Toolbox. For more information about STREL objects, see strel in the
Image Processing Toolbox documentation.
8-20
9
Fixed-Point Design
Fixed-Point Signal Processing on page 9-2
Fixed-Point Concepts and Terminology on page 9-4
Arithmetic Operations on page 9-10
Fixed-Point Support for MATLAB System Objects on page 9-21
Specify Fixed-Point Attributes for Blocks on page 9-25
Fixed-Point Design
Fixed-Point Features
Many of the blocks in this product have fixed-point support, so you can design
signal processing systems that use fixed-point arithmetic. Fixed-point support
in DSP System Toolbox software includes
Signed twos complement and unsigned fixed-point data types
Word lengths from 2 to 128 bits in simulation
Word lengths from 2 to the size of a long on the Simulink Coder C
code-generation target
Overflow handling and rounding methods
C code generation for deployment on a fixed-point embedded processor,
with Simulink Coder code generation software. The generated code uses all
allowed data types supported by the embedded target, and automatically
includes all necessary shift and scaling operations
9-2
9-3
Fixed-Point Design
Note The Glossary defines much of the vocabulary used in these sections.
For more information on these subjects, see the Fixed-Point Designer
documentation.
9-4
where
bi is the ith binary digit.
wl is the word length in bits.
bwl1 is the location of the most significant, or highest, bit (MSB).
b0 is the location of the least significant, or lowest, bit (LSB).
The binary point is shown four places to the left of the LSB. In this
example, therefore, the number is said to have four fractional bits, or a
fraction length of four.
Fixed-point data types can be either signed or unsigned. Signed binary
fixed-point numbers are typically represented in one of three ways:
Sign/magnitude
Ones complement
Twos complement
Twos complement is the most common representation of signed fixed-point
numbers and is used by System Toolbox software. See Twos Complement
on page 9-11 for more information.
Scaling
Fixed-point numbers can be encoded according to the scheme
9-5
Fixed-Point Design
The slope and bias together represent the scaling of the fixed-point number.
In a number with zero bias, only the slope affects the scaling. A fixed-point
number that is only scaled by binary point position is equivalent to a number
in the Fixed-Point Designer [Slope Bias] representation that has a bias equal
to zero and a slope adjustment equal to one. This is referred to as binary
point-only scaling or power-of-two scaling:
Range
The range is the span of numbers that a fixed-point data type and scaling
can represent. The range of representable numbers for a twos complement
fixed-point number of word length wl, scaling S, and bias B is illustrated
below:
9-6
For both signed and unsigned fixed-point numbers of any data type, the
number of different bit patterns is 2wl.
For example, in twos complement, negative numbers must be represented
as well as zero, so the maximum value is 2wl1. Because there is only one
representation for zero, there are an unequal number of positive and negative
numbers. This means there is a representation for -2wl1 but not for 2wl 1:
Precision
The precision of a fixed-point number is the difference between successive
values representable by its data type and scaling, which is equal to the value
9-7
Fixed-Point Design
of its least significant bit. The value of the least significant bit, and therefore
the precision of the number, is determined by the number of fractional bits.
A fixed-point value can be represented to within half of the precision of its
data type and scaling.
For example, a fixed-point representation with four bits to the right of the
binary point has a precision of 2-4 or 0.0625, which is the value of its least
significant bit. Any number within the range of this data type and scaling can
be represented to within (2-4)/2 or 0.03125, which is half the precision. This is
an example of representing a number with finite precision.
Rounding Modes. When you represent numbers with finite precision,
not every number in the available range can be represented exactly. If a
number cannot be represented exactly by the specified data type and scaling,
it is rounded to a representable number. Although precision is always lost
in the rounding operation, the cost of the operation and the amount of bias
that is introduced depends on the rounding mode itself. To provide you with
greater flexibility in the trade-off between cost and bias, DSP System Toolbox
software currently supports the following rounding modes:
Ceiling rounds the result of a calculation to the closest representable
number in the direction of positive infinity.
Convergent rounds the result of a calculation to the closest representable
number. In the case of a tie, Convergent rounds to the nearest even
number. This is the least biased rounding mode provided by the toolbox.
Floor, which is equivalent to truncation, rounds the result of a calculation
to the closest representable number in the direction of negative infinity.
Nearest rounds the result of a calculation to the closest representable
number. In the case of a tie, Nearest rounds to the closest representable
number in the direction of positive infinity.
Round rounds the result of a calculation to the closest representable
number. In the case of a tie, Round rounds positive numbers to the closest
representable number in the direction of positive infinity, and rounds
negative numbers to the closest representable number in the direction
of negative infinity.
Simplest rounds the result of a calculation using the rounding mode
(Floor or Zero) that adds the least amount of extra rounding code to your
9-8
9-9
Fixed-Point Design
Arithmetic Operations
In this section...
Modulo Arithmetic on page 9-10
Twos Complement on page 9-11
Addition and Subtraction on page 9-12
Multiplication on page 9-13
Casts on page 9-16
Note These sections will help you understand what data type and scaling
choices result in overflows or a loss of precision.
Modulo Arithmetic
Binary math is based on modulo arithmetic. Modulo arithmetic uses only
a finite set of numbers, wrapping the results of any calculations that fall
outside the given set back into the set.
9-10
Arithmetic Operations
For example, the common everyday clock uses modulo 12 arithmetic. Numbers
in this system can only be 1 through 12. Therefore, in the clock system, 9
plus 9 equals 6. This can be more easily visualized as a number circle:
Similarly, binary math can only use the numbers 0 and 1, and any arithmetic
results that fall outside this range are wrapped around the circle to either 0
or 1.
Twos Complement
Twos complement is a way to interpret a binary number. In twos
complement, positive numbers always start with a 0 and negative numbers
always start with a 1. If the leading bit of a twos complement number is 0,
the value is obtained by calculating the standard binary value of the number.
If the leading bit of a twos complement number is 1, the value is obtained by
assuming that the leftmost bit is negative, and then calculating the binary
value of the number. For example,
9-11
Fixed-Point Design
01 (0 20 ) 1
11 ((21 ) (20 )) (2 1) 1
To compute the negative of a binary number using twos complement,
1 Take the ones complement, or flip the bits.
2 Add a 1 using binary math.
3 Discard any bits carried beyond the original word length.
For example, consider taking the negative of 11010 (-6). First, take the ones
complement of the number, or flip the bits:
11010 00101
Next, add a 1, wrapping all numbers to 0 or 1:
00101
1
00110 (6)
010010.1
(18.5)
0110.110 (6.75)
011001.010 (25.25)
Fixed-point subtraction is equivalent to adding while using the twos
complement value for any negative values. In subtraction, the addends
9-12
Arithmetic Operations
must be sign extended to match each others length. For example, consider
subtracting 0110.110 (6.75) from 010010.1 (18.5):
Most fixed-point DSP System Toolbox blocks that perform addition cast the
adder inputs to an accumulator data type before performing the addition.
Therefore, no further shifting is necessary during the addition to line up the
binary points. See Casts on page 9-16 for more information.
Multiplication
The multiplication of twos complement fixed-point numbers is directly
analogous to regular decimal multiplication, with the exception that the
intermediate results must be sign extended so that their left sides align
before you add them together.
For example, consider the multiplication of 10.11 (-1.25) with 011 (3):
9-13
Fixed-Point Design
9-14
Arithmetic Operations
9-15
Fixed-Point Design
System Toolbox blocks cast to the accumulator data type before performing
addition or subtraction operations. In the preceding diagram, this is
equivalent to the C code
acc=ac;
acc-=bd;
Casts
Many fixed-point System Toolbox blocks that perform arithmetic operations
allow you to specify the accumulator, intermediate product, and product
output data types, as applicable, as well as the output data type of the block.
This section gives an overview of the casts to these data types, so that you can
tell if the data types you select will invoke sign extension, padding with zeros,
rounding, and/or overflow.
9-16
Arithmetic Operations
Casting Examples
It is important to keep in mind the ramifications of each cast when selecting
these intermediate data types, as well as any other intermediate fixed-point
data types that are allowed by a particular block. Depending upon the data
types you select, overflow and/or rounding might occur. The following two
examples demonstrate cases where overflow and rounding can occur.
9-17
Fixed-Point Design
Cast from a Shorter Data Type to a Longer Data Type. Consider the
cast of a nonzero number, represented by a four-bit data type with two
fractional bits, to an eight-bit data type with seven fractional bits:
As the diagram shows, the source bits are shifted up so that the binary point
matches the destination binary point position. The highest source bit does
not fit, so overflow might occur and the result can saturate or wrap. The
empty bits at the low end of the destination data type are padded with either
0s or 1s:
If overflow does not occur, the empty bits are padded with 0s.
If wrapping occurs, the empty bits are padded with 0s.
If saturation occurs,
You can see that even with a cast from a shorter data type to a longer data
type, overflow might still occur. This can happen when the integer length of
the source data type (in this case two) is longer than the integer length of
9-18
Arithmetic Operations
the destination data type (in this case one). Similarly, rounding might be
necessary even when casting from a shorter data type to a longer data type, if
the destination data type and scaling has fewer fractional bits than the source.
Cast from a Longer Data Type to a Shorter Data Type. Consider the
cast of a nonzero number, represented by an eight-bit data type with seven
fractional bits, to a four-bit data type with two fractional bits:
As the diagram shows, the source bits are shifted down so that the binary
point matches the destination binary point position. There is no value for the
highest bit from the source, so the result is sign extended to fill the integer
portion of the destination data type. The bottom five bits of the source do not
fit into the fraction length of the destination. Therefore, precision can be
lost as the result is rounded.
In this case, even though the cast is from a longer data type to a shorter
data type, all the integer bits are maintained. Conversely, full precision can
be maintained even if you cast to a shorter data type, as long as the fraction
length of the destination data type is the same length or longer than the
9-19
Fixed-Point Design
fraction length of the source data type. In that case, however, bits are lost
from the high end of the result and overflow might occur.
The worst case occurs when both the integer length and the fraction length of
the destination data type are shorter than those of the source data type and
scaling. In that case, both overflow and a loss of precision can occur.
9-20
9-21
Fixed-Point Design
vision.EdgeDetector
vision.FFT
vision.GeometricRotator
vision.GeometricScaler
vision.GeometricTranslator
vision.Histogram
vision.HoughLines
vision.HoughTransform
vision.IDCT
vision.IFFT
vision.ImageDataTypeConverter
vision.ImageFilter
vision.MarkerInserter
vision.Maximum
vision.Mean
vision.Median
vision.MedianFilter
vision.Minimum
vision.OpticalFlow
vision.PSNR
vision.Pyramid
vision.SAD
vision.ShapeInserter
vision.Variance
9-22
the MATLAB desktop, and then select System Objects. Finally, select or
deselect Show fixed-point properties.
9-23
Fixed-Point Design
You change the values of fixed-point properties in the same way as you
change any System object property value. You also use the Fixed-Point
Designer numerictype object to specify the desired data type as fixed-point,
the signedness, and the word- and fraction-lengths.
In the same way as for blocks, the data type properties of many System
objects can set the appropriate word lengths and scalings automatically by
using full precision. System objects assume that the target specified on the
Configuration Parameters Hardware Implementation target is ASIC/FPGA.
If you have not set the property that activates a dependent property
and you attempt to change that dependent property, a warning message
displays. For example, for the vision.EdgeDetector object, before you
set CustomProductDataType to numerictype(1,16,15) you must set
ProductDataType to 'Custom'.
Note System objects do not support fixed-point word lengths greater than
128 bits.
For any System object provided in the Toolbox, the fimath settings for any
fimath attached to a fi input or a fi property are ignored. Outputs from a
System object never have an attached fimath.
9-24
9-25
Fixed-Point Design
All System Toolbox blocks with fixed-point capabilities share a set of common
parameters, but each block can have a different subset of these fixed-point
parameters. The following table provides an overview of the most common
fixed-point block parameters.
Fixed-Point Data
Type Parameter
Description
Rounding Mode
Overflow Mode
9-26
Fixed-Point Data
Type Parameter
Description
Product Output
Accumulator
Output
9-27
Fixed-Point Design
Logging
The Fixed-Point Tool logs overflows, saturations, and simulation minimums
and maximums for fixed-point System Toolbox blocks. The Fixed-Point Tool
does not log overflows and saturations when the Data overflow line in the
Diagnostics > Data Integrity pane of the Configuration Parameters dialog
box is set to None.
9-28
Autoscaling
You can use the Fixed-Point Tool autoscaling feature to set the scaling for
System Toolbox fixed-point data types.
9-29
Fixed-Point Design
9-30
9-31
Fixed-Point Design
9-32
9-33
Fixed-Point Design
ASIC/FPGA. Therefore, the accumulator data type used by the internal rule is
the ideal, full-precision result.
Calculate the full-precision word length for each of the Difference blocks in
the model:
9-34
As you can see in the dialog box, this device has 8-, 16-, and 32-bit word
lengths available. Therefore, the ideal word lengths of 10, 17, and 128 bits
calculated by the internal rule cannot be used. Instead, the internal rule uses
the next largest available word length in each case You can see this if you
rerun the model, as shown in the following figure.
9-35
9-36
Fixed-Point Design
9-37
Fixed-Point Design
9-38
As you can see in the dialog box, this device has 8-, 16-, and 32-bit word
lengths available. Therefore, the ideal word lengths of 12 and 31 bits
calculated by the internal rule cannot be used. Instead, the internal rule uses
the next largest available word length in each case. You can see this if you
rerun the model, as shown in the following figure.
9-39
Fixed-Point Design
9-40
This model uses the Cumulative Sum block to sum the input coming from the
Fixed-Point Sources subsystem. The Fixed-Point Sources subsystem outputs
two signals with different data types:
The Signed source has a word length of 16 bits and a fraction length of
15 bits.
The Unsigned source has a word length of 16 bits and a fraction length of
16 bits.
2 Run the model to check for overflow. MATLAB displays the following
9-41
Fixed-Point Design
open the Fixed-Point Tool by selecting Tools > Fixed-Point > Fixed-Point
Tool from the model menu. Turn on logging for all blocks in your model by
setting the Fixed-point instrumentation mode parameter to Minimums,
maximums and overflows.
4 Now that you have turned on logging, rerun the model by clicking the
Simulation button.
9-42
9-43
Fixed-Point Design
6 Assume a target hardware that supports 32-bit integers, and set the
b Double-click the block in the model, and select the Data Types pane
Assistant button (
9-44
d Set the Mode to Fixed Point. To see the representable range of the
current specified data type, click the Fixed-point details link. The tool
displays the representable maximum and representable minimum values
for the current data type.
e Change the Word length to 32, and click the Refresh details button in
9-45
Fixed-Point Design
When you change the value of the Word length parameter, the data type
string in the Data Type edit box automatically updates.
f Click OK on the block dialog box to save your changes and close the window.
g Set the word length of the Accumulator data type of the Unsigned
button.
3 Examine the results in the Contents pane of the Fixed-Point Tool. Because
you ran the model in Double override mode, you get an accurate, idealized
representation of the simulation minimums and maximums. These values
appear in the SimMin and SimMax parameters.
9-46
minimum and maximum values, you can more easily choose appropriate
fraction lengths. Before making these choices, save your active results to
reference so you can use them as your floating-point benchmark. To do so,
select Results > Move Active Results To Reference from the Fixed-Point
Tool menu. The status displayed in the Run column changes from Active to
Reference for all signals in your model.
check box. You did not specify any design minimums or maximums for the
data types in this model. Thus, the tool uses the logged information to compute
and propose fraction lengths. For information on specifying design minimums
and maximums, see Signal Ranges in the Simulink documentation.
3 Click the Propose fraction lengths button (
9-47
Fixed-Point Design
The proposed fraction length for the Accumulator data type of both the
Signed and Unsigned Cumulative Sum blocks is 17 bits.
To get more details about the proposed scaling for a particular data type,
highlight the data type in the Contents pane of the Fixed-Point Tool.
Open the Autoscale Information window for the highlighted data type
by clicking the Show autoscale information for the selected result
button (
).
2 When the Autoscale Information window opens, check the Value and
for each data type whose proposed scaling you want to keep. Then, click the
Apply accepted fraction lengths button (
). The tool updates the
specified data types on the block dialog boxes and the SpecifiedDT column in
the Contents pane.
4 To verify the newly accepted scaling, set the Data type override parameter
back to Use local settings, and run the model. Looking at Contents pane of
the Fixed-Point Tool, you can see the following details:
The SimMin and SimMax values of the Active run match the SimMin
and SimMax values from the floating-point Reference run.
There are no longer any overflows.
The SimDT does not match the SpecifiedDT for the Accumulator data
type of either Cumulative Sum block. This difference occurs because the
Cumulative Sum block always inherits its Signedness from the input
signal and only allows you to specify a Signedness of Auto. Therefore,
the SpecifiedDT for both Accumulator data types is fixdt([],32,17).
However, because the Signed Cumulative Sum block has a signed input
signal, the SimDT for the Accumulator parameter of that block is also
signed (fixdt(1,32,17)). Similarly, the SimDT for the Accumulator
parameter of the Unsigned Cumulative Sum block inherits its Signedness
from its input signal and thus is unsigned (fixdt(0,32,17)).
9-48
10
Code Generation
Code Generation for Computer Vision Processing in MATLAB on page
10-2
Code Generation Support, Usage Notes, and Limitations on page 10-4
Simulink Shared Library Dependencies on page 10-13
Introduction to Code Generation with Feature Matching and Registration
on page 10-14
Accelerating Simulink Models on page 10-20
10
Code Generation
10-2
10-3
10
Code Generation
10-4
BRISKPoints
cornerPoints
detectBRISKFeatures
detectFASTFeatures
Name
detectHarrisFeatures
detectMinEigenFeatures
detectMSERFeatures
detectSURFFeatures
extractFeatures
extractHOGFeatures
matchFeatures
10-5
10
Code Generation
Name
MSERRegions
SURFPoints
vision.BoundaryTracer
vision.EdgeDetector
10-6
estimateGeometricTransform
vision.GeometricRotator
vision.GeometricScaler
vision.GeometricShearer
vision.GeometricTransformer
Name
vision.GeometricTranslator
ocrText
vision.PeopleDetector
vision.CascadeObjectDetector
vision.ForegroundDetector
vision.HistogramBasedTracker
vision.KalmanFilter
vision.PointTracker
10-7
10
Code Generation
Name
vision.TemplateMatcher
epipolarline
estimateFundamentalMatrix
estimateUncalibratedRectification
isEpipoleInImage
lineToBorderPoints
Statistics
10-8
vision.Autocorrelator
vision.BlobAnalysis
vision.Crosscorrelator
vision.Histogram
vision.LocalMaximaFinder
Name
vision.Maximum
vision.Mean
vision.Median
vision.Minimum
vision.PSNR
vision.StandardDeviation
vision.Variance
Morphological Operations
vision.ConnectedComponentLabeler
vision.MorphologicalClose
vision.MorphologicalDilate
vision.MorphologicalErode
vision.MorphologicalOpen
vision.Convolver
vision.ContrastAdjuster
10-9
10
Code Generation
Name
vision.DCT
vision.Deinterlacer
vision.EdgeDetector
vision.FFT
vision.HistogramEqualizer
vision.HoughLines
vision.HoughTransform
vision.IDCT
vision.IFFT
vision.ImageFilter
vision.MedianFilter
vision.Pyramid
10-10
Name
vision.VideoFileReader
vision.VideoFileWriter
vision.Autothresholder
vision.ChromaResampler
vision.ColorSpaceConverter
vision.DemosaicInterpolator
vision.GammaCorrector
vision.ImageComplementer
vision.ImageDataTypeConverter
vision.ImagePadder
Graphics
insertMarker
10-11
10
10-12
Code Generation
Name
insertShape
vision.AlphaBlender
vision.MarkerInserter
vision.ShapeInserter
vision.TextInserter
Simulink Coder provides functions to help you set up and manage the build
information for your models. For example, one of the Build Information
functions that Simulink Coder provides is getNonBuildFiles. This function
allows you to identify the shared libraries required by blocks in your model. If
your model contains any blocks that use precompiled shared libraries, you can
install those libraries on the target system. The folder that you install the
shared libraries in must be on the system path. The target system does not
need to have MATLAB installed, but it does need to be supported by MATLAB.
10-13
10
Code Generation
To run this example, you must have access to a C compiler and you must
configure it using mex -setup command. For more information, see Setting
Up Your C Compiler.
Decide Whether to Run under MATLAB or as a Standalone Application
Generated code can run inside the MATLAB environment as a C-MEX file,
or outside the MATLAB environment as a standalone executable or shared
utility to be linked with another standalone executable. For more details
about setting code generation options, see the config option of the codegen
command.
MEX Executables
This example generates a MEX executable to be run inside the MATLAB
environment.
Generating a C-MEX executable to run inside of MATLAB can also be a great
first step in a workflow that ultimately leads to standalone code. The inputs
and the outputs of the MEX-file are available for inspection in the MATLAB
environment, where visualization and other kinds of tools for verification and
analysis are readily available. You also have the choice of running individual
10-14
and pass that object to the coder command on the MATLAB command line.
When you create a standalone executable, you have to write your own main.c
(or main.cpp). Note that when you create a standalone executable, there
are no ready-made utilities for importing or exporting data between the
executable and the MATLAB environment. One of the options is to use
printf/fprintf to a file (in your handwritten main.c) and then import data into
MATLAB using load -ascii with your file. For an example, see the atoms
example under MATLAB Coder.
Break Out the Computational Part of the Algorithm into a Separate MATLAB
Function
10-15
10
Code Generation
code into the primary computational portion, from which C code generation
is desired, and a harness or driver, which does not need to generate C code that code will run in MATLAB. The harness may contain visualization and
other verification aids that are not actually part of the system under test.
The code for the main algorithm of this example resides in a function called
visionRecovertformCodeGeneration_kernel.m
Once the code has been re-architected as described above, you must check
that the rest of the code uses capabilities that are supported by MATLAB
coder. For a list of supported commands, see MATLAB Coder documentation.
For a list of supported language constructs, see here.
It may be convenient to have limited visualization or some other capability
that is not supported by the MATLAB Coder present in the function
containing the main algorithm, which we hope to compile. In these cases, you
can declare these items extrinsic (using coder.extrinsic). Such capability is
only possible when you generate the C code into a MATLAB MEX-file, and
those functions will actually run in interpreted MATLAB mode. If generating
code for standalone use, extrinsic functions are either ignored or they generate
an error, depending on whether the code generation engine determines that
they affect the results. Thus the code must be properly architected so that the
extrinsic functions do not materially affect the code in question if a standalone
executable is ultimately desired.
The original example uses showMatchedFeatures and imshowpair routines
for visualization of the results. These routines are extracted to a new function
featureMatchingVisualization_extrinsic.m. This function is declared extrinsic.
Run the Simulation
10-16
10-17
10
Code Generation
mlcmexdir
[matchedOriginalLocCG, matchedDistortedLocCG,...
thetaRecoveredCG, scaleRecoveredCG, recoveredCG] = ...
visionRecovertformCodeGeneration_kernel_mex(original, distorted);
scaleRecovered = 0.699810
thetaRecovered = 29.658672
cd(curdir);
clear visionRecovertformCodeGeneration_kernel_mex;
cd ..;
rmdir(mlcmexdir,'s');
cd(visiondemo_dir);
Compare Codegen with MATLAB Code
10-18
Recovered scale and theta for both MATLAB and CODEGEN, as shown
above, are within reasonable tolerance. Furthermore, the matched points
are identical, as shown below:
isequal(matchedOriginalLocCG, matchedOriginalLoc)
isequal(matchedDistortedLocCG, matchedDistortedLoc)
ans =
1
ans =
1
Appendix
10-19
10
Code Generation
10-20
10-21
10
10-22
Code Generation
11
Define New System Objects
Define Basic System Objects on page 11-3
Change Number of Step Inputs or Outputs on page 11-5
Specify System Block Input and Output Names on page 11-8
Validate Property and Input Values on page 11-10
Initialize Properties and Setup One-Time Calculations on page 11-13
Set Property Values at Construction Time on page 11-16
Reset Algorithm State on page 11-19
Define Property Attributes on page 11-21
Hide Inactive Properties on page 11-25
Limit Property Values to Finite String Set on page 11-27
Process Tuned Properties on page 11-30
Release System Object Resources on page 11-32
Define Composite System Objects on page 11-34
Define Finite Source Objects on page 11-38
Save System Object on page 11-40
Load System Object on page 11-43
Clone System Object on page 11-46
Define System Object Information on page 11-47
Define System Block Icon on page 11-49
Add Header to System Block Dialog on page 11-51
Add Property Groups to System Object and Block Dialog on page 11-53
11
11-2
System object.
edit AddOne.m
2 Subclass your object from matlab.System. Insert this line as the first line
of your file.
classdef AddOne < matlab.System
3 Add the stepImpl method, which contains the algorithm that runs when
users call the step method on your object. You always set the stepImpl
method access to protected because it is an internal method that users
do not directly call or run.
All methods, except static methods, expect the System object handle as the
first input argument. You can use any name for your System object handle.
In this example, instead of passing in the object handle, ~ is used to indicate
that the object handle is not used in the function. Using ~ instead of an
object handle prevents warnings about unused variables from occurring.
By default, the number of inputs and outputs are both one. To
change the number of inputs or outputs, use the getNumInputsImpl or
getNumOutputsImpl method, respectively.
methods (Access=protected)
function y = stepImpl(~, x)
y = x + 1;
end
end
11-3
11
Note Instead of manually creating your class definition file, you can use
an option on the New > System Object menu to open a template. The
Basic template opens a simple System object template. The Advanced
template includes more advanced features of System objects, such as backup
and restore. The Simulink Extension template includes additional
customizations of the System object for use in the Simulink MATLAB System
block. You then can edit the template file, using it as guideline, to create
your own System object.
11-4
See Also
Related
Examples
Concepts
Update the stepImpl method to accept a second input and provide a second
output.
methods (Access=protected)
function [y1,y2] = stepImpl(~,x1,x2)
y1 = x1 + 1
y2 = x2 + 1;
end
end
Update the Associated Methods
11-5
11
methods (Access=protected)
function numIn = getNumInputsImpl(~)
numIn = 2;
end
function numOut = getNumOutputsImpl(~)
numOut = 2;
end
end
Complete Class Definition File with Multiple Inputs and Outputs
11-6
See Also
getNumInputsImpl | getNumOutputsImpl
Related
Examples
Concepts
11-7
11
11-8
methods
function obj = MyCounter(varargin)
setProperties(obj,nargin,varargin{:});
end
end
methods (Access=protected)
function setupImpl(obj, u)
obj.Count = 0;
end
function resetImpl(obj)
obj.Count = 0;
end
function y = stepImpl(obj, u)
if (u > obj.Threshold)
obj.Count = obj.Count + 1;
end
y = obj.Count;
end
function inputName = getInputNamesImpl(~)
inputName = 'source data';
end
function outputName = getOutputNamesImpl(~)
outputName = 'count';
end
end
end
See Also
Related
Examples
Concepts
11-9
11
This example shows how to validate the value of a single property using
set.PropertyName syntax. In this case, the PropertyName is Increment.
methods
% Validate the properties of the object
function set.Increment(obj,val)
if val >= 10
error('The increment value must be less than 10');
end
obj.Increment = val;
end
end
This example shows how to validate that the first input is a numeric value.
methods (Access=protected)
function validateInputsImpl(~,x)
if ~isnumeric(x)
11-10
11-11
11
if ~isnumeric(x)
error('Input must be numeric');
end
end
function out = stepImpl(obj,in)
if obj.UseIncrement
out = in + obj.Increment;
else
out = in + 1;
end
end
end
end
See Also
validateInputsImpl | validatePropertiesImpl
Related
Examples
Concepts
11-12
In this example, you define the public Filename property and specify the
value of that property as the nontunable string, default.bin. Users cannot
change nontunable properties after the setup method has been called. Refer
to the Methods Timing section for more information.
properties (Nontunable)
Filename ='default.bin'
end
Define Private Properties to Initialize
Users cannot access private properties directly, but only through methods of
the System object. In this example, you define the pFileID property as a
private property. You also define this property as hidden to indicate it is an
internal property that never displays to the user.
properties (Hidden,Access=private)
pFileID;
end
Define Setup
You use the setupImpl method to perform setup and initialization tasks. You
should include code in the setupImpl method that you want to execute one
time only. The setupImpl method is called once during the first call to the
step method. In this example, you allocate file resources by opening the
file for writing binary data.
methods
function setupImpl(obj,data)
obj.pFileID = fopen(obj.Filename,'wb');
11-13
11
if obj.pFileID < 0
error('Opening the file failed');
end
end
end
Although not part of setup, you should close files when your code is done using
them. You use the releaseImpl method to release resources.
Complete Class Definition File with Initialization and Setup
11-14
See Also
Related
Examples
Concepts
11-15
11
Define the System object constructor, which is a method that has the same
name as the class (MyFile in this example). Within that method, you use
the setProperties method to make all public properties available for input
when the user constructs the object. nargin is a MATLAB function that
determines the number of input arguments. varargin indicates all of the
objects public properties.
methods
function obj = MyFile(varargin)
setProperties(obj,nargin,varargin{:});
end
end
Complete Class Definition File with Constructor Setup
11-16
See Also
nargin | setProperties
11-17
11
Related
Examples
11-18
pCount is an internal counter property of the System object obj. The user
calls the reset method, which calls the resetImpl method. In this example
, pCount resets to 0.
Note When resetting an objects state, make sure you reset the size,
complexity, and data type correctly.
methods (Access=protected)
function resetImpl(obj)
obj.pCount = 0;
end
end
Complete Class Definition File with State Reset
11-19
11
obj.pCount = 0;
end
% The step method takes no inputs
function numIn = getNumInputsImpl(~)
numIn = 0;
end
end
end
end
See Also
Concepts
11-20
resetImpl
Use the nontunable attribute for a property when the algorithm depends on
the value being constant once data processing starts. Defining a property as
nontunable may improve the efficiency of your algorithm by removing the
need to check for or react to values that change. For code generation, defining
a property as nontunable allows the memory associated with that property
to be optimized. You should define all properties that affect the number of
input or output ports as nontunable.
System object users cannot change nontunable properties after the setup or
step method has been called. In this example, you define the InitialValue
property, and set its value to 0.
properties (Nontunable)
InitialValue = 0;
end
Specify Property as Logical
Logical properties have the value, true or false. System object users can
enter 1 or 0 or any value that can be converted to a logical. The value,
however, displays as true or false. You can use sparse logical values, but
they must be scalar values. In this example, the Increment property indicates
whether to increase the counter. By default, Increment is tunable property.
The following restrictions apply to a property with the Logical attribute,
Cannot also be Dependent or PositiveInteger
Default value must be true or false. You cannot use 1 or 0 as a default
value.
11-21
11
properties (Logical)
Increment = true
end
Specify Property as Positive Integer
If your algorithm uses properties that hold state, you can assign those
properties the DiscreteState attribute . Properties with this attribute
display their state values when users call getDiscreteStateImpl via the
getDiscreteState method. The following restrictions apply to a property
with the DiscreteState attribute,
Numeric, logical, or fi value, but not a scaled double fi value
Does not have any of these attributes: Nontunable, Dependent, Abstract,
Constant, or Transient.
No default value
Not publicly settable
GetAccess=Public by default
Value set only using the setupImpl method or when the System object is
locked during resetImpl or stepImpl
In this example, you define the Count property.
properties (DiscreteState)
Count;
11-22
end
Complete Class Definition File with Property Attributes
11-23
11
end
% The step method takes no inputs
function numIn = getNumInputsImpl(~)
numIn = 0;
end
end
end
Concepts
11-24
Class Attributes
Property Attributes
What You Cannot Change While Your System Is Running
Methods Timing on page 11-75
11-25
11
See Also
11-26
isInactivePropertyImpl
String sets use two related properties. You first specify the user-visible
property name and default string value. Then, you specify the associated
hidden property by appending Set to the property name. You must use
a capital S in Set.
In the Set property, you specify the valid string values as a cell array of the
matlab.system.Stringset class. This example uses Color and ColorSet
as the associated properties.
properties
Color = 'blue'
end
properties (Hidden,Transient)
ColorSet = matlab.system.StringSet({'red','blue','green'});
end
Complete Class Definition File with String Set
11-27
11
methods(Access = protected)
function stepImpl(obj)
h = Whiteboard.getWhiteboard();
plot(h, ...
randn([2,1]),randn([2,1]), ...
'Color',obj.Color(1));
end
function releaseImpl(obj)
cla(Whiteboard.getWhiteboard());
hold('on');
end
function n = getNumInputsImpl(~)
n = 0;
end
function n = getNumOutputsImpl(~)
n = 0;
end
end
methods (Static)
function a = getWhiteboard()
h = findobj('tag','whiteboard');
if isempty(h)
h = figure('tag','whiteboard');
hold('on');
end
a = gca;
end
end
end
%%
% Each call to step draws lines on a whiteboard
%% Construct the System object
hGreenInk = Whiteboard;
hBlueInk = Whiteboard;
11-28
See Also
matlab.system.StringSet
11-29
11
11-30
See Also
processTunedPropertiesImpl
11-31
11
This method allows you to clear the axes on the Whiteboard figure window
while keeping the figure open.
methods
function releaseImpl(obj)
cla(Whiteboard.getWhiteboard());
hold('on');
end
end
Complete Class Definition File with Released Resources
11-32
end
function releaseImpl(obj)
cla(Whiteboard.getWhiteboard());
hold('on');
end
function n = getNumInputsImpl(~)
n = 0;
end
function n = getNumOutputsImpl(~)
n = 0;
end
end
methods (Static)
function a = getWhiteboard()
h = findobj('tag','whiteboard');
if isempty(h)
h = figure('tag','whiteboard');
hold('on');
end
a = gca;
end
end
end
See Also
Related
Examples
releaseImpl
11-33
11
To define a System object from other System objects, store those objects in
your class definition file as properties. In this example, FIR and IIR are
separate System objects defined in their own class-definition files. You use
those two objects to calculate the pFir and pIir property values.
properties (Nontunable, Access = private)
pFir % store the FIR filter
pIir % store the IIR filter
end
methods
function obj = Filter(varargin)
setProperties(obj, nargin, varargin{:});
obj.pFir = FIR(obj.zero);
obj.pIir = IIR(obj.pole);
end
end
Complete Class Definition File of Composite System Object
11-34
end
properties (Nontunable,Access=private)
pZero % store the FIR filter
pPole % store the IIR filter
end
methods
function obj = Filter(varargin)
setProperties(obj,nargin, varargin{:});
% Create instances of FIR and IIR as
% private properties
obj.pZero = Zero(obj.zero);
obj.pPole = Pole(obj.pole);
end
end
methods (Access=protected)
function setupImpl(obj,x)
setup(obj.pZero,x);
setup(obj.pPole,x);
end
function resetImpl(obj)
reset(obj.pZero);
reset(obj.pPole);
end
function y = stepImpl(obj,x)
y = step(obj.pZero,x) + step(obj.pPole,x);
end
function releaseImpl(obj)
release(obj.pZero);
release(obj.pPole);
end
end
end
Class Definition File for IIR Component of Filter
11-35
11
11-36
end
end
methods (Access=protected)
function y = stepImpl(obj,x)
y = x + obj.tap * obj.Num;
obj.tap = x;
end
end
end
See Also
nargin
11-37
11
11-38
y = 0;
end
end
function bDone = isDoneImpl(obj)
bDone = obj.NumSteps==2;
end
end
methods (Access=protected)
function n = getNumInputsImpl(~)
n = 0;
end
function n = getNumOutputsImpl(~)
n = 1;
end
end
end
See Also
Concepts
matlab.system.mixin.FiniteSource
11-39
11
11-40
end
properties (Dependent)
dependentprop
end
methods
function obj = MySaveLoader(varargin)
[email protected]();
setProperties(obj, nargin, varargin{:});
end
end
methods(Access = protected)
function setupImpl(obj, varargin)
obj.state = 42;
end
function out = stepImpl(obj, in)
obj.state = in;
out = obj.state;
end
end
% Serialization
methods(Access=protected)
function s = saveObjectImpl(obj)
% Call the base class method
s = [email protected](obj);
% Save the child System objects
s.child = matlab.System.saveObject(obj.child);
% Save the protected & private properties
s.protected = obj.protected;
s.pdependentprop = obj.pdependentprop;
% Save the state only if object locked
if isLocked(obj)
11-41
11
s.state = obj.state;
end
end
function loadObjectImpl(obj,s,wasLocked)
% Load child System objects
obj.child = matlab.System.loadObject(s.child);
% Load protected and private properties
obj.protected = s.protected;
obj.pdependentprop = s.pdependentprop;
% Load the state only if object locked
if wasLocked
obj.state = s.state;
end
% Call base class method to load public properties
[email protected](obj,s,wasLocked);
end
end
end
See Also
saveObjectImpl | loadObjectImpl
Related
Examples
11-42
11-43
11
state
end
properties (Dependent)
dependentprop
end
methods
function obj = MySaveLoader(varargin)
[email protected]();
setProperties(obj, nargin, varargin{:});
end
end
methods(Access = protected)
function setupImpl(obj,varargin)
obj.state = 42;
end
function out = stepImpl(obj,in)
obj.state = in;
out = obj.state;
end
end
% Serialization
methods(Access=protected)
function s = saveObjectImpl(obj)
% Call the base class method
s = [email protected](obj);
% Save the child System objects
s.child = matlab.System.saveObject(obj.child);
% Save the protected & private properties
s.protected = obj.protected;
s.pdependentprop = obj.pdependentprop;
% Save the state only if object locked
11-44
if isLocked(obj)
s.state = obj.state;
end
end
function loadObjectImpl(obj,s,wasLocked)
% Load child System objects
obj.child = matlab.System.loadObject(s.child);
% Load protected and private properties
obj.protected = s.protected;
obj.pdependentprop = s.pdependentprop;
% Load the state only if object locked
if wasLocked
obj.state = s.state;
end
% Call base class method to load public properties
[email protected](obj,s,wasLocked);
end
end
end
See Also
saveObjectImpl | loadObjectImpl
Related
Examples
11-45
11
You can define your own clone method, which is useful for copying objects
without saving their state. The default cloneImpl method copies both a
System object and its current state. If an object is locked, the default
cloneImpl creates a cloned object that is also locked. An example of when you
may want to write your own clone method is for cloning objects that handle
resources. These objects cannot allocate resources twice and you would not
want to save their states. To write your clone method, use the saveObject
and loadObject methods to perform the clone within the cloneImpl method.
methods(Access=protected)
function obj2 = cloneImpl(obj1)
s = saveObject (obj1);
obj2 = loadObject(s);
end
end
Complete Class Definition File with Clone
See Also
11-46
You can define your own info method to display specific information for
your System object. The default infoImpl method returns an empty struct.
This infoImpl method returns detailed information when the info method
is called using info(x,'details') or only count information if it is called
using info(x).
methods (Access=protected)
function s = infoImpl(obj,varargin)
if nargin>1 && strcmp('details',varargin(1))
s = struct('Name','Counter',...
'Properties', struct('CurrentCount', ...
obj.pCount,'Threshold',obj.Threshold));
else
s = struct('Count',obj.pCount);
end
end
end
Complete Class Definition File with InfoImpl
11-47
11
end
function resetImpl(obj)
obj.Count = 0;
end
function y = stepImpl(obj, u)
if (u > obj.Threshold)
obj.Count = obj.Count + 1;
end
y = obj.Count;
end
function s = infoImpl(obj,varargin)
if nargin>1 && strcmp('details',varargin(1))
s = struct('Name','Counter',...
'Properties', struct('CurrentCount', ...
obj.pCount,'Threshold',obj.Threshold));
else
s = struct('Count',obj.pCount);
end
end
end
See Also
11-48
infoImpl
11-49
11
methods (Access=protected)
function setupImpl(obj, u)
obj.Count = 0;
end
function resetImpl(obj)
obj.Count = 0;
end
function y = stepImpl(obj, u)
if (u > obj.Threshold)
obj.Count = obj.Count + 1;
end
y = obj.Count;
end
function icon = getIconImpl(~)
icon = sprintf('New\nCounter');
end
end
end
See Also
getIconImpl | matlab.system.mixin.CustomIcon
Concepts
11-50
This example shows how to use getHeaderImpl to specify a panel title and
text for the MyCounter System object.
If you do not specify the getHeaderImpl, the block does not display any title
or text for the panel.
You always set the getHeaderImpl method access to protected because it is
an internal method that end users do not directly call or run.
methods(Static,Access=protected)
function header = getHeaderImpl
header = matlab.system.display.Header('MyCounter',...
'Title','My Enhanced Counter');
end
end
Complete Class Definition File with Defined Header
11-51
11
See Also
11-52
getHeaderImpl | matlab.system.display.Header
11-53
11
11-54
upperGroup = matlab.system.display.Section(...
'Title', 'Upper threshold', ...
'PropertyList',{'UpperThreshold'});
lowerGroup = matlab.system.display.Section(...
'Title','Lower threshold', ...
'PropertyList',{'UseLowerThreshold','LowerThreshold'});
thresholdGroup = matlab.system.display.SectionGroup(...
'Title', 'Parameters', ...
'Sections', [upperGroup,lowerGroup]);
valuesGroup = matlab.system.display.SectionGroup(...
'Title', 'Initial conditions', ...
'PropertyList', {'StartValue'});
groups = [thresholdGroup, valuesGroup];
end
end
methods(Access=protected)
function setupImpl(obj, ~, ~)
obj.Count = obj.StartValue;
end
function y = stepImpl(obj,u)
if obj.UseLowerThreshold
if (u > obj.UpperThreshold) || ...
(u < obj.LowerThreshold)
obj.Count = obj.Count + 1;
end
else
if (u > obj.UpperThreshold)
obj.Count = obj.Count + 1;
end
end
y = obj.Count;
end
function resetImpl(obj)
obj.Count = obj.StartValue;
end
11-55
11
See Also
getPropertyGroupsImpl | matlab.system.display.Section |
matlab.system.display.SectionGroup
Concepts
11-56
To use the getOutputSizeImpl method, you must subclass from both the
matlab.System base class and the Propagates mixin class.
classdef CounterReset < matlab.System & ...
matlab.system.mixin.Propagates
Specify Output Size
11-57
11
function y = stepImpl(obj,u1,u2)
% Add to count if u1 is above threshold
% Reset if u2 is true
if (u2)
obj.Count = 0;
elseif (u1 > obj.Threshold)
obj.Count = obj.Count + 1;
end
y = obj.Count;
end
function resetImpl(obj)
obj.Count = 0;
end
function N = getNumInputsImpl(~)
N = 2;
end
function [sz,dt,cp] = getDiscreteStateSpecificationImpl(~,name)
if strcmp(name,'Count')
sz = [1 1];
dt = 'double';
cp = false;
else
error(['Error: Incorrect State Name: 'name'.']);
end
end
function dataout = getOutputDataTypeImpl(~)
dataout = 'double';
end
function sizeout = getOutputSizeImpl(~)
sizeout = [1 1];
end
function cplxout = isOutputComplexImpl(~)
cplxout = false;
end
function fixedout = isOutputFixedSizeImpl(~)
fixedout = true;
11-58
end
end
end
See Also
matlab.system.mixin.Propagates | getOutputSizeImpl
Concepts
11-59
11
11-60
obj.Count = 0;
end
function resetImpl(obj)
obj.Count = 0;
end
function y = stepImpl(obj,u1,u2)
% Add to count if u1 is above threshold
% Reset if u2 is true
if (u2)
obj.Count = 0;
elseif (u1 > obj.Threshold)
obj.Count = obj.Count + 1;
end
y = obj.Count;
end
function N = getNumInputsImpl(~)
N = 2;
end
function [sz,dt,cp] = getDiscreteStateSpecificationImpl(~,name)
if strcmp(name,'Count')
sz = [1 1];
dt = 'double';
cp = false;
else
error(['Error: Incorrect State Name: 'name'.']);
end
end
function dataout = getOutputDataTypeImpl(~)
dataout = 'double';
end
function sizeout = getOutputSizeImpl(~)
sizeout = [1 1];
end
function cplxout = isOutputComplexImpl(~)
cplxout = false;
end
11-61
11
See Also
matlab.system.mixin.Propagates | getOutputDataTypeImpl
Concepts
11-62
To use the isOutputComplexImpl method, you must subclass from both the
matlab.System base class and the Propagates mixin class.
classdef CounterReset < matlab.System & ...
matlab.system.mixin.Propagates
Specify Output Complexity
11-63
11
end
function resetImpl(obj)
obj.Count = 0;
end
function y = stepImpl(obj,u1,u2)
% Add to count if u1 is above threshold
% Reset if u2 is true
if (u2)
obj.Count = 0;
elseif (u1 > obj.Threshold)
obj.Count = obj.Count + 1;
end
y = obj.Count;
end
function N = getNumInputsImpl(~)
N = 2;
end
function [sz,dt,cp] = getDiscreteStateSpecificationImpl(~,name)
if strcmp(name,'Count')
sz = [1 1];
dt = 'double';
cp = false;
else
error(['Error: Incorrect State Name: 'name'.']);
end
end
function dataout = getOutputDataTypeImpl(~)
dataout = 'double';
end
function sizeout = getOutputSizeImpl(~)
sizeout = [1 1];
end
function cplxout = isOutputComplexImpl(~)
cplxout = false;
end
function fixedout = isOutputFixedSizeImpl(~)
11-64
fixedout = true;
end
end
end
See Also
matlab.system.mixin.Propagates | isOutputComplexImpl
Concepts
11-65
11
To use the isOutputFixedSizeImpl method, you must subclass from both the
matlab.System base class and the Propagates mixin class.
classdef CounterReset < matlab.System & ...
matlab.system.mixin.Propagates
Specify Output as Fixed Size
11-66
obj.Count = 0;
end
function resetImpl(obj)
obj.Count = 0;
end
function y = stepImpl(obj,u1,u2)
% Add to count if u1 is above threshold
% Reset if u2 is true
if (u2)
obj.Count = 0;
elseif (u1 > obj.Threshold)
obj.Count = obj.Count + 1;
end
y = obj.Count;
end
function N = getNumInputsImpl(~)
N = 2;
end
function [sz,dt,cp] = getDiscreteStateSpecificationImpl(~,name)
if strcmp(name,'Count')
sz = [1 1];
dt = 'double';
cp = false;
else
error(['Error: Incorrect State Name: 'name'.']);
end
end
function dataout = getOutputDataTypeImpl(~)
dataout = 'double';
end
function sizeout = getOutputSizeImpl(~)
sizeout = [1 1];
end
function cplxout = isOutputComplexImpl(~)
cplxout = false;
end
11-67
11
See Also
matlab.system.mixin.Propagates | isOutputFixedSizeImpl
Concepts
11-68
11-69
11
Count
end
methods (Access=protected)
function setupImpl(obj,~,~)
obj.Count = 0;
end
function resetImpl(obj)
obj.Count = 0;
end
function y = stepImpl(obj,u1,u2)
% Add to count if u1 is above threshold
% Reset if u2 is true
if (u2)
obj.Count = 0;
elseif (u1 > obj.Threshold)
obj.Count = obj.Count + 1;
end
y = obj.Count;
end
function N = getNumInputsImpl(~)
N = 2;
end
function [sz,dt,cp] = getDiscreteStateSpecificationImpl(~,name)
sz = [1 1];
dt = 'double';
cp = false;
end
function dataout = getOutputDataTypeImpl(~)
dataout = 'double';
end
function sizeout = getOutputSizeImpl(~)
sizeout = [1 1];
end
function cplxout = isOutputComplexImpl(~)
cplxout = false;
11-70
end
function fixedout = isOutputFixedSizeImpl(~)
fixedout = true;
end
end
end
See Also
matlab.system.mixin.Propagates | getDiscreteStateSpecificationImpl
Concepts
11-71
11
11-72
methods(Access=protected)
function validatePropertiesImpl(obj)
if ((numel(obj.NumDelays)>1) || (obj.NumDelays <= 0))
error('Number of delays must be positive non-zero scalar value.
end
if (numel(obj.InitialOutput)>1)
error('Initial Output must be scalar value.');
end
end
function setupImpl(obj, ~)
obj.PreviousInput = ones(1,obj.NumDelays)*obj.InitialOutput;
end
function resetImpl(obj)
11-73
11
obj.PreviousInput = ones(1,obj.NumDelays)*obj.InitialOutput;
end
function [y] = outputImpl(obj, ~)
y = obj.PreviousInput(end);
end
function updateImpl(obj, u)
obj.PreviousInput = [u obj.PreviousInput(1:end-1)];
end
function flag = isInputDirectFeedthroughImpl(~,~)
flag = false;
end
end
end
See Also
Concepts
11-74
Methods Timing
Methods Timing
In this section...
Setup Method Call Sequence on page 11-75
Step Method Call Sequence on page 11-76
Reset Method Call Sequence on page 11-76
Release Method Call Sequence on page 11-77
11-75
11
11-76
Methods Timing
See Also
Related
Examples
Concepts
11-77
11
11-78
11-79
11
11-80