(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
|
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