Creation of OpenCv-Mat Matrix (Image Container) and Detailed Explanation of CV_8UC1,CV_8UC2 and Other Parameters

(1) Detailed description of CV_8UC1,CV_8UC2 and other parameters when Mat matrix (image container) is created

1--Mat is not only a very useful image container class, but also a general matrix class.
2 -- There are many ways to create a Mat object. Let's first look at the source code of Mat Matrix/Image Container class in OpenCv:
    3 -- Create Mat class objects using Mat image container class
    //! 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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
4 -- Create or initialize Mat class objects using MAT style functions
//! 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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  5 -- We can see that many of the constructions or other member methods for creating Mat matrix / image container classes are used when creating Mat objects.
      You need to specify type -- the type of image / matrix created
  6 -- So what is the type? OpenCv's source code says:
    (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)
  • 1
  • 2
  • 1
  • 2
  7 -- Similarly, by turning to the definition method, we can see what these macros are like, CV_8UC1,CV_64FC3 and so on. OpenCv source code display
       As follows (source code in types_c.h):
#define CV_CN_MAX     512
#define CV_CN_SHIFT   3
#define CV_DEPTH_MAX  (1 << CV_CN_SHIFT)

#define CV_8U   0
#define CV_8S   1
#define CV_16U  2
#define CV_16S  3
#define CV_32S  4
#define CV_32F  5
#define CV_64F  6
#define CV_USRTYPE1 7

#define CV_MAT_DEPTH_MASK       (CV_DEPTH_MAX - 1)
#define CV_MAT_DEPTH(flags)     ((flags) & CV_MAT_DEPTH_MASK)

#define CV_MAKETYPE(depth,cn) (CV_MAT_DEPTH(depth) + (((cn)-1) << CV_CN_SHIFT))
#define CV_MAKE_TYPE CV_MAKETYPE

#define CV_8UC1 CV_MAKETYPE(CV_8U,1)
#define CV_8UC2 CV_MAKETYPE(CV_8U,2)
#define CV_8UC3 CV_MAKETYPE(CV_8U,3)
#define CV_8UC4 CV_MAKETYPE(CV_8U,4)
#define CV_8UC(n) CV_MAKETYPE(CV_8U,(n))

#define CV_8SC1 CV_MAKETYPE(CV_8S,1)
#define CV_8SC2 CV_MAKETYPE(CV_8S,2)
#define CV_8SC3 CV_MAKETYPE(CV_8S,3)
#define CV_8SC4 CV_MAKETYPE(CV_8S,4)
#define CV_8SC(n) CV_MAKETYPE(CV_8S,(n))

#define CV_16UC1 CV_MAKETYPE(CV_16U,1)
#define CV_16UC2 CV_MAKETYPE(CV_16U,2)
#define CV_16UC3 CV_MAKETYPE(CV_16U,3)
#define CV_16UC4 CV_MAKETYPE(CV_16U,4)
#define CV_16UC(n) CV_MAKETYPE(CV_16U,(n))

#define CV_16SC1 CV_MAKETYPE(CV_16S,1)
#define CV_16SC2 CV_MAKETYPE(CV_16S,2)
#define CV_16SC3 CV_MAKETYPE(CV_16S,3)
#define CV_16SC4 CV_MAKETYPE(CV_16S,4)
#define CV_16SC(n) CV_MAKETYPE(CV_16S,(n))

#define CV_32SC1 CV_MAKETYPE(CV_32S,1)
#define CV_32SC2 CV_MAKETYPE(CV_32S,2)
#define CV_32SC3 CV_MAKETYPE(CV_32S,3)
#define CV_32SC4 CV_MAKETYPE(CV_32S,4)
#define CV_32SC(n) CV_MAKETYPE(CV_32S,(n))

#define CV_32FC1 CV_MAKETYPE(CV_32F,1)
#define CV_32FC2 CV_MAKETYPE(CV_32F,2)
#define CV_32FC3 CV_MAKETYPE(CV_32F,3)
#define CV_32FC4 CV_MAKETYPE(CV_32F,4)
#define CV_32FC(n) CV_MAKETYPE(CV_32F,(n))

#define CV_64FC1 CV_MAKETYPE(CV_64F,1)
#define CV_64FC2 CV_MAKETYPE(CV_64F,2)
#define CV_64FC3 CV_MAKETYPE(CV_64F,3)
#define CV_64FC4 CV_MAKETYPE(CV_64F,4)
#define CV_64FC(n) CV_MAKETYPE(CV_64F,(n))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
8 -- The type here can be any predefined type. The structure of the predefined type is as follows:
    CV_<bit_depth>(S|U|F)C<number_of_channels>
  • 1
  • 1
     1--bit_depth -- bit number -- 8 bites, 16 bites, 32 bites, 64 bites -- for example
        If you now create a Mat object that stores grayscale images, the size of which is 100 wide and 100 high, then this one is now
        There are 10,000 pixels in a gray-scale image, and each pixel occupies 8 bites of memory space -- so it does.
        It should be CV_8.
     2--S|U|F--S -- Represents signed int -- Signed shaping
               U -- Representation -- unsigned int -- Unsigned shaping
               F--Representation--float--single precision floating-point type
     3 - C < number_of_channels > - represents the number of channels for a picture, such as:
         1 -- Grayscale image -- grayImg -- yes -- single channel image
         2--RGB color image - ----------------------- 3-channel image
         3 -- RGB image with Alph channel -- yes -- 4 channel image
    9 -- With the above instructions, now let's read the source code of OpenCv:
//[1] CV_8UC1 - - - can create - - - 8-bit unsigned single channel - - - grayImg
#define CV_8UC1 CV_MAKETYPE(CV_8U,1)
#define CV_8UC2 CV_MAKETYPE(CV_8U,2)
//[2] CV_8UC3 - - - can create - - - 8-bit unsigned three-channel - - - RGB color image - - - colorImg 
#define CV_8UC3 CV_MAKETYPE(CV_8U,3)
//[3] CV_8UC4 -- can create an 8-bit unsigned four-channel RGB image with transparent color 
#define CV_8UC4 CV_MAKETYPE(CV_8U,4)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

(2) Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat Mat

1) Create Mat class objects using the constructor of Mat matrix image container class
    //[1] Load the original image 1.jpg
    Mat srcImg=imread("1.jpg",1);
    //[2] Create an 8-bit unsigned single-channel gray image container with the same height and width as the original image srcImg, and initialize the image to white 255
    Mat grayImg(srcImg.rows,srcImg.cols,CV_8UC1,Scalar(255));
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
2) Create headers for existing IplImage pointers
    //Declare IplImg pointer
    IplImg* imgTopDown; 
    //[2] Loading pictures into memory
    imgTopDown=cvLoadImage("1.jpg", CV_LOAD_IMAGE_GRAYSCALE);
    //[3] Create headers for existing imgTopDown pointers
    //[4] Convert IplImage* - > Mat
    Mat mtx(imgTopDown);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
3) Creating Mat Matrix Image Container Class Objects with Create() Function
    //[1]Load the original image1.jpg
    Mat srcImg=imread("1.jpg",1);
    //[2]Create an original image srcImg Consistent in height and width8Gray Image Container with Bit Unsigned Single Channel,And initialize the picture to white255
    Mat dstImg.create(srcImg.rows,srcImg.cols,CV_8UC1);
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
4) Creating Mat Matrix Graphics Container Class Objects with Functions in the Style of Matlab
    //! 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] Load the original image 1.jpg
    Mat srcImg=imread("1.jpg",1);
    //[2] Create an 8-bit unsigned single-channel gray image container with the same height and width as the original image srcImg, and initialize the image to white 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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
2 step on 1
 
 

Recommendations for related articles

Tags: OpenCV Javascript MATLAB Linux

Posted on Mon, 17 Dec 2018 03:18:04 -0800 by sstoveld