Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column sum of Opencv Matrix elements

Tags:

opencv

matrix

sum

I need to compute sum of elements in all columns separately.

Now I'm using:

Matrix cross_corr should be summed.

Mat cross_corr_summed;
for (int i=0;i<cross_corr.cols;i++)                                                     
    {
        double column_sum=0;
        for (int k=0;k<cross_corr.rows;k++)
        {
            column_sum +=cross_corr.at<float>(k,i);             
        }
        cross_corr_summed.push_back(column_sum);
    }

The problem is that my program takes quite a long time to run. This is one of parts that is suspicious to cause this. Can you advise any possible faster implementation???

Thanks!!!

like image 928
Robert Kirchhoff Avatar asked Oct 11 '12 06:10

Robert Kirchhoff


2 Answers

You need a cv::reduce:

cv::reduce(cross_corr, cross_corr_summed, 0, CV_REDUCE_SUM, CV_32S);
like image 101
Andrey Kamaev Avatar answered Oct 12 '22 23:10

Andrey Kamaev


If you know that your data is continuous and single-channeled, you can access the matrix data directly:

int width = cross_corr.cols;
float* data = (float*)cross_corr.data;
Mat cross_corr_summed;
for (int i=0;i<cross_corr.cols;i++)
{
    double column_sum=0;
    for (int k=0;k<cross_corr.rows;k++)
    {
        column_sum += data[i + k*width];
    }
    cross_corr_summed.push_back(column_sum);
}

which will be faster than your use of .at_<float>(). In general I avoid the use of .at() whenever possible because it is slower than direct access.

Also, although cv::reduce() (suggested by Andrey) is much more readable, I have found it is slower than even your implementation in some cases.

like image 40
Aurelius Avatar answered Oct 13 '22 01:10

Aurelius