C++ OpenCV中矩阵中元素的总和?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21874774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sum of elements in a matrix in OpenCV?
提问by kadu
I need to sum all the elements in a matrix. I used the function
我需要对矩阵中的所有元素求和。我用的功能
sum(sum(A));
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.
在matlab中。其中A
是大小为 300*360 的矩阵。我想在 OpenCV 中实现相同的功能。我用过这样的东西。
double s=cv::sum(cv::sum(A));
But there is error showing cannot convert scalar to double. How to fix this problem?
但是有错误显示无法将标量转换为双精度。如何解决这个问题?
回答by Shai
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
与 Matlab 不同,在 opencv 中,cv::sum(A)
沿所有维度求和并返回一个等于 Matlab 的sum(sum(A))
.
所以,你需要的是
double s = cv::sum( A )[0];