convertTO函数 简介



本文转自: http://opencvchina.com/thread-2416-1-1.html 

cv::Mat::convertTo

函数原型
void Mat::convertTo( Mat& m, int rtype, double alpha=1, double beta=0 )
const;

输入参数:
m  目标矩阵。如果m的大小与原矩阵不一样,或者数据类型与参数不匹配,那么在函数convertTo内部会先给m重新分配空间。
rtype 指定从原矩阵进行转换后的数据类型,即目标矩阵m的数据类型。当然,矩阵m的通道数应该与原矩阵一样的。如果rtype是负数,那么m矩阵的数据类型应该与原矩阵一样。
alpha 缩放因子。默认值是1。即把原矩阵中的每一个元素都乘以alpha。
beta 增量。默认值是0。即把原矩阵中的每一个元素都乘以alpha,再加上beta。

功能
把一个矩阵从一种数据类型转换到另一种数据类型,同时可以带上缩放因子和增量,公式如下:
m(x,y)=saturate_cast<rType>(alpha*(*this)(x,y)+beta);
由于有数据类型的转换,所以需要用saturate_cast<rType>来处理数据的溢出。

实验程序1:
  1. #include "opencv2/opencv.hpp"
  2. #include <stdio.h>
  3. using namespace cv;
  4. void printMat(Mat mat)
  5. {
  6.         int x,y;
  7.         
  8.         for(y=0;y<mat.rows;y++)
  9.         {
  10.         printf("第%d行:",y);
  11.                 for(x=0;x<mat.cols;x++)
  12.                 {
  13.                         float value;
  14.                         value = mat.at<float>(y,x);
  15.                         
  16.                         printf("%.2f  ",value);
  17.                 }
  18.                 printf("\n");
  19.         }
  20. }



  21. int main(int argc, char* argv[])
  22. {
  23.         
  24.         int x,y;
  25. //创建一个10*10的uchar型单通道矩阵 并且矩阵里的初始值为1
  26. cv::Mat image(10,10,CV_8U,cv::Scalar(1));

  27. //imageConvert
  28. cv::Mat imageConvert;


  29. //把CV_8U型的矩阵转换为CV_32F,并且矩阵中每一个值乘以3 加上4
  30. image.convertTo(imageConvert,CV_32F,3,4);

  31. printMat(imageConvert);
  32.         return 0;
  33. }
复制代码
程序1的输出:
第0行:7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00
第1行:7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00
第2行:7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00
第3行:7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00
第4行:7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00
第5行:7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00
第6行:7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00
第7行:7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00
第8行:7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00
第9行:7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00  7.00
请按任意键继续. . .
convertTo函数的在opencv中的源码
opencv-2.3.0\modules\core\src\convert.cpp  line:900
  1. void cv::Mat::convertTo(OutputArray _dst, int _type, double alpha, double beta) const
  2. {
  3.     bool noScale = fabs(alpha-1) < DBL_EPSILON && fabs(beta) < DBL_EPSILON;

  4.     if( _type < 0 )
  5.         _type = _dst.fixedType() ? _dst.type() : type();
  6.     else
  7.         _type = CV_MAKETYPE(CV_MAT_DEPTH(_type), channels());

  8.     int sdepth = depth(), ddepth = CV_MAT_DEPTH(_type);
  9.     if( sdepth == ddepth && noScale )
  10.     {
  11.         copyTo(_dst);
  12.         return;
  13.     }

  14.     Mat src = *this;
  15.         
  16.     BinaryFunc func = noScale ? getConvertFunc(sdepth, ddepth) : getConvertScaleFunc(sdepth, ddepth);
  17.     double scale[] = {alpha, beta};
  18.     int cn = channels();
  19.     CV_Assert( func != 0 );
  20.    
  21.     if( dims <= 2 )
  22.     {
  23.         <font color="red">_dst.create( size(), _type );</font>
  24.         Mat dst = _dst.getMat();
  25.         Size sz = getContinuousSize(src, dst, cn);
  26.         func( src.data, src.step, 0, 0, dst.data, dst.step, sz, scale );
  27.     }
  28.     else
  29.     {
  30.        <font color="red"> _dst.create( dims, size, _type );</font>
  31.         Mat dst = _dst.getMat();
  32.         const Mat* arrays[] = {&src, &dst, 0};
  33.         uchar* ptrs[2];
  34.         NAryMatIterator it(arrays, ptrs);
  35.         Size sz((int)(it.size*cn), 1);
  36.         
  37.         for( size_t i = 0; i < it.nplanes; i++, ++it )
  38.             func(ptrs[0], 0, 0, 0, ptrs[1], 0, sz, scale);
  39.     }
  40. }
复制代码
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值