If you know that your data is continuous and single-channel, you can directly access matrix data:
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 using .at_<float>() . In general, I avoid using .at() whenever possible, because it is slower than direct access.
In addition, although cv::reduce() (proposed by Andrey) is much more readable, I found that it is slower than even your implementation in some cases.
Aurelius
source share