opencv Notes (22) - Implications of CV_8UC1,CV_32FC3 and other parameters

Keywords: OpenCV MATLAB

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

Mat is not only a very useful image container class, but also a general matrix class. 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:

Using Mat Image Container Class to Create Mat Class Objects

    //! 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);

We can see that many methods of constructing Mat matrix/image container classes or other member methods need to specify type when creating Mat objects -- the type of image/matrix created. What is the type? OpenCv's source code says:

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

By going to the definition method, look at the macros CV_8UC1,CV_64FC3, and so on.

The type here can be any predefined type, and the structure of the predefined type is as follows:

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

1--bit_depth -- bit number -- 8 bites, 16 bites, 32 bites, 64 bites -- for example
If you now create a Mat object that stores gray-scale images, the size of which is 100 wide and 100 high, then there are 10,000 pixels in this gray-scale image. The size of each pixel in the memory space is 8 bites and 8 bits, so it corresponds to CV_8.


2--S|U|F--S -- Represents signed int -- Signed shaping
U -- Representative -- 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 images with Alph channels -- yes -- 4 channels

Create Mat class objects:

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

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

With the above explanation, 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)

(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));
  • 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);
  • 3) Creating Mat Matrix Image Container Class Objects with Create() Function
    //[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.create(srcImg.rows,srcImg.cols,CV_8UC1);
  • 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);
  •  

Change from: https://blog.csdn.net/maweifei/article/details/51221259

Posted by funkyfela on Tue, 25 Dec 2018 13:36:06 -0800