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!!!
You need a cv::reduce:
If you know that your data is continuous and single-channeled, you can access the matrix data directly:
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.