Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Calculate Covariance

I have a matrix (an image). And I want to calculate the covariance of the colors. However, there are sections of the matrix that are blank (-1,-1,-1) that I want to ignore in the covariance calculation. Currently, I am doing this process manually like so :

  1. iterate over every pixel: if not equal to (-1,-1,-1) subtract mean color (calculated during matrix generation), if equal to (-1,-1,-1) set to (0,0,0)

  2. Split Matrix into 3 color matrices

  3. Reshape each color matrices into column matrix

  4. Recombine column color matrices

  5. Multiply by transpose and divide by count - 1 (count does not include (-1,-1,-1) pixels)

I would like to add GPU (CUDA) support and use calcCovarMatrix() However I don't know if it is possible to get this function to perform this operation. Any help would be nice.

PS. Here is my Code

updateCovariance(cv::Mat matrix, cv::Mat covariance Vec3f averageColor, int count){
/*DEFINE VARIABLE*/
cv::Mat meanDifference(matrix.rows,matrix.cols,matrix.type());
cv::Mat covarinace

std::vector<cv::Mat> colors(3);
std::vector<cv::Mat> columnColor(3, cv::Mat(matrix.rows*matrix.cols,1,matrix.type()));

cv::Mat colorMat(matrix.rows*matrix.cols,3,matrix.type());
cv::Mat colorMatTrans(3,matrix.rows*matrix.cols,matrix.type());

/*RUN CODE*/
for(int y = 0; y < meanDifference.rows; ++y)
{
    for (int x=0; x<meanDifference.cols; ++x)
    {
        if (matrix.at<cv::Vec3f>(y,x) != cv::Vec3f(-1,-1,-1))
        {
            meanDifference.at<cv::Vec3f>(y,x) = matrix.at<cv::Vec3f>(y,x)-averageColor;
        }
        else
        {
            meanDifference.at<cv::Vec3f>(y,x) = cv::Vec3f(0,0,0);
        }
    }
}
split(meanDifference,colors);

//Reshape Color Matrices into column arrays
for(int i=0;i < colors.size();++i)
{
    columnColor[i] = colors[i].reshape(1,1);
}

//Recombine color matrices and transpose
vconcat(columnColor,colorMat);

transpose(colorMat,colorMatTrans);

covariance = (colorMat * colorMatTrans)/(count-1);
/*CLEAN UP*/
/*RETURN*/
}

Calculate Covariance

I have a matrix (an image). And I want to calculate the covariance of the colors. However, there are sections of the matrix that are blank (-1,-1,-1) that I want to ignore in the covariance calculation. Currently, I am doing this process manually like so :

  1. iterate over every pixel: if not equal to (-1,-1,-1) subtract mean color (calculated during matrix generation), if equal to (-1,-1,-1) set to (0,0,0)

  2. Split Matrix into 3 color matrices

  3. Reshape each color matrices into column matrix

  4. Recombine column color matrices

  5. Multiply by transpose and divide by count - 1 (count does not include (-1,-1,-1) pixels)

I would like to add GPU (CUDA) support and use calcCovarMatrix() However I don't know if it is possible to get this function to perform this operation. Any help would be nice.

PS. Here is my Code

updateCovariance(cv::Mat matrix, cv::Mat covariance Vec3f averageColor, int count){
/*DEFINE VARIABLE*/
cv::Mat meanDifference(matrix.rows,matrix.cols,matrix.type());
cv::Mat covarinace

std::vector<cv::Mat> colors(3);
std::vector<cv::Mat> columnColor(3, cv::Mat(matrix.rows*matrix.cols,1,matrix.type()));

cv::Mat colorMat(matrix.rows*matrix.cols,3,matrix.type());
cv::Mat colorMatTrans(3,matrix.rows*matrix.cols,matrix.type());

/*RUN CODE*/
for(int y = 0; y < meanDifference.rows; ++y)
{
    for (int x=0; x<meanDifference.cols; ++x)
    {
        if (matrix.at<cv::Vec3f>(y,x) != cv::Vec3f(-1,-1,-1))
        {
            meanDifference.at<cv::Vec3f>(y,x) = matrix.at<cv::Vec3f>(y,x)-averageColor;
        }
        else
        {
            meanDifference.at<cv::Vec3f>(y,x) = cv::Vec3f(0,0,0);
        }
    }
}
split(meanDifference,colors);

//Reshape Color Matrices into column arrays
for(int i=0;i < colors.size();++i)
{
    columnColor[i] = colors[i].reshape(1,1);
}

//Recombine color matrices and transpose
vconcat(columnColor,colorMat);

transpose(colorMat,colorMatTrans);

covariance = (colorMat * colorMatTrans)/(count-1);
/*CLEAN UP*/
/*RETURN*/
}