OpenCV -- CV_8UC1,CV_32FC3等参数的含义

(一)Mat矩阵(图像容器)创建时CV_8UC1,CV_8UC2等参数详解

        Mat不但是一个非常有用的图像容器类,同时也是一个通用的矩阵类.创建一个Mat对象的方法很多,我们现在先看一下Mat矩阵/图像容器类在OpenCv中的有关源代码:

使用Mat图像容器类创建Mat类的对象.

    //! default constructor
    Mat();
    //! constructs 2D matrix of the specified size and type
    // (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)
    Mat(int rows, int cols, int type);
    Mat(Size size, int type);
    //! constucts 2D matrix and fills it with the specified value _s.
    Mat(int rows, int cols, int type, const Scalar& s);
    Mat(Size size, int type, const Scalar& s);
 
    //! constructs n-dimensional matrix
    Mat(int ndims, const int* sizes, int type);
    Mat(int ndims, const int* sizes, int type, const Scalar& s);

我们可以看见,创建Mat矩阵/图像容器类的很多构造方法或者其他成员方法在创建Mat对象的时候,都需要指定type--所创建图像/矩阵的类型。 那么类型是什么呢?OpenCv的源代码中说了一句:

(_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)

通过转到定义法,看一下CV_8UC1,CV_64FC3等这些宏。

这里的type可以是任何的预定义类型,预定义类型的结构如下所示:

CV_<bit_depth>(S|U|F)C<number_of_channels>

  

 1 -- bit_depth---比特数---代表8bite,16bites,32bites,64bites---举个例子吧--比如说,如
        如果你现在创建了一个存储--灰度图片的Mat对象,这个图像的大小为宽100,高100,那么,现在这张灰度图片中有10000个像素点,它每一个像素点在内存空间所占的空间大小是8bite,8位--所以它对应的就是CV_8


 2 -- S|U|F--

         S--代表---signed int---有符号整形
         U--代表--unsigned int--无符号整形
         F--代表--float---------单精度浮点型


3 -- C<number_of_channels>----代表---一张图片的通道数,比如:
         1--灰度图片--grayImg---是--单通道图像
         2--RGB彩色图像---------是--3通道图像
         3--带Alph通道的RGB图像--是--4通道图像

创建Mat类对象:

// (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)

Mat(int rows, int cols, int type);

  通过上面的讲解,现在,我们解读一下OpenCv的源代码:

//【1】CV_8UC1---则可以创建----8位无符号的单通道---灰度图片------grayImg
#define CV_8UC1 CV_MAKETYPE(CV_8U,1)
#define CV_8UC2 CV_MAKETYPE(CV_8U,2)
//【2】CV_8UC3---则可以创建----8位无符号的三通道---RGB彩色图像---colorImg 
#define CV_8UC3 CV_MAKETYPE(CV_8U,3)
//【3】CV_8UC4--则可以创建-----8位无符号的四通道---带透明色的RGB图像 
#define CV_8UC4 CV_MAKETYPE(CV_8U,4)

(二)Mat矩阵图像容器类创建Mat类对象常用的几种方法:

  • 1)使用Mat矩阵图像容器类的构造函数创建Mat类对象
    //【1】载入原始图像1.jpg
    int flag;
    Mat srcImg=imread("1.jpg",flag); //flag = 0 表示灰度图; 1表示原图
    //【2】创建一个和原始图像srcImg高和宽一致的8位无符号单通道的灰度图片容器,并且初始化图片为白色255
    Mat grayImg(srcImg.rows,srcImg.cols,CV_8UC1,Scalar(255));
  • 2)为已经存在的IplImage指针创建信息头
    //【1】声明IplImg指针
    IplImg* imgTopDown; 
    //【2】将图片加载到内存中
    imgTopDown=cvLoadImage("1.jpg", CV_LOAD_IMAGE_GRAYSCALE);
    //【3】为已经存在的imgTopDown指针创建信息头
    //【4】转换IplImage*--->Mat
    Mat mtx(imgTopDown);
  • 3)利用Create()函数创建Mat矩阵图像容器类的对象
    //【1】载入原始图像1.jpg
    Mat srcImg=imread("1.jpg",1);
    //【2】创建一个和原始图像srcImg高和宽一致的8位无符号单通道的灰度图片容器,并且初始化图片为白色255
    Mat dstImg.create(srcImg.rows,srcImg.cols,CV_8UC1);
  • 4)使用Matlab风格的函数创建Mat矩阵图形容器类的对象
    //! Matlab-style matrix initialization
    static MatExpr zeros(int rows, int cols, int type);
    static MatExpr zeros(Size size, int type);
    static MatExpr zeros(int ndims, const int* sz, int type);
    static MatExpr ones(int rows, int cols, int type);
    static MatExpr ones(Size size, int type);
    static MatExpr ones(int ndims, const int* sz, int type);
    static MatExpr eye(int rows, int cols, int type);
    static MatExpr eye(Size size, int type);
    //【1】载入原始图像1.jpg
    Mat srcImg=imread("1.jpg",1);
    //【2】创建一个和原始图像srcImg高和宽一致的8位无符号单通道的灰度图片容器,并且初始化图片为白色255
    Mat dstImg=Mat::zeros(srcImg.rows,srcImg.cols,CV_8UC3);
    Mat dstImg=Mat::ones(srcImg.rows,srcImg.cols,CV_8UC3);
    Mat dstImg=Mat::eye(srcImg.rows,srcImg.cols,CV_8UC3);

转自:https://blog.csdn.net/maweifei/article/details/51221259

原文地址:https://www.cnblogs.com/zzzsj/p/14576107.html