opencv reduce函数

举报
风吹稻花香 发表于 2021/06/05 01:54:33 2021/06/05
【摘要】 二,reduce() 的使用陷阱。        函数原型:void cv::reduce(InputArray _src, OutputArray _dst, int dim, int op, int dtype=-1)        对dtype采用...
二,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()
源码,方便对照学习。

 

 


  
  1. void cv::reduce(InputArray _src, OutputArray _dst, int dim, int op, int dtype)
  2. {
  3. Mat src = _src.getMat();
  4. CV_Assert( src.dims <= 2 );
  5. int op0 = op;
  6. int stype = src.type(), sdepth = src.depth(), cn = src.channels();
  7. if( dtype < 0 )
  8. dtype = _dst.fixedType() ? _dst.type() : stype;
  9. int ddepth = CV_MAT_DEPTH(dtype);
  10. _dst.create(dim == 0 ? 1 : src.rows, dim == 0 ? src.cols : 1,
  11. CV_MAKETYPE(dtype >= 0 ? dtype : stype, cn));
  12. Mat dst = _dst.getMat(), temp = dst;
  13. CV_Assert( op == CV_REDUCE_SUM || op == CV_REDUCE_MAX ||
  14. op == CV_REDUCE_MIN || op == CV_REDUCE_AVG );
  15. CV_Assert( src.channels() == dst.channels() );
  16. if( op == CV_REDUCE_AVG )
  17. {
  18. op = CV_REDUCE_SUM;
  19. if( sdepth < CV_32S && ddepth < CV_32S )
  20. {
  21. temp.create(dst.rows, dst.cols, CV_32SC(cn));
  22. ddepth = CV_32S;
  23. }
  24. }
  25. ReduceFunc func = 0;
  26. if( dim == 0 )
  27. {
  28. if( op == CV_REDUCE_SUM )
  29. {
  30. if(sdepth == CV_8U && ddepth == CV_32S)
  31. func = reduceR_<uchar,int,OpAdd<int> >;
  32. else if(sdepth == CV_8U && ddepth == CV_32F)
  33. func = reduceR_<uchar,float,OpAdd<int> >;
  34. else if(sdepth == CV_8U && ddepth == CV_64F)
  35. func = reduceR_<uchar,double,OpAdd<int> >;
  36. else if(sdepth == CV_16U && ddepth == CV_32F)
  37. func = reduceR_<ushort,float,OpAdd<float> >;
  38. else if(sdepth == CV_16U && ddepth == CV_64F)
  39. func = reduceR_<ushort,double,OpAdd<double> >;
  40. else if(sdepth == CV_16S && ddepth == CV_32F)
  41. func = reduceR_<short,float,OpAdd<float> >;
  42. else if(sdepth == CV_16S && ddepth == CV_64F)
  43. func = reduceR_<short,double,OpAdd<double> >;
  44. else if(sdepth == CV_32F && ddepth == CV_32F)
  45. func = reduceR_<float,float,OpAdd<float> >;
  46. else if(sdepth == CV_32F && ddepth == CV_64F)
  47. func = reduceR_<float,double,OpAdd<double> >;
  48. else if(sdepth == CV_64F && ddepth == CV_64F)
  49. func = reduceR_<double,double,OpAdd<double> >;
  50. }
  51. else if(op == CV_REDUCE_MAX)
  52. {
  53. if(sdepth == CV_8U && ddepth == CV_8U)
  54. func = reduceR_<uchar, uchar, OpMax<uchar> >;
  55. else if(sdepth == CV_16U && ddepth == CV_16U)
  56. func = reduceR_<ushort, ushort, OpMax<ushort> >;
  57. else if(sdepth == CV_16S && ddepth == CV_16S)
  58. func = reduceR_<short, short, OpMax<short> >;
  59. else if(sdepth == CV_32F && ddepth == CV_32F)
  60. func = reduceR_<float, float, OpMax<float> >;
  61. else if(sdepth == CV_64F && ddepth == CV_64F)
  62. func = reduceR_<double, double, OpMax<double> >;
  63. }
  64. else if(op == CV_REDUCE_MIN)
  65. {
  66. if(sdepth == CV_8U && ddepth == CV_8U)
  67. func = reduceR_<uchar, uchar, OpMin<uchar> >;
  68. else if(sdepth == CV_16U && ddepth == CV_16U)
  69. func = reduceR_<ushort, ushort, OpMin<ushort> >;
  70. else if(sdepth == CV_16S && ddepth == CV_16S)
  71. func = reduceR_<short, short, OpMin<short> >;
  72. else if(sdepth == CV_32F && ddepth == CV_32F)
  73. func = reduceR_<float, float, OpMin<float> >;
  74. else if(sdepth == CV_64F && ddepth == CV_64F)
  75. func = reduceR_<double, double, OpMin<double> >;
  76. }
  77. }
  78. else
  79. {
  80. if(op == CV_REDUCE_SUM)
  81. {
  82. if(sdepth == CV_8U && ddepth == CV_32S)
  83. func = reduceC_<uchar,int,OpAdd<int> >;
  84. else if(sdepth == CV_8U && ddepth == CV_32F)
  85. func = reduceC_<uchar,float,OpAdd<int> >;
  86. else if(sdepth == CV_8U && ddepth == CV_64F)
  87. func = reduceC_<uchar,double,OpAdd<int> >;
  88. else if(sdepth == CV_16U && ddepth == CV_32F)
  89. func = reduceC_<ushort,float,OpAdd<float> >;
  90. else if(sdepth == CV_16U && ddepth == CV_64F)
  91. func = reduceC_<ushort,double,OpAdd<double> >;
  92. else if(sdepth == CV_16S && ddepth == CV_32F)
  93. func = reduceC_<short,float,OpAdd<float> >;
  94. else if(sdepth == CV_16S && ddepth == CV_64F)
  95. func = reduceC_<short,double,OpAdd<double> >;
  96. else if(sdepth == CV_32F && ddepth == CV_32F)
  97. func = reduceC_<float,float,OpAdd<float> >;
  98. else if(sdepth == CV_32F && ddepth == CV_64F)
  99. func = reduceC_<float,double,OpAdd<double> >;
  100. else if(sdepth == CV_64F && ddepth == CV_64F)
  101. func = reduceC_<double,double,OpAdd<double> >;
  102. }
  103. else if(op == CV_REDUCE_MAX)
  104. {
  105. if(sdepth == CV_8U && ddepth == CV_8U)
  106. func = reduceC_<uchar, uchar, OpMax<uchar> >;
  107. else if(sdepth == CV_16U && ddepth == CV_16U)
  108. func = reduceC_<ushort, ushort, OpMax<ushort> >;
  109. else if(sdepth == CV_16S && ddepth == CV_16S)
  110. func = reduceC_<short, short, OpMax<short> >;
  111. else if(sdepth == CV_32F && ddepth == CV_32F)
  112. func = reduceC_<float, float, OpMax<float> >;
  113. else if(sdepth == CV_64F && ddepth == CV_64F)
  114. func = reduceC_<double, double, OpMax<double> >;
  115. }
  116. else if(op == CV_REDUCE_MIN)
  117. {
  118. if(sdepth == CV_8U && ddepth == CV_8U)
  119. func = reduceC_<uchar, uchar, OpMin<uchar> >;
  120. else if(sdepth == CV_16U && ddepth == CV_16U)
  121. func = reduceC_<ushort, ushort, OpMin<ushort> >;
  122. else if(sdepth == CV_16S && ddepth == CV_16S)
  123. func = reduceC_<short, short, OpMin<short> >;
  124. else if(sdepth == CV_32F && ddepth == CV_32F)
  125. func = reduceC_<float, float, OpMin<float> >;
  126. else if(sdepth == CV_64F && ddepth == CV_64F)
  127. func = reduceC_<double, double, OpMin<double> >;
  128. }
  129. }
  130. if( !func )
  131. CV_Error( CV_StsUnsupportedFormat,
  132. "Unsupported combination of input and output array formats" );
  133. func( src, temp );
  134. if( op0 == CV_REDUCE_AVG )
  135. temp.convertTo(dst, dst.type(), 1./(dim == 0 ? src.rows : src.cols));
  136. }

文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/60589168

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。