OpenCV MAT基本圖像容器

參考博客:html

OpenCv中cv::Mat和IplImage,CvMat之間的轉換算法

Mat - 基本圖像容器express

 

Mat類型較CvMat和IplImage有更強的矩陣運算能力,支持常見的矩陣運算(參照Matlab中的各類矩陣運算),因此將IplImage類型和CvMat類型轉換爲Mat類型更易於數據處理。less

關於 Mat ,首先要知道的是你沒必要再手動地(1)爲其開闢空間(2)在不須要時當即將空間釋放。但手動地作仍是能夠的:大多數OpenCV函數仍會手動地爲輸出數據開闢空間。當傳遞一個已經存在的 Mat 對象時,開闢好的矩陣空間會被重用。也就是說,咱們每次都使用大小正好的內存來完成任務。ide

基本上講 Mat 是一個類,由兩個數據部分組成:矩陣頭(包含矩陣尺寸,存儲方法,存儲地址等信息)和一個指向存儲全部像素值的矩陣(根據所選存儲方法的不一樣矩陣能夠是不一樣的維數)的指針。矩陣頭的尺寸是常數值,但矩陣自己的尺寸會依圖像的不一樣而不一樣,一般比矩陣頭的尺寸大數個數量級。所以,當在程序中傳遞圖像並建立拷貝時,大的開銷是由矩陣形成的,而不是信息頭。函數

爲了解決圖像傳輸拖程序速度,OpenCV使用引用計數機制。其思路是讓每一個 Mat 對象有本身的信息頭,但共享同一個矩陣。這經過讓矩陣指針指向同一地址而實現。而拷貝構造函數則 只拷貝信息頭和矩陣指針 ,而不拷貝矩陣。ui

 

Mat對象操做:this

一、建立Mat類對象編碼

Mat A, C;                                 // 只建立信息頭部分
A = imread(argv[1], CV_LOAD_IMAGE_COLOR); // 這裏爲矩陣開闢內存

Mat B(A);                                 // 使用拷貝構造函數
C = A;                                    // 賦值運算符

二、建立ROI的信息頭spa

Mat D (A, Rect(10, 10, 100, 100) ); // using a rectangle
Mat E = A(Range:all(), Range(1,3)); // using row and column boundaries

三、矩陣的拷貝

Mat F = A.clone();
Mat G;
A.copyTo(G);

總結:

  • OpenCV函數中輸出圖像的內存分配是自動完成的(若是不特別指定的話)。
  • 使用OpenCV的C++接口時不須要考慮內存釋放問題。
  • 賦值運算符和拷貝構造函數( ctor )只拷貝信息頭。
  • 使用函數 clone() 或者 copyTo() 來拷貝一副圖像的矩陣。

 

像素值的存儲

顏色空間:

指對一個給定的顏色,如何組合顏色元素以對其編碼。最簡單的顏色空間要屬灰度級空間,只處理黑色和白色,對它們進行組合能夠產生不一樣程度的灰色。

對於 彩色 方式則有更多種類的顏色空間,但不論哪一種方式都是把顏色分紅三個或者四個基元素,經過組合基元素能夠產生全部的顏色。RGB顏色空間是最經常使用的一種顏色空間,這歸功於它也是人眼內部構成顏色的方式。它的基色是紅色、綠色和藍色,有時爲了表示透明顏色也會加入第四個元素 alpha (A)。

  • RGB是最多見的,這是由於人眼採用類似的工做機制,它也被顯示設備所採用。
  • HSV和HLS把顏色分解成色調、飽和度和亮度/明度。這是描述顏色更天然的方式,好比能夠經過拋棄最後一個元素,使算法對輸入圖像的光照條件不敏感。
  • YCrCb在JPEG圖像格式中普遍使用。
  • CIE L*a*b*是一種在感知上均勻的顏色空間,它適合用來度量兩個顏色之間的 距離 。

存儲類型:

每一個組成元素都有其本身的定義域,取決於其數據類型。如何存儲一個元素決定了咱們在其定義域上可以控制的精度。最小的數據類型是char ,佔一個字節或者8位,能夠是有符號型(0到255之間)或無符號型(-127到+127之間)。儘管使用三個 char 型元素已經能夠表示1600萬種可能的顏色(使用RGB顏色空間),但若使用float(4字節,32位)或double(8字節,64位)則能給出更加精細的顏色分辨能力。但同時也要切記增長元素的尺寸也會增長了圖像所佔的內存空間。

 

 

int flags;  

int dims;    矩陣的維度(>=2)

int rows, cols;   矩陣行列值( 對二維矩陣而言,超過二維賦值爲(-1,-1) )

uchar *data;   數據指針

int* refcount;  引用計數,當矩陣指針指向用戶分配的數據,指針爲NULL(?)

uchar* datastart; 設定ROI區域空間的三個指針
uchar* dataend;
uchar* datalimit;

MatAllocator* allocator;   custom allocator(?)

MSize size;    
MStep step;  每行數據字節數

  1 class CV_EXPORTS Mat
  2 {
  3 public:
  4     //! default constructor
  5     Mat();
  6     //! constructs 2D matrix of the specified size and type
  7     // (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)
  8     Mat(int rows, int cols, int type);
  9     Mat(Size size, int type);
 10     //! constucts 2D matrix and fills it with the specified value _s.
 11     Mat(int rows, int cols, int type, const Scalar& s);
 12     Mat(Size size, int type, const Scalar& s);
 13 
 14     //! constructs n-dimensional matrix
 15     Mat(int ndims, const int* sizes, int type);
 16     Mat(int ndims, const int* sizes, int type, const Scalar& s);
 17 
 18     //! copy constructor
 19     Mat(const Mat& m);
 20     //! constructor for matrix headers pointing to user-allocated data
 21     Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP);
 22     Mat(Size size, int type, void* data, size_t step=AUTO_STEP);
 23     Mat(int ndims, const int* sizes, int type, void* data, const size_t* steps=0);
 24 
 25     //! creates a matrix header for a part of the bigger matrix
 26     Mat(const Mat& m, const Range& rowRange, const Range& colRange=Range::all());
 27     Mat(const Mat& m, const Rect& roi);
 28     Mat(const Mat& m, const Range* ranges);
 29     //! converts old-style CvMat to the new matrix; the data is not copied by default
 30     Mat(const CvMat* m, bool copyData=false);
 31     //! converts old-style CvMatND to the new matrix; the data is not copied by default
 32     Mat(const CvMatND* m, bool copyData=false);
 33     //! converts old-style IplImage to the new matrix; the data is not copied by default
 34     Mat(const IplImage* img, bool copyData=false);
 35     //! builds matrix from std::vector with or without copying the data
 36     template<typename _Tp> explicit Mat(const vector<_Tp>& vec, bool copyData=false);
 37     //! builds matrix from cv::Vec; the data is copied by default
 38     template<typename _Tp, int n> explicit Mat(const Vec<_Tp, n>& vec, bool copyData=true);
 39     //! builds matrix from cv::Matx; the data is copied by default
 40     template<typename _Tp, int m, int n> explicit Mat(const Matx<_Tp, m, n>& mtx, bool copyData=true);
 41     //! builds matrix from a 2D point
 42     template<typename _Tp> explicit Mat(const Point_<_Tp>& pt, bool copyData=true);
 43     //! builds matrix from a 3D point
 44     template<typename _Tp> explicit Mat(const Point3_<_Tp>& pt, bool copyData=true);
 45     //! builds matrix from comma initializer
 46     template<typename _Tp> explicit Mat(const MatCommaInitializer_<_Tp>& commaInitializer);
 47 
 48     //! download data from GpuMat
 49     explicit Mat(const gpu::GpuMat& m);
 50 
 51     //! destructor - calls release()
 52     ~Mat();
 53     //! assignment operators
 54     Mat& operator = (const Mat& m);
 55     Mat& operator = (const MatExpr& expr);
 56 
 57     //! returns a new matrix header for the specified row
 58     Mat row(int y) const;
 59     //! returns a new matrix header for the specified column
 60     Mat col(int x) const;
 61     //! ... for the specified row span
 62     Mat rowRange(int startrow, int endrow) const;
 63     Mat rowRange(const Range& r) const;
 64     //! ... for the specified column span
 65     Mat colRange(int startcol, int endcol) const;
 66     Mat colRange(const Range& r) const;
 67     //! ... for the specified diagonal
 68     // (d=0 - the main diagonal,
 69     //  >0 - a diagonal from the lower half,
 70     //  <0 - a diagonal from the upper half)
 71     Mat diag(int d=0) const;
 72     //! constructs a square diagonal matrix which main diagonal is vector "d"
 73     static Mat diag(const Mat& d);
 74 
 75     //! returns deep copy of the matrix, i.e. the data is copied
 76     Mat clone() const;
 77     //! copies the matrix content to "m".
 78     // It calls m.create(this->size(), this->type()).
 79     void copyTo( OutputArray m ) const;
 80     //! copies those matrix elements to "m" that are marked with non-zero mask elements.
 81     void copyTo( OutputArray m, InputArray mask ) const;
 82     //! converts matrix to another datatype with optional scalng. See cvConvertScale.
 83     void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const;
 84 
 85     void assignTo( Mat& m, int type=-1 ) const;
 86 
 87     //! sets every matrix element to s
 88     Mat& operator = (const Scalar& s);
 89     //! sets some of the matrix elements to s, according to the mask
 90     Mat& setTo(InputArray value, InputArray mask=noArray());
 91     //! creates alternative matrix header for the same data, with different
 92     // number of channels and/or different number of rows. see cvReshape.
 93     Mat reshape(int cn, int rows=0) const;
 94     Mat reshape(int cn, int newndims, const int* newsz) const;
 95 
 96     //! matrix transposition by means of matrix expressions
 97     MatExpr t() const;
 98     //! matrix inversion by means of matrix expressions
 99     MatExpr inv(int method=DECOMP_LU) const;
100     //! per-element matrix multiplication by means of matrix expressions
101     MatExpr mul(InputArray m, double scale=1) const;
102 
103     //! computes cross-product of 2 3D vectors
104     Mat cross(InputArray m) const;
105     //! computes dot-product
106     double dot(InputArray m) const;
107 
108     //! Matlab-style matrix initialization
109     static MatExpr zeros(int rows, int cols, int type);
110     static MatExpr zeros(Size size, int type);
111     static MatExpr zeros(int ndims, const int* sz, int type);
112     static MatExpr ones(int rows, int cols, int type);
113     static MatExpr ones(Size size, int type);
114     static MatExpr ones(int ndims, const int* sz, int type);
115     static MatExpr eye(int rows, int cols, int type);
116     static MatExpr eye(Size size, int type);
117 
118     //! allocates new matrix data unless the matrix already has specified size and type.
119     // previous data is unreferenced if needed.
120     void create(int rows, int cols, int type);
121     void create(Size size, int type);
122     void create(int ndims, const int* sizes, int type);
123 
124     //! increases the reference counter; use with care to avoid memleaks
125     void addref();
126     //! decreases reference counter;
127     // deallocates the data when reference counter reaches 0.
128     void release();
129 
130     //! deallocates the matrix data
131     void deallocate();
132     //! internal use function; properly re-allocates _size, _step arrays
133     void copySize(const Mat& m);
134 
135     //! reserves enough space to fit sz hyper-planes
136     void reserve(size_t sz);
137     //! resizes matrix to the specified number of hyper-planes
138     void resize(size_t sz);
139     //! resizes matrix to the specified number of hyper-planes; initializes the newly added elements
140     void resize(size_t sz, const Scalar& s);
141     //! internal function
142     void push_back_(const void* elem);
143     //! adds element to the end of 1d matrix (or possibly multiple elements when _Tp=Mat)
144     template<typename _Tp> void push_back(const _Tp& elem);
145     template<typename _Tp> void push_back(const Mat_<_Tp>& elem);
146     void push_back(const Mat& m);
147     //! removes several hyper-planes from bottom of the matrix
148     void pop_back(size_t nelems=1);
149 
150     //! locates matrix header within a parent matrix. See below
151     void locateROI( Size& wholeSize, Point& ofs ) const;
152     //! moves/resizes the current matrix ROI inside the parent matrix.
153     Mat& adjustROI( int dtop, int dbottom, int dleft, int dright );
154     //! extracts a rectangular sub-matrix
155     // (this is a generalized form of row, rowRange etc.)
156     Mat operator()( Range rowRange, Range colRange ) const;
157     Mat operator()( const Rect& roi ) const;
158     Mat operator()( const Range* ranges ) const;
159 
160     //! converts header to CvMat; no data is copied
161     operator CvMat() const;
162     //! converts header to CvMatND; no data is copied
163     operator CvMatND() const;
164     //! converts header to IplImage; no data is copied
165     operator IplImage() const;
166 
167     template<typename _Tp> operator vector<_Tp>() const;
168     template<typename _Tp, int n> operator Vec<_Tp, n>() const;
169     template<typename _Tp, int m, int n> operator Matx<_Tp, m, n>() const;
170 
171     //! returns true iff the matrix data is continuous
172     // (i.e. when there are no gaps between successive rows).
173     // similar to CV_IS_MAT_CONT(cvmat->type)
174     bool isContinuous() const;
175 
176     //! returns true if the matrix is a submatrix of another matrix
177     bool isSubmatrix() const;
178 
179     //! returns element size in bytes,
180     // similar to CV_ELEM_SIZE(cvmat->type)
181     size_t elemSize() const;
182     //! returns the size of element channel in bytes.
183     size_t elemSize1() const;
184     //! returns element type, similar to CV_MAT_TYPE(cvmat->type)
185     int type() const;
186     //! returns element type, similar to CV_MAT_DEPTH(cvmat->type)
187     int depth() const;
188     //! returns element type, similar to CV_MAT_CN(cvmat->type)
189     int channels() const;
190     //! returns step/elemSize1()
191     size_t step1(int i=0) const;
192     //! returns true if matrix data is NULL
193     bool empty() const;
194     //! returns the total number of matrix elements
195     size_t total() const;
196 
197     //! returns N if the matrix is 1-channel (N x ptdim) or ptdim-channel (1 x N) or (N x 1); negative number otherwise
198     int checkVector(int elemChannels, int depth=-1, bool requireContinuous=true) const;
199 
200     //! returns pointer to i0-th submatrix along the dimension #0
201     uchar* ptr(int i0=0);
202     const uchar* ptr(int i0=0) const;
203 
204     //! returns pointer to (i0,i1) submatrix along the dimensions #0 and #1
205     uchar* ptr(int i0, int i1);
206     const uchar* ptr(int i0, int i1) const;
207 
208     //! returns pointer to (i0,i1,i3) submatrix along the dimensions #0, #1, #2
209     uchar* ptr(int i0, int i1, int i2);
210     const uchar* ptr(int i0, int i1, int i2) const;
211 
212     //! returns pointer to the matrix element
213     uchar* ptr(const int* idx);
214     //! returns read-only pointer to the matrix element
215     const uchar* ptr(const int* idx) const;
216 
217     template<int n> uchar* ptr(const Vec<int, n>& idx);
218     template<int n> const uchar* ptr(const Vec<int, n>& idx) const;
219 
220     //! template version of the above method
221     template<typename _Tp> _Tp* ptr(int i0=0);
222     template<typename _Tp> const _Tp* ptr(int i0=0) const;
223 
224     template<typename _Tp> _Tp* ptr(int i0, int i1);
225     template<typename _Tp> const _Tp* ptr(int i0, int i1) const;
226 
227     template<typename _Tp> _Tp* ptr(int i0, int i1, int i2);
228     template<typename _Tp> const _Tp* ptr(int i0, int i1, int i2) const;
229 
230     template<typename _Tp> _Tp* ptr(const int* idx);
231     template<typename _Tp> const _Tp* ptr(const int* idx) const;
232 
233     template<typename _Tp, int n> _Tp* ptr(const Vec<int, n>& idx);
234     template<typename _Tp, int n> const _Tp* ptr(const Vec<int, n>& idx) const;
235 
236     //! the same as above, with the pointer dereferencing
237     template<typename _Tp> _Tp& at(int i0=0);
238     template<typename _Tp> const _Tp& at(int i0=0) const;
239 
240     template<typename _Tp> _Tp& at(int i0, int i1);
241     template<typename _Tp> const _Tp& at(int i0, int i1) const;
242 
243     template<typename _Tp> _Tp& at(int i0, int i1, int i2);
244     template<typename _Tp> const _Tp& at(int i0, int i1, int i2) const;
245 
246     template<typename _Tp> _Tp& at(const int* idx);
247     template<typename _Tp> const _Tp& at(const int* idx) const;
248 
249     template<typename _Tp, int n> _Tp& at(const Vec<int, n>& idx);
250     template<typename _Tp, int n> const _Tp& at(const Vec<int, n>& idx) const;
251 
252     //! special versions for 2D arrays (especially convenient for referencing image pixels)
253     template<typename _Tp> _Tp& at(Point pt);
254     template<typename _Tp> const _Tp& at(Point pt) const;
255 
256     //! template methods for iteration over matrix elements.
257     // the iterators take care of skipping gaps in the end of rows (if any)
258     template<typename _Tp> MatIterator_<_Tp> begin();
259     template<typename _Tp> MatIterator_<_Tp> end();
260     template<typename _Tp> MatConstIterator_<_Tp> begin() const;
261     template<typename _Tp> MatConstIterator_<_Tp> end() const;
262 
263     enum { MAGIC_VAL=0x42FF0000, AUTO_STEP=0, CONTINUOUS_FLAG=CV_MAT_CONT_FLAG, SUBMATRIX_FLAG=CV_SUBMAT_FLAG };
264 
265     /*! includes several bit-fields:
266          - the magic signature
267          - continuity flag
268          - depth
269          - number of channels
270      */
271     int flags;
272     //! the matrix dimensionality, >= 2
273     int dims;
274     //! the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
275     int rows, cols;
276     //! pointer to the data
277     uchar* data;
278 
279     //! pointer to the reference counter;
280     // when matrix points to user-allocated data, the pointer is NULL
281     int* refcount;
282 
283     //! helper fields used in locateROI and adjustROI
284     uchar* datastart;
285     uchar* dataend;
286     uchar* datalimit;
287 
288     //! custom allocator
289     MatAllocator* allocator;
290 
291     struct CV_EXPORTS MSize
292     {
293         MSize(int* _p);
294         Size operator()() const;
295         const int& operator[](int i) const;
296         int& operator[](int i);
297         operator const int*() const;
298         bool operator == (const MSize& sz) const;
299         bool operator != (const MSize& sz) const;
300 
301         int* p;
302     };
303 
304     struct CV_EXPORTS MStep
305     {
306         MStep();
307         MStep(size_t s);
308         const size_t& operator[](int i) const;
309         size_t& operator[](int i);
310         operator size_t() const;
311         MStep& operator = (size_t s);
312 
313         size_t* p;
314         size_t buf[2];
315     protected:
316         MStep& operator = (const MStep&);
317     };
318 
319     MSize size;
320     MStep step;
321 
322 protected:
323     void initEmpty();
324 };
相關文章
相關標籤/搜索