Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum of elements in a matrix in OpenCV?

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.

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

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

like image 889
kadu Avatar asked Feb 19 '14 08:02

kadu


2 Answers

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];
like image 169
Shai Avatar answered Nov 12 '22 14:11

Shai


In addition with @Shai you can use;

double sum = cv::sum(A).val(0);
like image 11
eiki Avatar answered Nov 12 '22 13:11

eiki