C++ OpenCV:获取 Mat 值的总和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10156044/
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
OpenCV: Getting the total of Mat values
提问by roboto1986
Is there some openCV function that I can pass in a cv::Mat
and get the sum of all values in them?
是否有一些 openCV 函数可以传入 acv::Mat
并获取其中所有值的总和?
For example: int cvSumFoo(Mat &srcMat)
; I'm expecting an int to come back
例如:int cvSumFoo(Mat &srcMat)
; 我期待 int 回来
I create it like this:
我像这样创建它:
srcMat= new Mat(rows, cols, CV_8U);
I would like to avoid creating my own loop if at all possible.
如果可能,我想避免创建自己的循环。
回答by dennisg
The function 'sum' "calculates and returns the sum of array elements, independently for each channel."
函数“sum”“独立地为每个通道计算并返回数组元素的总和。”
You can find the information here: http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#sum
您可以在此处找到信息:http: //docs.opencv.org/modules/core/doc/operations_on_arrays.html#sum
回答by kiltek
I know that the questioner didn't askfor the function in Java-openCV, but I still want to show how to do it in Java, because:
我知道提问者没有要求Java-openCV 中的函数,但我仍然想展示如何在 Java 中做到这一点,因为:
- the "sum"-function in Java is named totally different and is in a differentclass
- Java openCV API examples are sparse
- Java 中的“sum”函数命名完全不同并且在不同的类中
- Java openCV API 示例很少
The code for printing a sum in Java openCV is:
在 Java openCV 中打印总和的代码是:
Core.sumElems(myMat);
Obvious is that, it is nota function of the "Scalar"-class and its name is notsum()
.
显然,它不是“标量”类的函数,它的名字也不是sum()
.