参考博客:

OpenCv中cv::Mat和IplImage,CvMat之间的转换

Mat - 基本图像容器

Mat类型较CvMat和IplImage有更强的矩阵运算能力,支持常见的矩阵运算(参照Matlab中的各种矩阵运算),所以将IplImage类型和CvMat类型转换为Mat类型更易于数据处理。

关于 Mat ,首先要知道的是你不必再手动地(1)为其开辟空间(2)在不需要时立即将空间释放。但手动地做还是可以的:大多数OpenCV函数仍会手动地为输出数据开辟空间。当传递一个已经存在的 Mat 对象时,开辟好的矩阵空间会被重用。也就是说,我们每次都使用大小正好的内存来完成任务。

基本上讲 Mat 是一个类,由两个数据部分组成:矩阵头(包含矩阵尺寸,存储方法,存储地址等信息)和一个指向存储所有像素值的矩阵(根据所选存储方法的不同矩阵可以是不同的维数)的指针。矩阵头的尺寸是常数值,但矩阵本身的尺寸会依图像的不同而不同,通常比矩阵头的尺寸大数个数量级。因此,当在程序中传递图像并创建拷贝时,大的开销是由矩阵造成的,而不是信息头。

为了解决图像传输拖程序速度,OpenCV使用引用计数机制。其思路是让每个 Mat 对象有自己的信息头,但共享同一个矩阵。这通过让矩阵指针指向同一地址而实现。而拷贝构造函数则 只拷贝信息头和矩阵指针 ,而不拷贝矩阵。

Mat对象操作:

1、创建Mat类对象

  1. Mat A, C; // 只创建信息头部分
  2. A = imread(argv[1], CV_LOAD_IMAGE_COLOR); // 这里为矩阵开辟内存
  3.  
  4. Mat B(A); // 使用拷贝构造函数
  5. C = A; // 赋值运算符

2、创建ROI的信息头

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

3、矩阵的拷贝

  1. Mat F = A.clone();
  2. Mat G;
  3. 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=);
  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=) 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=, double beta= ) const;
  84.  
  85. void assignTo( Mat& m, int type=- ) 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=) 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=) 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=);
  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=) 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=-, bool requireContinuous=true) const;
  199.  
  200. //! returns pointer to i0-th submatrix along the dimension #0
  201. uchar* ptr(int i0=);
  202. const uchar* ptr(int i0=) 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=);
  222. template<typename _Tp> const _Tp* ptr(int i0=) 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=);
  238. template<typename _Tp> const _Tp& at(int i0=) 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=, 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[];
  315. protected:
  316. MStep& operator = (const MStep&);
  317. };
  318.  
  319. MSize size;
  320. MStep step;
  321.  
  322. protected:
  323. void initEmpty();
  324. };

OpenCV MAT基本图像容器的更多相关文章

  1. OpenCV Mat - 基本图像容器

    Mat 在2001年刚刚出现的时候,OpenCV基于 C 语言接口而建.为了在内存(memory)中存放图像,当时采用名为 IplImage 的C语言结构体,时至今日这仍出现在大多数的旧版教程和教学材 ...

  2. [学习OpenCV攻略][013][Mat - 基本图像容器]

    Mat 是一个类,由两个数据部分组成:矩阵头(包含矩阵尺寸,存储方法,存储地址等信息)和一个指向存储所有像素值的矩阵(根据所选存储方法的不同矩阵可以是不同的维数)的指针. 矩阵属于多个 Mat 对象, ...

  3. MFC:CImage显示OpenCV:Mat矩阵图像

    *************************************/ //1.读入Mat矩阵(cvMat一样),Mat img=imread("*.*");//cvLoad ...

  4. opencv学习笔记(九)Mat 访问图像像素的值

    对图像的像素进行访问,可以实现空间增强,反色,大部分图像特效系列都是基于像素操作的.图像容器Mat是一个矩阵的形式,一般情况下是二维的.单通道灰度图一般存放的是<uchar>类型,其数据存 ...

  5. 快速遍历OpenCV Mat图像数据的多种方法和性能分析 | opencv mat for loop

    本文首发于个人博客https://kezunlin.me/post/61d55ab4/,欢迎阅读! opencv mat for loop Series Part 1: compile opencv ...

  6. opencv提取截获图像(总结摘来)

    opencv提取截获图像(总结摘来) http://blog.csdn.net/wuxiaoyao12/article/details/7305865 版权声明:本文为博主原创文章,未经博主允许不得转 ...

  7. opencv学习笔记-图像对比度、亮度调节

    在数学中我们学过线性理论,在图像亮度和对比度调节中同样适用,看下面这个公式: 在图像像素中其中: 参数f(x)表示源图像像素. 参数g(x) 表示输出图像像素. 参数a(需要满足a>0)被称为增 ...

  8. OpenCV——Mat,IplImage,CvMat类型转换

    Mat,cvMat和IplImage这三种类型都可以代表和显示图像,三者区别如下 Mat类型侧重于计算,数学性较高,openCV对Mat类型的计算也进行了优化. 而CvMat和IplImage类型更侧 ...

  9. OpenCV Mat数据类型及位数总结(转载)

    OpenCV Mat数据类型及位数总结(转载) 前言 opencv中很多数据结构为了达到內存使用的最优化,通常都会用它最小上限的空间来分配变量,有的数据结构也会因为图像文件格式的关系而给予适当的变量, ...

随机推荐

  1. Android sdk环境配置

      1.环境搭建 1.1.JDK安装 1.2.Eclipse安装 1.3.Android SDK安装 1.4.ADT安装 1.5.创建AVD 详细细节 http://www.cnblogs.com/s ...

  2. reinstall ubuntu

    flickering mouse issue http://askubuntu.com/questions/310341/do-graphics-drivers-for-intel-hd-4600-e ...

  3. 初探NIOS II之hello_world

    平台背景: 操作系统:win7  64bit 开发板:DE2-115 Quartus ii:15.0及配套的NIOS ii开发平台 一.硬件系统的建立 1.在Quartus里新建工程,这是很基本的就不 ...

  4. SQL联合查询(内联、左联、右联、全联)的语法

    联合查询效率较高,举例子来说明联合查询:内联inner join .左联left outer join .右联right outer join .全联full outer join 的好处及用法. 联 ...

  5. 获取局域网中指定IP或是主机名称的所有文件夹及其搜索文件

    最近做个功能在局域网中所有指定文件,于是花了点精力完成了部分功能,先贴上 using System; using System.Collections.Generic; using System.Co ...

  6. mybatis中的#{}和${}

    #{}:相当于预处理中的占位符?. #{}里面的参数表示接收java输入参数的名称. #{}可以接受HashMap.简单类型.POJO类型的参数. 当接受简单类型的参数时,#{}里面可以是value, ...

  7. Python 小问题解决

    安装第三方源包时候,我使用的是pip,但是遇到很多问题.使用的Python 3.5,安装了VS2012 因为参考的是廖雪峰的教程,首先是PIL的安装上一直出问题. 直接使用 pip install P ...

  8. 认识Java

    java出生地:SUN Microsystems Inc<开源>     -SUN : Standford University Network java之父:James Gosling ...

  9. c++unsigned char的输出问题

    unsigned char的范围是0~255,在用cout输出的时候要显示数字的话记得进行int的强制转化才可以,否则都是输出的字符,除此之外的所有比较转换成整数在做比较吧 除此之外,在最近的项目里由 ...

  10. Objective C运行时(runtime)

    #import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...