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?
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];
In addition with @Shai you can use;
double sum = cv::sum(A).val(0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With