OpenCV中矩阵中元素的总和? [英] Sum of elements in a matrix in OpenCV?

查看:2392
本文介绍了OpenCV中矩阵中元素的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将矩阵中的所有元素求和。我使用函数

I need to sum all the elements in a matrix. I used the function

  sum(sum(A)); 

其中 A 是大小为300 * 360的矩阵。
我想在OpenCV中实现相同的功能。我使用这样的东西。

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?

推荐答案

不同于Matlab,在opencv中, cv :: sum 沿所有维度求和并返回等于Matlab的 sum(sum(A))的单个数字(标量)。

所以,你需要的是

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];

这篇关于OpenCV中矩阵中元素的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆