一、矩阵的求和函数

函数: reduce();
官方文档:

Reduces a matrix to a vector.

C++: void reduce(InputArray src, OutputArray dst, int dim, int rtype, int dtype=-1 )¶
Python: cv2.reduce(src, dim, rtype[, dst[, dtype]]) → dst
C: void cvReduce(const CvArr* src, CvArr* dst, int dim=-1, int op=CV_REDUCE_SUM)
Python: cv.Reduce(src, dst, dim=-1, op=CV_REDUCE_SUM) → None
Parameters:
  • src – input 2D matrix.
  • dst – output vector. Its size and type is defined by dim and dtype parameters.
  • dim – dimension index along which the matrix is reduced. 0 means that the matrix is reduced to a single row. 1 means that the matrix is reduced to a single column.
  • rtype –

    reduction operation that could be one of the following:

    • CV_REDUCE_SUM: the output is the sum of all rows/columns of the matrix.
    • CV_REDUCE_AVG: the output is the mean vector of all rows/columns of the matrix.
    • CV_REDUCE_MAX: the output is the maximum (column/row-wise) of all rows/columns of the matrix.
    • CV_REDUCE_MIN: the output is the minimum (column/row-wise) of all rows/columns of the matrix.
  • dtype – when negative, the output vector will have the same type as the input matrix, otherwise, its type will be CV_MAKE_TYPE(CV_MAT_DEPTH(dtype), src.channels()).

The function reduce reduces the matrix to a vector by treating the matrix rows/columns as a set of 1D vectors and performing the specified operation on the vectors until a single row/column is obtained. For example, the function can be used to compute horizontal and vertical projections of a raster image. In case of CV_REDUCE_SUM and CV_REDUCE_AVG , the output may have a larger element bit-depth to preserve accuracy. And multi-channel arrays are also supported in these two reduction modes.

总结一句话:参数的选择要对应

示例:

double a[5][4] ={{ 4, 0, 2, 5 },{ 1, 1, 0, 7 },{ 0, 5, 2, 0 },{ 0, 3, 4, 0 },{ 8, 0, 1, 2 }};Mat ma(5, 4, CV_64FC1, a);Mat mb(5, 1, CV_64FC1, Scalar(0));Mat mc(1, 4, CV_64FC1, Scalar(0)); cout << "原矩阵:" << endl;cout << ma << endl;reduce(ma, mb, 1, CV_REDUCE_SUM);cout << "列向量" << endl;cout << mb << endl;reduce(ma, mc, 0, CV_REDUCE_SUM);cout << "行向量" << endl;cout << mc << endl;

参考的文档:http://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#reduce

Tips: 异常信息提示

cv::reduce for SUM and 8U input can return only 32S, 32F or 64F types. 

所以在进行计算的时候一定要注意矩阵的数据类型,不然会出现以下错误提示信息:

cv::reduce gives unsupported format exception

就像下面的代码,如果将 double 改成了uchar ,ma(....,CV_8UC1,...) mb, mc 同样,那么计算reduce的时候就会抛出异常了。
   uchar matrix[5][6] = {  { 1, 2, 3, 4, 5, 6 }, { 7, 8, 9, 10, 11, 12 }, 
                { 13, 14, 15, 16, 17, 18 }, { 19, 20, 21, 22, 23, 24 }, { 25, 26, 27, 28, 29, 30 } };Mat matr(Size(6, 5), CV_8UC1, matrix);Mat mb(5, 1, CV_8UC1, Scalar(0));Mat mc(1, 6, CV_8UC1, Scalar(0));reduce(matr, mb, 1, CV_REDUCE_SUM);cout << "列向量" << endl;cout << mb << endl;

参考链接:

http://answers.opencv.org/question/3698/cvreduce-gives-unsupported-format-exception/

// Mark一下,各种操作函数待续

opencv 矩阵行列求和相关推荐

  1. opencv 矩阵相乘

    opencv 矩阵乘法 1. dot说明: 2. Mat矩阵mul--A.mul(B) 3. opencv将整数像素图片转化为浮点型 1. dot说明: 1. 对两个向量执行点乘运算,就是对这两个向量 ...

  2. OpenCV 矩阵常用操作,比如 addWeighted, flip, hconcat, reduce, merge, norm, repeat, split, sort, mulSpectrum 等

    平时经常会用到一些 C++ OpenCV 矩阵 Mat 常用的函数,每次用到都要到官网去查看,现在特地整理了一下. 下面的函数均来自于 https://docs.opencv.org/master/d ...

  3. OpenCV—矩阵数据类型转换cv::convertTo

    OpenCV-矩阵数据类型转换cv::convertTo 函数 [cpp] view plaincopy void convertTo( OutputArray m, int rtype, doubl ...

  4. matlab 矩阵元素求和、求均值(期望)和均方差

    matlab中矩阵元素求和.求期望和均方差 在matlab中求一个矩阵中元素的和可以自己编写for循环来完成,这样比较方便,想求那些数据的和都可以做到,然而效率比较低,如果数据量大程序会跑好长时间.所 ...

  5. Oracle对话框列间距太近,如何调整MathType矩阵行列间距

    矩阵在数学中也是非常常见一种,尤其是线性代数中,基本都是矩阵与行列式的天下,在编辑矩阵与行列式时都是使用MathType矩阵模板来进行编辑的,我们在用MathType编辑矩阵的的时候,有时会觉得矩阵的 ...

  6. excel高效快捷键行列求和、向右填充

    一.excel行列求和,快捷键:ALT+= 1.新建excel如下图: 2.选中要累加的单元格,如下图:也就是除数值以外,多选一行,多选一列. 3.选中后,使用快捷键alt+=即可行列分别求和.如下图 ...

  7. 已知一个二维数组A 表示一个矩阵,求AT。 其中,AT 表示矩阵的转置。矩阵转置的含义:表示把一个矩阵行列互换。

    已知一个二维数组A 表示一个矩阵,求AT. 其中,AT 表示矩阵的转置.矩阵转置的含义:表示把一个矩阵行列互换. //传入需要验证的数组,在main里面调用该方法public static void ...

  8. matlab中sum函数对矩阵的求和总结

    matlab中sum函数对矩阵的求和总结 A= [1, 2 ,3 ,4, 5:     1, 2, 3, 4, 5]: a=sum(A)  %对整个矩阵按列求和 >>a= [2 4 6 8 ...

  9. hdu 5411 矩阵幂求和

    碧云天,黄叶地,秋色连波,波上寒烟翠. 山映斜阳天接水,芳草无情,更在斜阳外. 黯乡魂,追旅思,夜夜除非,好梦留人睡. 明月楼高休独倚,酒入愁肠,化作相思泪. -----范仲淹<苏幕遮·碧云天& ...

最新文章

  1. Spring Boot 无侵入式 实现 API 接口统一 JSON 格式返回
  2. 我的世界java路径_我的世界java路径在哪 路径有误怎么设置
  3. Bzoj2762: [JLOI2011]不等式组
  4. hdu1.3.2 Rank
  5. 【一周入门MySQL—2】单表查询
  6. 网络编程释疑之:TCP连接拔掉网线后会发生什么
  7. 漫谈ElasticSearch关于ES性能调优几件必须知道的事(转)
  8. 900款工作岗位竞聘PPT模板免费下载
  9. linux 进程 线程 优先级,Linux编程-线程优先级的设定
  10. 警惕!关于5G的最新骗局!
  11. 基于华为云服务的人脸识别功能实现
  12. 英语六级 Java_过英语六级算什么,你过了Java25级了吗!
  13. Cheat Engine(CE)的下载和安装指南以及相关教程
  14. C/C++ 八股文(二)
  15. anywebp jpg png 转换webp格式图片 免费在线转换
  16. Android中删除EditText中内容时报SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
  17. Android判断CPU是32位还是64位
  18. linux CR2 to JPG
  19. 收银软件如何实现收银一体化
  20. ArrayList的remove()方法解读

热门文章

  1. ElasticSearch入门教程(1)
  2. 背包问题变种:将数组分成两部分使得两部分的和的差最小
  3. 渗透测试之突破口——web服务突破
  4. android createbitmap设置背景色,Android createBitmap截取实现移动的游戏背景
  5. 炫酷的生日快乐网页 【附带源码】
  6. MySQL入门学习教程
  7. 第六章微型计算机,微型计算机原理及应用 第六章微型计算机的接口技术
  8. C语言 | 九九乘法表
  9. 企企通创始人徐辉:连通 跨界 赋能 创新,构建一个企业互联的网状生态
  10. 群体创新技术/群体决策的几种类型