OpenCV MAT基本图像容器
参考博客:
OpenCv中cv::Mat和IplImage,CvMat之间的转换
Mat类型较CvMat和IplImage有更强的矩阵运算能力,支持常见的矩阵运算(参照Matlab中的各种矩阵运算),所以将IplImage类型和CvMat类型转换为Mat类型更易于数据处理。
关于 Mat ,首先要知道的是你不必再手动地(1)为其开辟空间(2)在不需要时立即将空间释放。但手动地做还是可以的:大多数OpenCV函数仍会手动地为输出数据开辟空间。当传递一个已经存在的 Mat 对象时,开辟好的矩阵空间会被重用。也就是说,我们每次都使用大小正好的内存来完成任务。
基本上讲 Mat 是一个类,由两个数据部分组成:矩阵头(包含矩阵尺寸,存储方法,存储地址等信息)和一个指向存储所有像素值的矩阵(根据所选存储方法的不同矩阵可以是不同的维数)的指针。矩阵头的尺寸是常数值,但矩阵本身的尺寸会依图像的不同而不同,通常比矩阵头的尺寸大数个数量级。因此,当在程序中传递图像并创建拷贝时,大的开销是由矩阵造成的,而不是信息头。
为了解决图像传输拖程序速度,OpenCV使用引用计数机制。其思路是让每个 Mat 对象有自己的信息头,但共享同一个矩阵。这通过让矩阵指针指向同一地址而实现。而拷贝构造函数则 只拷贝信息头和矩阵指针 ,而不拷贝矩阵。
Mat对象操作:
1、创建Mat类对象
- Mat A, C; // 只创建信息头部分
- A = imread(argv[1], CV_LOAD_IMAGE_COLOR); // 这里为矩阵开辟内存
- Mat B(A); // 使用拷贝构造函数
- C = A; // 赋值运算符
2、创建ROI的信息头
- Mat D (A, Rect(10, 10, 100, 100) ); // using a rectangle
- Mat E = A(Range:all(), Range(1,3)); // using row and column boundaries
3、矩阵的拷贝
- 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; 每行数据字节数
- class CV_EXPORTS Mat
- {
- public:
- //! 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);
- //! copy constructor
- Mat(const Mat& m);
- //! constructor for matrix headers pointing to user-allocated data
- Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP);
- Mat(Size size, int type, void* data, size_t step=AUTO_STEP);
- Mat(int ndims, const int* sizes, int type, void* data, const size_t* steps=);
- //! creates a matrix header for a part of the bigger matrix
- Mat(const Mat& m, const Range& rowRange, const Range& colRange=Range::all());
- Mat(const Mat& m, const Rect& roi);
- Mat(const Mat& m, const Range* ranges);
- //! converts old-style CvMat to the new matrix; the data is not copied by default
- Mat(const CvMat* m, bool copyData=false);
- //! converts old-style CvMatND to the new matrix; the data is not copied by default
- Mat(const CvMatND* m, bool copyData=false);
- //! converts old-style IplImage to the new matrix; the data is not copied by default
- Mat(const IplImage* img, bool copyData=false);
- //! builds matrix from std::vector with or without copying the data
- template<typename _Tp> explicit Mat(const vector<_Tp>& vec, bool copyData=false);
- //! builds matrix from cv::Vec; the data is copied by default
- template<typename _Tp, int n> explicit Mat(const Vec<_Tp, n>& vec, bool copyData=true);
- //! builds matrix from cv::Matx; the data is copied by default
- template<typename _Tp, int m, int n> explicit Mat(const Matx<_Tp, m, n>& mtx, bool copyData=true);
- //! builds matrix from a 2D point
- template<typename _Tp> explicit Mat(const Point_<_Tp>& pt, bool copyData=true);
- //! builds matrix from a 3D point
- template<typename _Tp> explicit Mat(const Point3_<_Tp>& pt, bool copyData=true);
- //! builds matrix from comma initializer
- template<typename _Tp> explicit Mat(const MatCommaInitializer_<_Tp>& commaInitializer);
- //! download data from GpuMat
- explicit Mat(const gpu::GpuMat& m);
- //! destructor - calls release()
- ~Mat();
- //! assignment operators
- Mat& operator = (const Mat& m);
- Mat& operator = (const MatExpr& expr);
- //! returns a new matrix header for the specified row
- Mat row(int y) const;
- //! returns a new matrix header for the specified column
- Mat col(int x) const;
- //! ... for the specified row span
- Mat rowRange(int startrow, int endrow) const;
- Mat rowRange(const Range& r) const;
- //! ... for the specified column span
- Mat colRange(int startcol, int endcol) const;
- Mat colRange(const Range& r) const;
- //! ... for the specified diagonal
- // (d=0 - the main diagonal,
- // >0 - a diagonal from the lower half,
- // <0 - a diagonal from the upper half)
- Mat diag(int d=) const;
- //! constructs a square diagonal matrix which main diagonal is vector "d"
- static Mat diag(const Mat& d);
- //! returns deep copy of the matrix, i.e. the data is copied
- Mat clone() const;
- //! copies the matrix content to "m".
- // It calls m.create(this->size(), this->type()).
- void copyTo( OutputArray m ) const;
- //! copies those matrix elements to "m" that are marked with non-zero mask elements.
- void copyTo( OutputArray m, InputArray mask ) const;
- //! converts matrix to another datatype with optional scalng. See cvConvertScale.
- void convertTo( OutputArray m, int rtype, double alpha=, double beta= ) const;
- void assignTo( Mat& m, int type=- ) const;
- //! sets every matrix element to s
- Mat& operator = (const Scalar& s);
- //! sets some of the matrix elements to s, according to the mask
- Mat& setTo(InputArray value, InputArray mask=noArray());
- //! creates alternative matrix header for the same data, with different
- // number of channels and/or different number of rows. see cvReshape.
- Mat reshape(int cn, int rows=) const;
- Mat reshape(int cn, int newndims, const int* newsz) const;
- //! matrix transposition by means of matrix expressions
- MatExpr t() const;
- //! matrix inversion by means of matrix expressions
- MatExpr inv(int method=DECOMP_LU) const;
- //! per-element matrix multiplication by means of matrix expressions
- MatExpr mul(InputArray m, double scale=) const;
- //! computes cross-product of 2 3D vectors
- Mat cross(InputArray m) const;
- //! computes dot-product
- double dot(InputArray m) const;
- //! 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);
- //! allocates new matrix data unless the matrix already has specified size and type.
- // previous data is unreferenced if needed.
- void create(int rows, int cols, int type);
- void create(Size size, int type);
- void create(int ndims, const int* sizes, int type);
- //! increases the reference counter; use with care to avoid memleaks
- void addref();
- //! decreases reference counter;
- // deallocates the data when reference counter reaches 0.
- void release();
- //! deallocates the matrix data
- void deallocate();
- //! internal use function; properly re-allocates _size, _step arrays
- void copySize(const Mat& m);
- //! reserves enough space to fit sz hyper-planes
- void reserve(size_t sz);
- //! resizes matrix to the specified number of hyper-planes
- void resize(size_t sz);
- //! resizes matrix to the specified number of hyper-planes; initializes the newly added elements
- void resize(size_t sz, const Scalar& s);
- //! internal function
- void push_back_(const void* elem);
- //! adds element to the end of 1d matrix (or possibly multiple elements when _Tp=Mat)
- template<typename _Tp> void push_back(const _Tp& elem);
- template<typename _Tp> void push_back(const Mat_<_Tp>& elem);
- void push_back(const Mat& m);
- //! removes several hyper-planes from bottom of the matrix
- void pop_back(size_t nelems=);
- //! locates matrix header within a parent matrix. See below
- void locateROI( Size& wholeSize, Point& ofs ) const;
- //! moves/resizes the current matrix ROI inside the parent matrix.
- Mat& adjustROI( int dtop, int dbottom, int dleft, int dright );
- //! extracts a rectangular sub-matrix
- // (this is a generalized form of row, rowRange etc.)
- Mat operator()( Range rowRange, Range colRange ) const;
- Mat operator()( const Rect& roi ) const;
- Mat operator()( const Range* ranges ) const;
- //! converts header to CvMat; no data is copied
- operator CvMat() const;
- //! converts header to CvMatND; no data is copied
- operator CvMatND() const;
- //! converts header to IplImage; no data is copied
- operator IplImage() const;
- template<typename _Tp> operator vector<_Tp>() const;
- template<typename _Tp, int n> operator Vec<_Tp, n>() const;
- template<typename _Tp, int m, int n> operator Matx<_Tp, m, n>() const;
- //! returns true iff the matrix data is continuous
- // (i.e. when there are no gaps between successive rows).
- // similar to CV_IS_MAT_CONT(cvmat->type)
- bool isContinuous() const;
- //! returns true if the matrix is a submatrix of another matrix
- bool isSubmatrix() const;
- //! returns element size in bytes,
- // similar to CV_ELEM_SIZE(cvmat->type)
- size_t elemSize() const;
- //! returns the size of element channel in bytes.
- size_t elemSize1() const;
- //! returns element type, similar to CV_MAT_TYPE(cvmat->type)
- int type() const;
- //! returns element type, similar to CV_MAT_DEPTH(cvmat->type)
- int depth() const;
- //! returns element type, similar to CV_MAT_CN(cvmat->type)
- int channels() const;
- //! returns step/elemSize1()
- size_t step1(int i=) const;
- //! returns true if matrix data is NULL
- bool empty() const;
- //! returns the total number of matrix elements
- size_t total() const;
- //! returns N if the matrix is 1-channel (N x ptdim) or ptdim-channel (1 x N) or (N x 1); negative number otherwise
- int checkVector(int elemChannels, int depth=-, bool requireContinuous=true) const;
- //! returns pointer to i0-th submatrix along the dimension #0
- uchar* ptr(int i0=);
- const uchar* ptr(int i0=) const;
- //! returns pointer to (i0,i1) submatrix along the dimensions #0 and #1
- uchar* ptr(int i0, int i1);
- const uchar* ptr(int i0, int i1) const;
- //! returns pointer to (i0,i1,i3) submatrix along the dimensions #0, #1, #2
- uchar* ptr(int i0, int i1, int i2);
- const uchar* ptr(int i0, int i1, int i2) const;
- //! returns pointer to the matrix element
- uchar* ptr(const int* idx);
- //! returns read-only pointer to the matrix element
- const uchar* ptr(const int* idx) const;
- template<int n> uchar* ptr(const Vec<int, n>& idx);
- template<int n> const uchar* ptr(const Vec<int, n>& idx) const;
- //! template version of the above method
- template<typename _Tp> _Tp* ptr(int i0=);
- template<typename _Tp> const _Tp* ptr(int i0=) const;
- template<typename _Tp> _Tp* ptr(int i0, int i1);
- template<typename _Tp> const _Tp* ptr(int i0, int i1) const;
- template<typename _Tp> _Tp* ptr(int i0, int i1, int i2);
- template<typename _Tp> const _Tp* ptr(int i0, int i1, int i2) const;
- template<typename _Tp> _Tp* ptr(const int* idx);
- template<typename _Tp> const _Tp* ptr(const int* idx) const;
- template<typename _Tp, int n> _Tp* ptr(const Vec<int, n>& idx);
- template<typename _Tp, int n> const _Tp* ptr(const Vec<int, n>& idx) const;
- //! the same as above, with the pointer dereferencing
- template<typename _Tp> _Tp& at(int i0=);
- template<typename _Tp> const _Tp& at(int i0=) const;
- template<typename _Tp> _Tp& at(int i0, int i1);
- template<typename _Tp> const _Tp& at(int i0, int i1) const;
- template<typename _Tp> _Tp& at(int i0, int i1, int i2);
- template<typename _Tp> const _Tp& at(int i0, int i1, int i2) const;
- template<typename _Tp> _Tp& at(const int* idx);
- template<typename _Tp> const _Tp& at(const int* idx) const;
- template<typename _Tp, int n> _Tp& at(const Vec<int, n>& idx);
- template<typename _Tp, int n> const _Tp& at(const Vec<int, n>& idx) const;
- //! special versions for 2D arrays (especially convenient for referencing image pixels)
- template<typename _Tp> _Tp& at(Point pt);
- template<typename _Tp> const _Tp& at(Point pt) const;
- //! template methods for iteration over matrix elements.
- // the iterators take care of skipping gaps in the end of rows (if any)
- template<typename _Tp> MatIterator_<_Tp> begin();
- template<typename _Tp> MatIterator_<_Tp> end();
- template<typename _Tp> MatConstIterator_<_Tp> begin() const;
- template<typename _Tp> MatConstIterator_<_Tp> end() const;
- enum { MAGIC_VAL=0x42FF0000, AUTO_STEP=, CONTINUOUS_FLAG=CV_MAT_CONT_FLAG, SUBMATRIX_FLAG=CV_SUBMAT_FLAG };
- /*! includes several bit-fields:
- - the magic signature
- - continuity flag
- - depth
- - number of channels
- */
- int flags;
- //! the matrix dimensionality, >= 2
- int dims;
- //! the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
- int rows, cols;
- //! pointer to the data
- uchar* data;
- //! pointer to the reference counter;
- // when matrix points to user-allocated data, the pointer is NULL
- int* refcount;
- //! helper fields used in locateROI and adjustROI
- uchar* datastart;
- uchar* dataend;
- uchar* datalimit;
- //! custom allocator
- MatAllocator* allocator;
- struct CV_EXPORTS MSize
- {
- MSize(int* _p);
- Size operator()() const;
- const int& operator[](int i) const;
- int& operator[](int i);
- operator const int*() const;
- bool operator == (const MSize& sz) const;
- bool operator != (const MSize& sz) const;
- int* p;
- };
- struct CV_EXPORTS MStep
- {
- MStep();
- MStep(size_t s);
- const size_t& operator[](int i) const;
- size_t& operator[](int i);
- operator size_t() const;
- MStep& operator = (size_t s);
- size_t* p;
- size_t buf[];
- protected:
- MStep& operator = (const MStep&);
- };
- MSize size;
- MStep step;
- protected:
- void initEmpty();
- };
OpenCV MAT基本图像容器的更多相关文章
- OpenCV Mat - 基本图像容器
Mat 在2001年刚刚出现的时候,OpenCV基于 C 语言接口而建.为了在内存(memory)中存放图像,当时采用名为 IplImage 的C语言结构体,时至今日这仍出现在大多数的旧版教程和教学材 ...
- [学习OpenCV攻略][013][Mat - 基本图像容器]
Mat 是一个类,由两个数据部分组成:矩阵头(包含矩阵尺寸,存储方法,存储地址等信息)和一个指向存储所有像素值的矩阵(根据所选存储方法的不同矩阵可以是不同的维数)的指针. 矩阵属于多个 Mat 对象, ...
- MFC:CImage显示OpenCV:Mat矩阵图像
*************************************/ //1.读入Mat矩阵(cvMat一样),Mat img=imread("*.*");//cvLoad ...
- opencv学习笔记(九)Mat 访问图像像素的值
对图像的像素进行访问,可以实现空间增强,反色,大部分图像特效系列都是基于像素操作的.图像容器Mat是一个矩阵的形式,一般情况下是二维的.单通道灰度图一般存放的是<uchar>类型,其数据存 ...
- 快速遍历OpenCV Mat图像数据的多种方法和性能分析 | opencv mat for loop
本文首发于个人博客https://kezunlin.me/post/61d55ab4/,欢迎阅读! opencv mat for loop Series Part 1: compile opencv ...
- opencv提取截获图像(总结摘来)
opencv提取截获图像(总结摘来) http://blog.csdn.net/wuxiaoyao12/article/details/7305865 版权声明:本文为博主原创文章,未经博主允许不得转 ...
- opencv学习笔记-图像对比度、亮度调节
在数学中我们学过线性理论,在图像亮度和对比度调节中同样适用,看下面这个公式: 在图像像素中其中: 参数f(x)表示源图像像素. 参数g(x) 表示输出图像像素. 参数a(需要满足a>0)被称为增 ...
- OpenCV——Mat,IplImage,CvMat类型转换
Mat,cvMat和IplImage这三种类型都可以代表和显示图像,三者区别如下 Mat类型侧重于计算,数学性较高,openCV对Mat类型的计算也进行了优化. 而CvMat和IplImage类型更侧 ...
- OpenCV Mat数据类型及位数总结(转载)
OpenCV Mat数据类型及位数总结(转载) 前言 opencv中很多数据结构为了达到內存使用的最优化,通常都会用它最小上限的空间来分配变量,有的数据结构也会因为图像文件格式的关系而给予适当的变量, ...
随机推荐
- Android sdk环境配置
1.环境搭建 1.1.JDK安装 1.2.Eclipse安装 1.3.Android SDK安装 1.4.ADT安装 1.5.创建AVD 详细细节 http://www.cnblogs.com/s ...
- reinstall ubuntu
flickering mouse issue http://askubuntu.com/questions/310341/do-graphics-drivers-for-intel-hd-4600-e ...
- 初探NIOS II之hello_world
平台背景: 操作系统:win7 64bit 开发板:DE2-115 Quartus ii:15.0及配套的NIOS ii开发平台 一.硬件系统的建立 1.在Quartus里新建工程,这是很基本的就不 ...
- SQL联合查询(内联、左联、右联、全联)的语法
联合查询效率较高,举例子来说明联合查询:内联inner join .左联left outer join .右联right outer join .全联full outer join 的好处及用法. 联 ...
- 获取局域网中指定IP或是主机名称的所有文件夹及其搜索文件
最近做个功能在局域网中所有指定文件,于是花了点精力完成了部分功能,先贴上 using System; using System.Collections.Generic; using System.Co ...
- mybatis中的#{}和${}
#{}:相当于预处理中的占位符?. #{}里面的参数表示接收java输入参数的名称. #{}可以接受HashMap.简单类型.POJO类型的参数. 当接受简单类型的参数时,#{}里面可以是value, ...
- Python 小问题解决
安装第三方源包时候,我使用的是pip,但是遇到很多问题.使用的Python 3.5,安装了VS2012 因为参考的是廖雪峰的教程,首先是PIL的安装上一直出问题. 直接使用 pip install P ...
- 认识Java
java出生地:SUN Microsystems Inc<开源> -SUN : Standford University Network java之父:James Gosling ...
- c++unsigned char的输出问题
unsigned char的范围是0~255,在用cout输出的时候要显示数字的话记得进行int的强制转化才可以,否则都是输出的字符,除此之外的所有比较转换成整数在做比较吧 除此之外,在最近的项目里由 ...
- Objective C运行时(runtime)
#import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...