opencv 数据类型及相互转换

获取Mat的类型:
// myMat即图像数据, type_val为类型标识值
myMat = imread("C:\someimage.jpg");
int type = myMat.type();
或者直接这样
System.out.println(CvType.typeToString(myMat));
类型标示值的含义:
C1C2C3C4C(5)C(6)C(7)C(8)
CV_8U08162432404856
CV_8S19172533414957
CV_16U210182634425058
CV_16S311192735435159
CV_32S412202836445260
CV_32F513212937455361
CV_64F614223038465462

注:C1, C2, C3等表示通道数;CV_8U, CV_8S, CV_16U等数据类型。
举个例子:如果myMat.type()==16, 通过上表我们得知它的数据类型是CV_8U 且有3个通道,一个CV_8U占1个字节,那么myMat一个像素点需要1×3=3字节。
stackoverflow:how-to-find-out-what-type-of-a-mat-object-is-with-mattype-in-opencv

颜色通道转换
//将RGB格式的图像转换成BGR格式
cvtColor(RGB_image, BGR_image, COLOR_RGB2BGR);
//将BGR格式的图像转换成灰度图
cvtColor(BGR_imgage, gray_image, COLOR_BGR2GRAY);
//将BGR格式的图像转换为BGRA
cvtColor(BGR_imgage, gray_image, COLOR_BGR2BGRA);
类型转换
//将[0,1]范围内的浮点表示的图像转换成8bit整数表示的图像
float_image.convertTo(integer_image, CV_8U, 255.0);

convertTo函数定义如下:
/** @brief 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:

    \f[m(x,y) = saturate \_ cast<rType>( \alpha (*this)(x,y) +  \beta )\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.
     */
    void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const;

stackoverflow:change-type-of-mat-object-from-cv-32f-to-cv-8u

  • 10
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值