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

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页