在OpenCV中,有时会出现如Mat_<Vec3f>图像类型,直接用cvsaveImage或者imwrite保存,会出现一片黑色。保存失败:
如下
这是定义格式问题
此时,需要转换正确的格式才能保存成功
OpenCV中有convertTo函数
是一种格式转换函数
具体如下
——————————————————————————————————————————————————
Mat::convertTo
Converts an array to another data type with optional scaling.
C++: void Mat::convertTo(OutputArray m, int rtype, double alpha=1, double beta=0 ) const
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.
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) + )
————————————————————分割线——————————————————————————————
在缩放或不缩放的情况下转换为另一种数据类型。
C++:
void Mat::convertTo(OutputArray m,int rtype,double alpha=1,double beta=0)const
参数:
m – 目标矩阵。如果它的尺寸和类型不正确,在操作之前会重新分配。
rtype – 要求是目标矩阵的类型,或者在当前通道数与源矩阵通道数相同的情况下的depth。如果rtype 为负,目标矩阵与源矩阵类型相同。
beta – 可选的delta加到缩放值中去。
该方法将源像素值转化为目标类型saturate_cast<> 要放在最后以避免溢出
m( x;y) = saturate_cast < rType > ( α*( *this)( x;y) +β)
——————————————————————————————————————————————————
具体实现如下:
Mat_<Vec3f> blend = LaplacianBlend(l, r, m); imshow("blended",blend); Mat re; blend.convertTo(re,CV_8UC3,255); imwrite("blended.jpg",re);
Mat_<Vec3f>源自这里