Documentation

# 2-D Variance

Compute variance of input or sequence of inputs

## Library

Statistics

`visionstatistics` ## Description

The 2-D Variance block computes the unbiased variance of each row or column of the input, along vectors of a specified dimension of the input, or of the entire input. The 2-D Variance block can also track the variance of a sequence of inputs over a period of time. The Running variance parameter selects between basic operation and running operation.

### Port Description

PortSupported Data Types

Input

• Double-precision floating point

• Single-precision floating point

• Fixed point

• 8-, 16-, and 32-bit signed integers

• 8-, 16-, and 32-bit unsigned integers

Reset

• Double-precision floating point

• Single-precision floating point

• Boolean

• 8-, 16-, and 32-bit signed integers

• 8-, 16-, and 32-bit unsigned integers

ROI

Rectangles and lines:

• Double-precision floating point

• Single-precision floating point

• Boolean

• 8-, 16-, and 32-bit signed integers

• 8-, 16-, and 32-bit unsigned integers

Binary Mask:

• Boolean

Label

• 8-, 16-, and 32-bit unsigned integers

Label Numbers

• 8-, 16-, and 32-bit unsigned integers

Output

• Double-precision floating point

• Single-precision floating point

• Fixed point

• 8-, 16-, and 32-bit signed integers

• 8-, 16-, and 32-bit unsigned integers

Flag

• Boolean

### Basic Operation

When you do not select the Running variance check box, the block computes the variance of each row or column of the input, along vectors of a specified dimension of the input, or of the entire input at each individual sample time, and outputs the array y. Each element in y is the variance of the corresponding column, row, vector, or entire input. The output y depends on the setting of the Find the variance value over parameter. For example, consider a 3-dimensional input signal of size M-by-N-by-P:

• `Entire input` — The output at each sample time is a scalar that contains the variance of the entire input.

```y = var(u(:)) % Equivalent MATLAB code ```
• `Each row` — The output at each sample time consists of an M-by-1-by-P array, where each element contains the variance of each vector over the second dimension of the input. For an input that is an M-by-N matrix, the output at each sample time is an M-by-1 column vector.

```y = var(u,0,2) % Equivalent MATLAB code ```
• `Each column` — The output at each sample time consists of a 1-by-N-by-P array, where each element contains the variance of each vector over the first dimension of the input. For an input that is an M-by-N matrix, the output at each sample time is a 1-by-N row vector.

```y = var(u,0,1) % Equivalent MATLAB code ```

In this mode, the block treats length-M unoriented vector inputs as M-by-1 column vectors.

• `Specified dimension` — The output at each sample time depends on Dimension. If Dimension is set to 1, the output is the same as that when you select ```Each column```. If Dimension is set to `2`, the output is the same as when you select ```Each row```. If Dimension is set to `3`, the output at each sample time is an M-by-N matrix containing the variance of each vector over the third dimension of the input.

```y = var(u,0,Dimension) % Equivalent MATLAB code ```

For purely real or purely imaginary inputs, the variance of an M-by-N matrix is the square of the standard deviation:

`$y={\sigma }^{2}=\frac{\sum _{i=1}^{M}\sum _{j=1}^{N}{|{u}_{ij}|}^{2}-\frac{{|\sum _{i=1}^{M}\sum _{j=1}^{N}{u}_{ij}|}^{2}}{M*N}}{M*N-1}$`

For complex inputs, the variance is given by the following equation:

`${\sigma }^{2}={\sigma }_{\mathrm{Re}}{}^{2}+{\sigma }_{\mathrm{Im}}{}^{2}$`

### Running Operation

When you select the Running variance check box, the block tracks the variance of successive inputs to the block. In this mode, the block treats each element as a channel.

### Resetting the Running Variance

The block resets the running variance whenever a reset event is detected at the optional Rst port. The reset sample time must be a positive integer multiple of the input sample time.

You specify the reset event in the Reset port parameter:

• `None` disables the Rst port.

• `Rising edge` — Triggers a reset operation when the Rst input does one of the following:

• Rises from a negative value to 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 (see the following figure) • `Falling edge` — Triggers a reset operation when the Rst input does one of the following:

• Falls from a positive value to 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 (see the following figure) • `Either edge` — Triggers a reset operation when the Rst input is a `Rising edge` or ```Falling edge``` (as described earlier)

• `Non-zero sample` — Triggers a reset operation at each sample time that the Rst input is not zero

### Note

When running simulations in the Simulink® MultiTasking mode, reset signals have a one-sample latency. Therefore, when the block detects a reset event, there is a one-sample delay at the reset port rate before the block applies the reset.

### ROI Processing

To calculate the statistical value within a particular region of interest (ROI) of the input, select the Enable ROI processing check box. This option is only available when the Find the variance value over parameter is set to `Entire input` and the Running variance check box is not selected. ROI processing is only supported for 2-D inputs.

Use the ROI type parameter to specify whether the ROI is a binary mask, label matrix, rectangle, or line. ROI processing is only supported for 2-D inputs.

• A binary mask is a binary image that enables you to specify which pixels to highlight, or select.

• In a label matrix, pixels equal to 0 represent the background, pixels equal to 1 represent the first object, pixels equal to 2 represent the second object, and so on. When the ROI type parameter is set to `Label matrix`, the Label and Label Numbers ports appear on the block. Use the Label Numbers port to specify the objects in the label matrix for which the block calculates statistics. The input to this port must be a vector of scalar values that correspond to the labeled regions in the label matrix.

• For more information about the format of the input to the ROI port when the ROI is a rectangle or a line, see the Draw Shapes reference page.

### Note

For rectangular ROIs, use the ROI portion to process parameter to specify whether to calculate the statistical value for the entire ROI or just the ROI perimeter.

Use the Output parameter to specify the block output. The block can output separate statistical values for each ROI or the statistical value for all specified ROIs. This parameter is not available if, for the ROI type parameter, you select `Binary mask`.

If, for the ROI type parameter, you select `Rectangles` or `Lines`, the Output flag indicating if ROI is within image bounds check box appears in the dialog box. If you select this check box, the Flag port appears on the block. The following tables describe the Flag port output based on the block parameters.

Output = Individual Statistics for Each ROI

Flag Port OutputDescription
0ROI is completely outside the input image.
1ROI is completely or partially inside the input image.

Output = Single Statistic for All ROIs

Flag Port OutputDescription
0All ROIs are completely outside the input image.
1At least one ROI is completely or partially inside the input image.

If the ROI is partially outside the image, the block only computes the statistical values for the portion of the ROI that is within the image.

If, for the ROI type parameter, you select ```Label matrix```, the Output flag indicating if input label numbers are valid check box appears in the dialog box. If you select this check box, the Flag port appears on the block. The following tables describe the Flag port output based on the block parameters.

Output = Individual Statistics for Each ROI

Flag Port OutputDescription
0Label number is not in the label matrix.
1Label number is in the label matrix.

Output = Single Statistic for All ROIs

Flag Port OutputDescription
0None of the label numbers are in the label matrix.
1At least one of the label numbers is in the label matrix.

### Fixed-Point Data Types

The parameters on the Data Types pane of the block dialog are only used for fixed-point inputs. For purely real or purely imaginary inputs, the variance of the input is the square of its standard deviation. For complex inputs, the output is the sum of the variance of the real and imaginary parts of the input.

The following diagram shows the data types used within the Variance block for fixed-point signals. The results of the magnitude-squared calculations in the figure are in the product output data type. You can set the accumulator, product output, and output data types in the block dialog as discussed in Parameters.

## Parameters

Running variance

Enables running operation when selected.

Reset port

Specify the reset event that causes the block to reset the running variance. The sample time of the input to the Rst port must be a positive integer multiple of the input sample time. This parameter appears only when you select the Running variance check box. For more information, see Resetting the Running Variance

Find the variance value over

Specify whether to find the variance along rows, columns, entire input, or the dimension specified in the Dimension parameter. For more information, see Basic Operation.

Dimension

Specify the dimension (one-based value) of the input signal, over which the variance is computed. The value of this parameter cannot exceed the number of dimensions in the input signal. This parameter is only visible when the Find the variance value over parameter is set to `Specified dimension`.

Enable ROI Processing

Select this check box to calculate the statistical value within a particular region of each image. This parameter is only available when the Find the variance value over parameter is set to `Entire input`, and the block is not in running mode.

### 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 are limited to the ROI type `Rectangles`.

ROI type

Specify the type of ROI you want to use. Your choices are `Rectangles`, `Lines`, ```Label matrix```, or `Binary mask`.

ROI portion to process

Specify whether you want to calculate the statistical value for the entire ROI or just the ROI perimeter. This parameter is only visible if, for the ROI type parameter, you specify `Rectangles`.

Output

Specify the block output. The block can output a vector of separate statistical values for each ROI or a scalar value that represents the statistical value for all the specified ROIs. This parameter is not available if, for the ROI type parameter, you select `Binary mask`.

Output flag indicating if ROI is within image bounds

When you select this check box, a Flag port appears on the block. For a description of the Flag port output, see the tables in ROI Processing.

Output flag indicating if label numbers are valid

When you select this check box, a Flag port appears on the block. This check box is visible only when you select ```Label matrix``` for the ROI type parameter. For a description of the Flag port output, see the tables in ROI Processing.

Rounding mode

Select the Rounding Modes for fixed-point operations.

Overflow mode

Select the Overflow mode for fixed-point operations.

### Note

See Fixed-Point Data Types for more information on how the product output, accumulator, and output data types are used in this block.

Input-squared product

Use this parameter to specify how to designate the input-squared product word and fraction lengths:

• When you select `Same as input`, these characteristics match those of the input to the block.

• When you select `Binary point scaling`, you can enter the word length and the fraction length of the input-squared product, in bits.

• When you select `Slope and bias scaling`, you can enter the word length, in bits, and the slope of the input-squared product. This block requires power-of-two slope and a bias of zero.

Input-sum-squared product

Use this parameter to specify how to designate the input-sum-squared product word and fraction lengths:

• When you select ```Same as input-squared product```, these characteristics match those of the input-squared product.

• When you select `Binary point scaling`, you can enter the word length and the fraction length of the input-sum-squared product, in bits.

• When you select `Slope and bias scaling`, you can enter the word length, in bits, and the slope of the input-sum-squared product. This block requires power-of-two slope and a bias of zero.

Accumulator

Use this parameter to specify the accumulator word and fraction lengths resulting from a complex-complex multiplication in the block:

• When you select ```Same as input-squared product```, these characteristics match those of the input-squared product.

• When you select `Same as input`, these characteristics match those of the input to the block.

• When you select `Binary point scaling`, you can enter the word length and the fraction length of the accumulator, in bits.

• When you select `Slope and bias scaling`, you can enter the word length, in bits, and the slope of the accumulator. This block requires power-of-two slope and a bias of zero.

Output

Choose how you specify the output word length and fraction length:

• When you select `Same as accumulator`, these characteristics match those of the accumulator.

• When you select ```Same as input-squared product```, these characteristics match those of the input-squared product.

• When you select `Same as input`, these characteristics match those of the input to the block.

• When you select `Binary point scaling`, you can enter the word length and the fraction length of the output, in bits.

• When you select `Slope and bias scaling`, you can enter the word length, in bits, and the slope of the output. This block requires power-of-two slope and a bias of zero.

Lock data type settings against changes by the fixed-point tools

Select this parameter to prevent the fixed-point tools from overriding the data types you specify on the block mask.

## Example The ex_vision_2dvar calculates the variance value within two ROIs.

## See Also

 2-D Mean Computer Vision Toolbox 2-D Standard Deviation Computer Vision Toolbox `var` MATLAB

Download ebook