OpenCV: Getting the total of Mat values

2020-08-12 04:14发布

问题:

Is there some openCV function that I can pass in a cv::Mat and get the sum of all values in them?

For example: int cvSumFoo(Mat &srcMat); I'm expecting an int to come back

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.

回答1:

The function 'sum' "calculates and returns the sum of array elements, independently for each channel."

You can find the information here: http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#sum



回答2:

I know that the questioner didn't ask for the function in Java-openCV, but I still want to show how to do it in Java, because:

  1. the "sum"-function in Java is named totally different and is in a different class
  2. Java openCV API examples are sparse

The code for printing a sum in Java openCV is:

Core.sumElems(myMat);

Obvious is that, it is not a function of the "Scalar"-class and its name is not sum().



标签: c++ opencv