单通道float类型Mat在存数据时需要归一化, 即图像数据需要归一化到0~1之间。这是float类型图片的灰度空间。
比如:
Mat a=Mat::zeros(200,200,CV_32FC1); for(int row=0;row!=a.rows;row++) for(int col=0;col!=a.cols;col++) { a.at<float>(row,col)=0.5; } imshow(" test ",a); waitKey(0);
若float数据<0,则会显示为黑色,
Mat a=Mat::zeros(200,200,CV_32FC1); for(int row=0;row!=a.rows;row++) for(int col=0;col!=a.cols;col++) { a.at<float>(row,col)=-8; } imshow(" test ",a); waitKey(0);
若float数据>1,则会显示为白色。
Mat a=Mat::zeros(200,200,CV_32FC1); for(int row=0;row!=a.rows;row++) for(int col=0;col!=a.cols;col++) { a.at<float>(row,col)=2; } imshow(" test ",a); waitKey(0);
单通道uchar 类型Mat在显示图片时,Mat数据应在uchar数据范围内,即0~255。 若数据值不在0~255内,则Mat会以256为周期,把数据平移至0~255范围内。 比如 数据为256,则Mat会把数据转化成0,因此显示为黑色。
<pre name="code" class="html">Mat a=Mat::zeros(200,200,CV_8UC1); for(int row=0;row!=a.rows;row++) for(int col=0;col!=a.cols;col++) { a.at<uchar>(row,col)=256; } imshow(" test ",a); waitKey(0);
又如数据为-1,则Mat会把数据转换为255,显示全白。
</pre><pre name="code" class="html">Mat a=Mat::zeros(200,200,CV_8UC1); for(int row=0;row!=a.rows;row++) for(int col=0;col!=a.cols;col++) { a.at<uchar>(row,col)=-1; } imshow(" test ",a); waitKey(0);
另外,可以用下面这句话实现转换矩阵类型转换和整体元素线性变化。
The method converts source pixel values to the target data type. saturate_cast<> is applied at the end to avoid possible overflows: f[m(x,y) = saturate \_ cast<rType>( alpha (*this)(x,y) + eta )f] @param m output matrix; if it does not have a proper size or type before the operation, it is reallocated. @param 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. @param alpha optional scale factor. @param beta optional delta added to the scaled values.
比如:
Mat A,B; A.convertTo(B,type,a,b);
// B.(y,x)=A.(y,x)*a+b
//关于通道和数据类型:
//输出矩阵通道(B)和输入矩阵通道(A)一样,type用来决定输出矩阵的数据类型。type =-1 则B将会和A同数据类型,也可根据自己的需要填写别的数据类型,比如CV_8U。