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