保存OpenCV中Mat_<Vec3f>格式的图像

本文介绍如何使用OpenCV中的Mat::convertTo函数解决特定图像格式(如Mat_<Vec3f>)无法直接保存的问题,并通过实例展示转换过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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) + )

————————————————————分割线——————————————————————————————

Mat::convertTo

在缩放或不缩放的情况下转换为另一种数据类型。

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>源自这里


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值