opencv函数学习:convertTo()的使用

convertTo()

官方使用说明

void cv::Mat::convertTo(OutputArray m, int rtype, double alpha = 1, double beta = 0) const

Converts an array to another data type with optional scaling.     ——    该函数主要用于数据类型的相互转换

The method converts source pixel values to the target data type. saturate_cast<> is applied at the end to avoid possible overflows:

m(x,y)=saturate_cast<rType>(α(this)(x,y)+β)     ——     这是函数底层算法,了解算法后方便我们熟练运用
Parameters
m output matrix; if it does not have a proper size or type before the operation, it is reallocated.
rtype desired output matrix type or, rather, the depth since the number of channels are the same as the input has; if rtype is negative, the output matrix will have the same type as the input.
alpha optional scale factor.
beta optional delta added to the scaled values.

例子:将16位图像数据转为8位

Mat dstImg;

Mat srcImg(w, h, CV_16UC1, img);

srcImg.convertTo(dstImg, CV_8CU1, 255.0/65535, 0.5);

原文地址:https://www.cnblogs.com/IAMSailorMoon/p/14372228.html