Compute 2-D cross-correlation of two input matrices

Statistics

`visionstatistics`

The 2-D Correlation block computes the two-dimensional cross-correlation
of two input matrices. Assume that matrix A has dimensions (*Ma*, *Na*)
and matrix B has dimensions (*Mb*, *Nb*).
When the block calculates the full output size, the equation for the
two-dimensional discrete cross-correlation is

$$C(i,j)={\displaystyle \sum _{m=0}^{(Ma-1)}{\displaystyle \sum _{n=0}^{(Na-1)}A(m,n)\cdot conj(B(m+i,n+j))}}$$

where $$0\le i<Ma+Mb-1$$ and $$0\le j<Na+Nb-1$$.

Port | Input/Output | Supported Data Types | Complex Values Supported |
---|---|---|---|

I1 | Vector or matrix of intensity values | Double-precision floating point Single-precision floating point Fixed point 8-, 16-, 32-bit signed integer 8-, 16-, 32-bit unsigned integer
| Yes |

I2 | Scalar, vector, or matrix of intensity values or a scalar, vector, or matrix that represents one plane of the RGB video stream | Same as I1 port | Yes |

Output | Convolution of the input matrices | Same as I1 port | Yes |

If the data type of the input is floating point, the output of the block is the same data type.

The dimensions of the output are dictated by the **Output size** parameter
and the sizes of the inputs at ports I1 and I2. For example, assume that the input at port I1
has dimensions (*Ma*, *Na*) and the input at port I2 has
dimensions (*Mb*, *Nb*). If, for the **Output
size** parameter, you choose `Full`

, the output is the
full two-dimensional cross-correlation with dimensions
(*Ma*+*Mb*-1,
*Na*+*Nb*-1). If, for the **Output size**
parameter, you choose `Same as input port I1`

, the output is the
central part of the cross-correlation with the same dimensions as the input at port I1. If,
for the **Output size** parameter, you choose
`Valid`

, the output is only those parts of the cross-correlation
that are computed without the zero-padded edges of any input. This output has dimensions
(Ma-Mb+1, Na-Nb+1). However,
if `all(size(I1)<size(I2))`

, the block errors out.

If you select the **Normalized output** check box, the block's output is
divided by
`sqrt(sum(dot(I1p,I1p))*sum(dot(I2,I2)))`

,
where `I1p`

is the portion of the I1 matrix that aligns with the I2 matrix. See Example 2 for
more information.

When you select the **Normalized output** check
box, the block input cannot be fixed point.

The following diagram shows the data types used in the 2-D Correlation block for fixed-point signals.

You can set the product output, accumulator, and output data types in the block mask as discussed in Parameters.

The output of the multiplier is in the product output data type if at least one of the inputs to the multiplier is real. If both of the inputs to the multiplier are complex, the result of the multiplication is in the accumulator data type. For details on the complex multiplication performed, refer to Multiplication Data Types.

Suppose I1, the first input matrix, has dimensions (4,3). I2, the second input matrix, has
dimensions (2,2). If, for the **Output size** parameter, you choose
`Full`

, the block uses the following equations to determine the number
of rows and columns of the output matrix:

The resulting matrix is

If, for the **Output size** parameter, you choose ```
Same as input
port I1
```

, the output is the central part of $$Cfull$$ with the same dimensions as the input at port I1, (4,3). However, since a
4-by-3 matrix cannot be extracted from the exact center of , the block leaves more rows and columns on the top and left side of the
matrix and outputs:

If, for the **Output size** parameter, you choose
`Valid`

, the block uses the following equations to determine the
number of rows and columns of the output matrix:

In this case, it is always possible to extract the exact center of . Therefore, the block outputs

In cross-correlation, the value of an output element is computed as a weighted sum of neighboring elements.

For example, suppose the first input matrix represents an image and is defined as

I1 = [17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9]

The second input matrix also represents an image and is defined as

I2 = [8 1 6 3 5 7 4 9 2]

The following figure shows how to compute the (2,4) output element (zero-based indexing) using these steps:

Slide the center element of I2 so that lies on top of the (1,3) element of I1.

Multiply each weight in I2 by the element of I1 underneath.

Sum the individual products from step 2.

The (2,4) output element from the cross-correlation is $$1\cdot 8+8\cdot 1+15\cdot 6+7\cdot 3+14\cdot 5+16\cdot 7+13\cdot 4+20\cdot 9+22\cdot 2=585$$.

**Computing the (2,4) Output of Cross-Correlation**

The normalized cross-correlation of the (2,4) output element
is `585/sqrt(sum(dot(I1p,I1p))*sum(dot(I2,I2))) `

= `0.8070`

,
where `I1p = [1 8 15; 7 14 16; 13 20 22]`

.

**Output size**This parameter controls the size of the output scalar, vector, or matrix produced as a result of the cross-correlation between the two inputs. If you choose

`Full`

, the output has dimensions (*Ma*+*Mb*-1,*Na*+*Nb*-1). If you choose`Same as input port I1`

, the output has the same dimensions as the input at port I1. If you choose`Valid`

, output has dimensions (*Ma*-*Mb*+1,*Na*-*Nb*+1).**Normalized output**If you select this check box, the block's output is normalized.

**Rounding mode**Select the Rounding Modes for fixed-point operations.

**Overflow mode**Select the Overflow mode for fixed-point operations.

**Product output**Specify the product output data type. See Fixed-Point Data Types and Multiplication Data Types for illustrations depicting the use of the product output data type in this block:

When you select

`Same as first input`

, these characteristics match those of the first input to the block.When you select

`Binary point scaling`

, you can enter the word length and the fraction length of the product output, in bits.When you select

`Slope and bias scaling`

, you can enter the word length, in bits, and the slope of the product output. The bias of all signals in the Computer Vision Toolbox™ software is 0.

The Product Output inherits its sign according to the inputs. If either or both input

**I1**and**I2**are signed, the Product Output will be signed. Otherwise, the Product Output is unsigned. The table below show all cases.Sign of Input I1 Sign of Input I2 Sign of Product Output unsigned unsigned unsigned unsigned signed signed signed unsigned signed signed signed signed **Accumulator**Use this parameter to specify how to designate the accumulator word and fraction lengths. Refer to Fixed-Point Data Types andMultiplication Data Types for illustrations depicting the use of the accumulator data type in this block. The accumulator data type is only used when both inputs to the multiplier are complex:

When you select

`Same as product output`

, these characteristics match those of the product output.When you select

`Same as first input`

, these characteristics match those of the first 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. The bias of all signals in the Computer Vision Toolbox software is 0.

**Output**Choose how to specify the word length and fraction length of the output of the block:

When you select

`Same as first input`

, these characteristics match those of the first 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. The bias of all signals in the Computer Vision Toolbox software is 0.

**Lock data type settings against change 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. For more information, see

`fxptdlg`

, a reference page on the Fixed-Point Tool in the Simulink^{®}documentation.

Computer Vision Toolbox | |

Computer Vision Toolbox | |

Computer Vision Toolbox | |

Computer Vision Toolbox | |

Computer Vision Toolbox | |

Computer Vision Toolbox | |

2-D Maximum | Computer Vision Toolbox |

2-D Minimum | Computer Vision Toolbox |