本文转自: 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:
- #include "opencv2/opencv.hpp"
- #include <stdio.h>
- using namespace cv;
- void printMat(Mat mat)
- {
- int x,y;
-
- for(y=0;y<mat.rows;y++)
- {
- printf("第%d行:",y);
- for(x=0;x<mat.cols;x++)
- {
- float value;
- value = mat.at<float>(y,x);
-
- printf("%.2f ",value);
- }
- printf("\n");
- }
- }
- int main(int argc, char* argv[])
- {
-
- int x,y;
- //创建一个10*10的uchar型单通道矩阵 并且矩阵里的初始值为1
- cv::Mat image(10,10,CV_8U,cv::Scalar(1));
- //imageConvert
- cv::Mat imageConvert;
- //把CV_8U型的矩阵转换为CV_32F,并且矩阵中每一个值乘以3 加上4
- image.convertTo(imageConvert,CV_32F,3,4);
- printMat(imageConvert);
- return 0;
- }
程序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
请按任意键继续. . .
第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
opencv-2.3.0\modules\core\src\convert.cpp line:900
- void cv::Mat::convertTo(OutputArray _dst, int _type, double alpha, double beta) const
- {
- bool noScale = fabs(alpha-1) < DBL_EPSILON && fabs(beta) < DBL_EPSILON;
- if( _type < 0 )
- _type = _dst.fixedType() ? _dst.type() : type();
- else
- _type = CV_MAKETYPE(CV_MAT_DEPTH(_type), channels());
- int sdepth = depth(), ddepth = CV_MAT_DEPTH(_type);
- if( sdepth == ddepth && noScale )
- {
- copyTo(_dst);
- return;
- }
- Mat src = *this;
-
- BinaryFunc func = noScale ? getConvertFunc(sdepth, ddepth) : getConvertScaleFunc(sdepth, ddepth);
- double scale[] = {alpha, beta};
- int cn = channels();
- CV_Assert( func != 0 );
-
- if( dims <= 2 )
- {
- <font color="red">_dst.create( size(), _type );</font>
- Mat dst = _dst.getMat();
- Size sz = getContinuousSize(src, dst, cn);
- func( src.data, src.step, 0, 0, dst.data, dst.step, sz, scale );
- }
- else
- {
- <font color="red"> _dst.create( dims, size, _type );</font>
- Mat dst = _dst.getMat();
- const Mat* arrays[] = {&src, &dst, 0};
- uchar* ptrs[2];
- NAryMatIterator it(arrays, ptrs);
- Size sz((int)(it.size*cn), 1);
-
- for( size_t i = 0; i < it.nplanes; i++, ++it )
- func(ptrs[0], 0, 0, 0, ptrs[1], 0, sz, scale);
- }
- }