2D Variance
Compute variance of input or sequence of inputs
Libraries:
Computer Vision Toolbox /
Statistics
Description
The 2D Variance block computes the unbiased variance of an input array. The input can be a 1D vector, 2D matrix, or an ND array. The block can compute variance along a specified dimension of the input or the entire input. If you select the Running variance parameter, the block can also track the variance in a sequence of inputs over a time period.
Examples
Compute Variance of ROIs
Compute the variance of regions of interest (ROIs) in the input image. The input image consists of different texture regions, and a mask image specifies ROIs selected to contain the texture regions.
Ports
Input
In — Input array
vector  matrix  array
Input array, specified as a vector, matrix, or ND array. This port is unnamed until you select the Enable ROI processing parameter.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
 fixed point
Complex Number Support: Yes
Rst — Reset port
scalar
Reset port, specified as a scalar. This port specifies the event that causes the block to reset the running variance. The sample time of the Rst input must be a positive integer and a multiple of the input sample time.
Dependencies
To enable this port, select the Running variance parameter
and set the Reset port parameter to Rising
edge
, Falling edge
, Either
edge
, or Nonzero sample
.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
 Boolean
ROI — Region of interest
vector  matrix
Region of interest (ROI), specified as a fourelement vector, mby4 matrix, or MbyN matrix. The input value to this port depends on the ROI type parameter.
Note
You can use the ROI
port only if the input is a 2D
image.
Dependencies
To enable this port, set the Find the variance value over
parameter to Entire input
and select the Enable
ROI processing parameter.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
 Boolean
 fixed point
Label — Label matrix
matrix
Label matrix, specified as a matrix of nonnegative integers. The label matrix represents the objects in an image. The pixels labeled 0 are the background. The pixels labeled 1 make up one object; the pixels labeled 2 make up a second object; and so on. The size of the label matrix must be the same size as the 2D input.
Dependencies
To enable this port, select the Enable ROI processing
parameter and set the ROI type parameter to
Label matrix
.
Data Types: uint8
 uint16
 uint32
Label Numbers — Label values of ROIs
vector
Label values of ROIs, specified as a Melement vector. The value of M can be less than or equal to the number of objects that are labeled in the label matrix.
Dependencies
To enable this port, select the Enable ROI processing
parameter and set the ROI type parameter to
Label matrix
.
Data Types: uint8
 uint16
 uint32
Output
Out — Computed output variance
scalar  vector  matrix  NDarray
Computed output variance, returned as a scalar, vector, matrix, or NDarray. The size of the returned output variance depends on the size of the input, and the settings for the Running variance and Find the variance value over parameters.
This port is unnamed until you select the Output flag indicating if ROI
is within image bounds and the ROI type parameter is
set to Rectangles
, Lines
, or
Label matrix
.
If Running variance Is Cleared
You can compute variance along any specified dimension of the input or the entire input. The output is a:
Scalar if the input is of any size and the Find the variance value over parameter is set to
Entire input
.Vector if the input is a matrix and the Find the variance value over parameter is set to any one of
Each row
,Each column
, andSpecified dimension
. In this case, the Dimension value forSpecified dimension
can be either 1 or 2.Matrix if the input is a 3D array and the Find the variance value over parameter is set to
Specified dimension
and the Dimension value is 3.ND array if the input is an ND array and the Find the variance value over parameter is set to an option other than
Entire input
.If you compute variance along the Nth dimension of the input, then the returned output is an (N1)D array. In this case, the Dimension value for
Specified dimension
is set to N.Example: For a 3D input array of size MbyNbyP, the dimension of the returned output is:
1byNbyP if you set the Find the variance value over parameter to
Entire row
.Mby1byP if you set the Find the variance value over parameter to
Entire column
.MbyN if you set the Find the variance value over parameter to
Specified dimension
and the Dimension value to 3.
If Running variance Is Selected
When you select the Running variance parameter, the block computes the variance of each sample in the input with respect to all previous samples. The output is of the same size as the input.
Data Types: single
 double
 int8
 int16
 int32
 uint8
 uint16
 uint32
 fixed point
Flag — Validation for ROI
0
 1
Validation for ROI, returned as 0 or 1. The output value signifies if all of the ROIs specified at the input lie within the image bounds. The output value depends on the values of the Output and ROI Type parameters.
ROI Type  Output  Output from Flag port  Description 
 Individual statistics for each
ROI  0  ROI is completely outside the input image. 
1  ROI is either completely or partially inside the input image.  
Single statistic for all
ROIs  0  ROI is completely outside the input image.  
1  ROI is either completely or partially inside the input image.  
Label matrix  Individual statistics for each
ROI  0  Label number is not in the label matrix. 
1  Label number is in the label matrix.  
Single statistic for all
ROIs  0  None of the label numbers are in the label matrix.  
1  At least one of the label numbers is in the label matrix. 
Note
If the ROI is partially outside the image, the block computes the variance values only for the portion of the ROI that lies within the image bounds.
Dependencies
To enable this port, select the Output flag indicating if ROI is
within image bounds parameter and set the value of ROI
type parameter to Rectangle
,
Lines
, or Label
Matrix
.
Parameters
Main
Running variance — Track running variance
off
(default)  on
Select to track the variance of successive inputs to the block. In this mode, the block treats each element as a channel.
Reset port — Reset event
None
(default)  Rising edge
 Falling edge
 Either edge
 Nonzero sample
Specify when the block detects a reset event. The block resets the running variance when a reset event is detected at the optional Rst port. The reset sample time must be a positive integer and a multiple of the input sample time.
Specify the reset event as:
None
to disable the Rst port.Rising edge
to trigger a reset event when the Rst input does one of the following:Rises from a negative value to either a positive value or zero
Rises from zero to a positive value, where the rise is not a continuation of a rise from a negative value to zero
Falling edge
to trigger a reset event when the Rst input does one of the following:Falls from a positive value to either a negative value or zero
Falls from zero to a negative value, where the fall is not a continuation of a fall from a positive value to zero
Either edge
to trigger a reset event when the Rst input is aRising edge
orFalling edge
.Nonzero sample
to trigger a reset event at each sample time, when the Rst input is not zero.Note
When running simulations in the Simulink^{®} multitasking mode, reset signals have a onesample latency. Therefore, when the block detects a reset event, there is a onesample delay at the reset port rate before the block applies the reset.
Dependencies
To enable this parameter, select the Running variance parameter.
Find the variance value over — Dimension along which variance is computed
Entire input
(default)  Each row
 Each column
 Specified dimension
Specify the dimension of the input along which the block computes the variance.
Entire input
— Computes variance over the entire input.Each row
— Computes variance over each row.Each column
— Computes variance over each column.Specified dimension
— Computes variance over the dimension specified in the Dimension parameter.If Dimension is 1, the output is the same as when you select
Each column
.If Dimension is 2, the output is the same as when you select
Each row
.
Dependencies
To enable this parameter, clear the Running variance parameter.
Dimension — Custom dimension
1
(default)  scalar
Specify the dimension of the input array over which the variance is computed as a onebased value. The value of this parameter must be greater than 0 and less than or equal to the number of dimensions in the input array.
Dependencies
To enable this parameter, set the Find the variance value
over parameter to Specified
dimension
.
ROI Processing
Enable ROI processing — Compute variance within a particular region
off
(default)  on
Select to calculate variance within a particular ROI in the image.
Note
Full ROI processing is available only if you have a Computer Vision Toolbox™ license. If you do not have a Computer Vision Toolbox license, you can still use ROI processing, but the ROI
type is limited to Rectangles
.
Dependencies
To enable this parameter, set the Find the variance value
over parameter to Entire input
.
ROI type — Type of ROI
Rectangles
(default)  Lines
 Label matrix
 Binary mask
Specify the type of ROI that represents the regions in the image over which the block computes the variance. The type of ROI can be a rectangle, line, label matrix, or a binary mask.
Parameters  Description  
ROI type  Inputs to the ROI port  
Rectangles 


Lines 


Label matrix  MbyN matrix  Matrix of the same size as the input image. The matrix contains label values that represent different objects in an image. The pixels labeled 0 are the background. The pixels labeled 1 make up one object; the pixels labeled 2 make up a second object; and so on. 
Binary mask  MbyN matrix  Matrix of the same size as the input image. The binary mask classifies image pixels as belonging to either the region of interest or the background. The mask pixel values of 1 indicate that the image pixel belongs to the ROI. The mask pixel values of 0 indicate that the image pixel is part of the background. 
Dependencies
To enable this parameter, select the Enable ROI processing parameter.
ROI portion to process — Portion of ROI in which to calculate variance
Entire ROI
(default)  ROI perimeter
Specify the portion of the ROI from which the block has to calculate 2D variance. The ROI portion is either the entire ROI or the ROI perimeter.
Dependencies
To enable this parameter, select the Enable ROI processing
parameter and set the ROI type parameter to
Rectangles
.
Output — Calculate variance for individual or entire ROI
Individual statistics for each
ROI
(default)  Single statistic for all ROIs
Specify whether to calculate 2D variance individually for each ROI or for the entire ROI.
If you select
Individual statistics for each ROI
, the block outputs a vector of variance values. The size of the output vector is equal to the number of ROIs.If you select
Single statistic for all ROIs
, the block outputs a scalar value. The scalar value represents the statistical value for all the specified ROIs.
Dependencies
To enable this parameter, select the Enable ROI processing
parameter and set the ROI type parameter to
Rectangles
, Lines
, or
Label matrix
.
Output flag indicating if ROI is within image bounds — Enable Flag port
off
(default)  on
Select to expose the Flag port. For a description of the Flag port output, see Flag.
Dependencies
To enable this parameter, select the Enable ROI processing
parameter and set the ROI type parameter to
Rectangles
, Lines
, or
Label matrix
.
Data Types
For details on the fixedpoint block parameters, see Specify FixedPoint Attributes for Blocks (DSP System Toolbox).
Lock data type settings against change by the fixedpoint tools — Prevent fixedpoint tools from overriding data types
off
(default)  on
Select this parameter to prevent the fixedpoint tools from overriding the data types you specify in this block. For more information, see Lock the Output Data Type Setting (FixedPoint Designer).
Block Characteristics
Data Types 

Multidimensional Signals 

VariableSize Signals 

More About
Variance
The variance of an input is the square of the standard deviation of the input. Variance gives a measure of deviation of the input values from its mean value.
For a purely real or imaginary input, u, of size MbyN, the variance is:
$$y={\sigma}^{2}=\frac{{\displaystyle \sum _{i=1}^{M}{\displaystyle \sum _{j=1}^{N}{\left{u}_{ij}\right}^{2}\frac{{\left{\displaystyle \sum _{i=1}^{M}{\displaystyle \sum _{j=1}^{N}{u}_{ij}}}\right}^{2}}{M*N}}}}{M*N1},$$
where:
u_{ij} is the input data element at (i, j).
M is the length of the jth column.
N is the number of columns.
For complex inputs, the variance is:
$${\sigma}^{2}={\sigma}_{\mathrm{Re}}{}^{2}+{\sigma}_{\mathrm{Im}}{}^{2},$$
where:
σ_{Re}^{2} is the variance of the real part of the complex input.
σ_{Im}^{2} is the variance of the imaginary part of the complex input.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
Version History
Introduced before R2006a
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
 América Latina (Español)
 Canada (English)
 United States (English)
Europe
 Belgium (English)
 Denmark (English)
 Deutschland (Deutsch)
 España (Español)
 Finland (English)
 France (Français)
 Ireland (English)
 Italia (Italiano)
 Luxembourg (English)
 Netherlands (English)
 Norway (English)
 Österreich (Deutsch)
 Portugal (English)
 Sweden (English)
 Switzerland
 United Kingdom (English)