Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV optimisation - multiplying a set of matrices by a set of scalars and summing.

I have an optimisation problem and I'm wondering what the best way to approach the problem is.

At present I have a set of matrices (mats) that I need to scale by a set of values held in a vector and then summed together. I have written the following code nut it seems to be pretty painfully slow (far more so than I would have thought).

cv::Mat sum = cv::Mat::zeros( mats[0].rows, mats[0].cols, cvType );
for( int m = 0; m < mats.size(); m++ )
{
     const Type val = rowVec.at< Type >( m );
     sum += val * mats[m];
}

Can anyone suggest a faster way of doing the above loop?

OpenCV optimisation - multiplying a set of matrices by a set of scalars and summing.

I have an optimisation problem and I'm wondering what the best way to approach the problem is.

At present I have a set of matrices (mats) that I need to scale by a set of values held in a vector and then summed together. I have written the following code nut it seems to be pretty painfully slow (far more so than I would have thought).

cv::Mat sum = cv::Mat::zeros( mats[0].rows, mats[0].cols, cvType );
for( int m = 0; m < mats.size(); m++ )
{
     const Type val = rowVec.at< Type >( m );
     sum += val * mats[m];
}

Can anyone suggest a faster way of doing the above loop?

Edit:

I wrote a little function to try and aid my performance:

template< typename Type >
void ScaleMatAndSum( cv::Mat& scale, const cv::Mat& mats, const Type val )
{
    for( int r = 0; r < mats.rows; r++ )
    {
        for( int c = 0; c < mats.cols; c++ )
        {
            scale.at< Type >( r, c )    += mats.at< Type >( r, c ) * val;
        }
    }
}

This is about 8 times faster when multi-threaded than the original code. Can anyone explain what is going on?