二,reduce() 的使用陷阱。

函数原型:void cv::reduce(InputArray _src, OutputArray _dst, int dim, int op, int dtype=-1)

对dtype采用默认值方式使用函数reduce(),出现了点异常问题,然后追加try-catch,发现输入输出的数据类型不匹配,于是就结合着reduce() 原代码做进一步分析,先是尝试着将输出创建为各种指定的格式,不行!
原因在于对下面这条语句没有理解好:

if( dtype < 0 )
            dtype = _dst.fixedType() ? _dst.type() : stype;

上网、上Q,折腾许久,终于想到了要把
dtype指定一个初值! 由于输入的数据类型是8U,对于求和操作CV_REDUCE_SUM,那么输出是32S就可以,对应的
dtype=CV_32S 就行,此时输出的矩阵也就只需要定义一下就行,不必再进行其它操作。例如:

Mat matIn=imread(''lena.jpg",0);

Mat matOut;

reduce(matIn, matOut, 1, CV_REDUCE_SUM, CV_32S);

以下为函数reduce()
源码,方便对照学习。

void cv::reduce(InputArray _src, OutputArray _dst, int dim, int op, int dtype)
{Mat src = _src.getMat();CV_Assert( src.dims <= 2 );int op0 = op;int stype = src.type(), sdepth = src.depth(), cn = src.channels();if( dtype < 0 )dtype = _dst.fixedType() ? _dst.type() : stype;int ddepth = CV_MAT_DEPTH(dtype);_dst.create(dim == 0 ? 1 : src.rows, dim == 0 ? src.cols : 1,CV_MAKETYPE(dtype >= 0 ? dtype : stype, cn));Mat dst = _dst.getMat(), temp = dst;CV_Assert( op == CV_REDUCE_SUM || op == CV_REDUCE_MAX ||op == CV_REDUCE_MIN || op == CV_REDUCE_AVG );CV_Assert( src.channels() == dst.channels() );if( op == CV_REDUCE_AVG ){op = CV_REDUCE_SUM;if( sdepth < CV_32S && ddepth < CV_32S ){temp.create(dst.rows, dst.cols, CV_32SC(cn));ddepth = CV_32S;}}ReduceFunc func = 0;if( dim == 0 ){if( op == CV_REDUCE_SUM ){if(sdepth == CV_8U && ddepth == CV_32S)func = reduceR_<uchar,int,OpAdd<int> >;else if(sdepth == CV_8U && ddepth == CV_32F)func = reduceR_<uchar,float,OpAdd<int> >;else if(sdepth == CV_8U && ddepth == CV_64F)func = reduceR_<uchar,double,OpAdd<int> >;else if(sdepth == CV_16U && ddepth == CV_32F)func = reduceR_<ushort,float,OpAdd<float> >;else if(sdepth == CV_16U && ddepth == CV_64F)func = reduceR_<ushort,double,OpAdd<double> >;else if(sdepth == CV_16S && ddepth == CV_32F)func = reduceR_<short,float,OpAdd<float> >;else if(sdepth == CV_16S && ddepth == CV_64F)func = reduceR_<short,double,OpAdd<double> >;else if(sdepth == CV_32F && ddepth == CV_32F)func = reduceR_<float,float,OpAdd<float> >;else if(sdepth == CV_32F && ddepth == CV_64F)func = reduceR_<float,double,OpAdd<double> >;else if(sdepth == CV_64F && ddepth == CV_64F)func = reduceR_<double,double,OpAdd<double> >;}else if(op == CV_REDUCE_MAX){if(sdepth == CV_8U && ddepth == CV_8U)func = reduceR_<uchar, uchar, OpMax<uchar> >;else if(sdepth == CV_16U && ddepth == CV_16U)func = reduceR_<ushort, ushort, OpMax<ushort> >;else if(sdepth == CV_16S && ddepth == CV_16S)func = reduceR_<short, short, OpMax<short> >;else if(sdepth == CV_32F && ddepth == CV_32F)func = reduceR_<float, float, OpMax<float> >;else if(sdepth == CV_64F && ddepth == CV_64F)func = reduceR_<double, double, OpMax<double> >;}else if(op == CV_REDUCE_MIN){if(sdepth == CV_8U && ddepth == CV_8U)func = reduceR_<uchar, uchar, OpMin<uchar> >;else if(sdepth == CV_16U && ddepth == CV_16U)func = reduceR_<ushort, ushort, OpMin<ushort> >;else if(sdepth == CV_16S && ddepth == CV_16S)func = reduceR_<short, short, OpMin<short> >;else if(sdepth == CV_32F && ddepth == CV_32F)func = reduceR_<float, float, OpMin<float> >;else if(sdepth == CV_64F && ddepth == CV_64F)func = reduceR_<double, double, OpMin<double> >;}}else{if(op == CV_REDUCE_SUM){if(sdepth == CV_8U && ddepth == CV_32S)func = reduceC_<uchar,int,OpAdd<int> >;else if(sdepth == CV_8U && ddepth == CV_32F)func = reduceC_<uchar,float,OpAdd<int> >;else if(sdepth == CV_8U && ddepth == CV_64F)func = reduceC_<uchar,double,OpAdd<int> >;else if(sdepth == CV_16U && ddepth == CV_32F)func = reduceC_<ushort,float,OpAdd<float> >;else if(sdepth == CV_16U && ddepth == CV_64F)func = reduceC_<ushort,double,OpAdd<double> >;else if(sdepth == CV_16S && ddepth == CV_32F)func = reduceC_<short,float,OpAdd<float> >;else if(sdepth == CV_16S && ddepth == CV_64F)func = reduceC_<short,double,OpAdd<double> >;else if(sdepth == CV_32F && ddepth == CV_32F)func = reduceC_<float,float,OpAdd<float> >;else if(sdepth == CV_32F && ddepth == CV_64F)func = reduceC_<float,double,OpAdd<double> >;else if(sdepth == CV_64F && ddepth == CV_64F)func = reduceC_<double,double,OpAdd<double> >;}else if(op == CV_REDUCE_MAX){if(sdepth == CV_8U && ddepth == CV_8U)func = reduceC_<uchar, uchar, OpMax<uchar> >;else if(sdepth == CV_16U && ddepth == CV_16U)func = reduceC_<ushort, ushort, OpMax<ushort> >;else if(sdepth == CV_16S && ddepth == CV_16S)func = reduceC_<short, short, OpMax<short> >;else if(sdepth == CV_32F && ddepth == CV_32F)func = reduceC_<float, float, OpMax<float> >;else if(sdepth == CV_64F && ddepth == CV_64F)func = reduceC_<double, double, OpMax<double> >;}else if(op == CV_REDUCE_MIN){if(sdepth == CV_8U && ddepth == CV_8U)func = reduceC_<uchar, uchar, OpMin<uchar> >;else if(sdepth == CV_16U && ddepth == CV_16U)func = reduceC_<ushort, ushort, OpMin<ushort> >;else if(sdepth == CV_16S && ddepth == CV_16S)func = reduceC_<short, short, OpMin<short> >;else if(sdepth == CV_32F && ddepth == CV_32F)func = reduceC_<float, float, OpMin<float> >;else if(sdepth == CV_64F && ddepth == CV_64F)func = reduceC_<double, double, OpMin<double> >;}}if( !func )CV_Error( CV_StsUnsupportedFormat,"Unsupported combination of input and output array formats" );func( src, temp );if( op0 == CV_REDUCE_AVG )temp.convertTo(dst, dst.type(), 1./(dim == 0 ? src.rows : src.cols));
}

opencv reduce函数相关推荐

  1. Python3不存在reduce函数

    原文: https://blog.csdn.net/nigelyq/article/details/79283014 原因 翻阅原文:http://www.artima.com/forums/flat ...

  2. opencv Mat 函数--CheckVector

    学习opencv ,我们需要对opencv的函数有点了解 ,实现什么样子的功能,以及如何实现 下面我们一次来看checkVector 这个函数, int org.opencv.core.Mat.che ...

  3. Python join sorted sort map reduce 函数解析

    为什么80%的码农都做不了架构师?>>>    1.字符串转list (list) s = 'abcde'     print list(s) ['a', 'b', 'c', 'd' ...

  4. matlab中imresize函数的用法,为何 MATLAB imresize 函数和 OpenCV resize 函数结果不同

    为何 MATLAB imresize 函数和 OpenCV resize 函数结果不同?今年 4 月,我在依照 MATLAB 代码自己写一个卷积神经网络 C++ 实现的过程中,就发现了这个问题,不过那 ...

  5. OpenCV findContours函数参数

    目录 OpenCV findContours函数参数 python检测外轮廓: c++轮廓检测: 一.mode取值"CV_RETR_EXTERNAL",method取值" ...

  6. Python中的map()函数和reduce()函数的用法

    Python中的map()函数和reduce()函数的用法 这篇文章主要介绍了Python中的map()函数和reduce()函数的用法,代码基于Python2.x版本,需要的朋友可以参考下  

  7. 利用OpenCV的函数cvtcolor()进行颜色空间转换时需要注意的地方

    OpenCV的函数cvtcolor()的原型如下: C++原型: void cv::cvtColor(InputArray src,OutputArray dst,int code,int dstCn ...

  8. python reduce()函数

    欢迎关注本人博客:云端筑梦师 描述 reduce() 函数会对参数序列中元素进行累积.函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给reduce中的函数 function(有两个参 ...

  9. Python编程基础:第五十七节 reduce函数Reduce

    第五十七节 reduce函数Reduce 前言 实践 前言 reduce函数的作用是逐项将迭代器的前两项按照指定方式进行计算,例如我们有一个数字列表[1, 2, 3, 4]我们可以使用reduce函数 ...

最新文章

  1. python实现文件搜索_python实现搜索指定目录下文件及文件内搜索指定关键词的方法...
  2. AndroidStudio(7)---导入jar包方法
  3. leetcode题解48-旋转图像
  4. 微型计算机8位数据总线,微机原理答案1
  5. 《统一沟通-微软-实战》-6-部署-2-中介服务器-2-安装中介服务器
  6. c语言头文件下载微盘,c语言头文件下载 C语言头文件大全.doc
  7. 常见的编程语言的特点与应用领域浅谈
  8. 内核获取网络设备的网桥接口
  9. 用计算机画图截图图片,电脑怎么截图
  10. MIPS汇编指令翻译机器码
  11. Oracle VM VirtualBox不能正常运行(电脑强行关机后,Linux不能正常运行)
  12. [BZOJ3240][Noi2013]矩阵游戏 快速幂
  13. 休闲零食生产企业如何做好供应链管理?
  14. LANDESK8.8版本操作说明书
  15. 手机互联网的三个致命缺陷
  16. CSS选择器(select)
  17. 如何去区分IP地址的网络位和主机位?
  18. 负荷分配问题的动态规划算法递归实现
  19. 机器学习数学基础:线代(3)
  20. 反编译并修复过的手写识别类--(android 搜狗 libhanwonhw_v15)

热门文章

  1. python性能优化的一些建议(一)
  2. window 2008 32位系统安装oracle 10g数据库,Oracle 10g for Windows 32bit安装图解-数据库专栏,ORACLE...
  3. tibco_TIBCO产品的微服务和DevOps
  4. matlab中PDE工具箱如何使用,使用PDE工具箱求解偏微分方程
  5. php配置设置,PHP设置配置文件的方法
  6. 用Spring Cloud Alibaba开发微服务会更香吗?
  7. 皮一皮:这是仙女用的嘛?
  8. 厉害了,程序员的高考试卷,你能拿几分?
  9. 深度学习手势识别带你玩转神庙逃亡
  10. oracle数据库作业1,北京语言20秋《Oracle数据库开发》作业1(100分)