欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

opencv 数据类型及相互转换

程序员文章站 2022-06-01 22:02:56
...

获取Mat的类型:

// myMat即图像数据, type_val为类型标识值
myMat = imread("C:\someimage.jpg");
int type = myMat.type();
或者直接这样
System.out.println(CvType.typeToString(myMat));

类型标示值的含义:

C1 C2 C3 C4 C(5) C(6) C(7) C(8)
CV_8U 0 8 16 24 32 40 48 56
CV_8S 1 9 17 25 33 41 49 57
CV_16U 2 10 18 26 34 42 50 58
CV_16S 3 11 19 27 35 43 51 59
CV_32S 4 12 20 28 36 44 52 60
CV_32F 5 13 21 29 37 45 53 61
CV_64F 6 14 22 30 38 46 54 62

*: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);

类型转换

//将[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;

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

相关标签: opencv 数据类型