OpenCV中矩阵中的元素总和?

我需要对矩阵中的所有元素求和.我使用了函数

I need to sum all the elements in a matrix. I used the function

  sum(sum(A)); 

在matlab中.其中 A 是一个大小为 300*360 的矩阵.我想在 OpenCV 中实现相同的功能.我用过这样的东西.

in matlab. Where A is a matrix of size 300*360. I want to implement the same function in OpenCV. I used something like this.

  double s=cv::sum(cv::sum(A));

但显示无法将标量转换为双精度的错误.如何解决这个问题?

But there is error showing cannot convert scalar to double. How to fix this problem?

推荐答案

与 Matlab 不同,在 opencv 中,cv::sum(A) 沿所有维度求和并返回单个数字(标量)等于Matlab的sum(sum(A)).
所以,你需要的是

Unlike Matlab, in opencv, cv::sum(A) sums along ALL dimensions and returns a single number (scalar) that is equal to Matlab's sum(sum(A)).
So, what you need is

double s = cv::sum(A)[0];

相关文章