
is convenient to talk about the covariance matrix , whose components include all of the covariances
between the variables as well as the variances of the variables individually. As can be seen from the
formula above, the covariance matrix is symmetric, i.e.,
.
In a much earlier chapter, we encountered a function which could be used when dealing with individual
vectors of data (as opposed to whole arrays of individual vectors). That function was
cv::calcCovarMatrix(), which will allow us to provide
N
vectors of dimension
d
and it will spit out
the
d
-by-
d
covariance matrix. Our problem now, however, is that we would like to compute such a matrix
for every point in an array (or at least, in the case of a 3-dimensional RGB image, we would like to
compute the 6 unique entries in that matrix).
In practice, the best way to do this is simply to compute the variances using the code we already developed,
and to compute the three new objects (the off-diagonal elements of
) separately. Looking at the
formula for the covariance, we see that cv::accumulateSquare() will not quite work here, as we
need to accumulate the
terms (i.e., the product of two different channel values from a particular
pixel in each image).
The function which does this for us in OpenCV is cv::accumulateProduct().
void accumulateProduct(
cv::InputArray src1, // Input, 1 or 3 channels, U8 or F32
cv::InputArray src2,