openCV学习之路(3-1)---解析Mat类构造函数

Mat类:

  关于 Mat ,首先要知道的是你不必再手动地(1)为其开辟空间(2)在不需要时立即将空间释放。但手动地做还是可以的:大多数OpenCV函数仍会手动地为输出数据开辟空间。当传递一个已经存在的 Mat 对象时,开辟好的矩阵空间会被重用。也就是说,我们每次都使用大小正好的内存来完成任务。
  基本上讲 Mat 是一个类,由两个数据部分组成:矩阵头(包含矩阵尺寸,存储方法,存储地址等信息)一个指向存储所有像素值的矩阵(根据所选存储方法的不同矩阵可以是不同的维数)的指针。矩阵头的尺寸是常数值,但矩阵本身的尺寸会依图像的不同而不同,通常比矩阵头的尺寸大数个数量级。因此,当在程序中传递图像并创建拷贝时,大的开销是由矩阵造成的,而不是信息头。OpenCV是一个图像处理库,囊括了大量的图像处理函数,为了解决问题通常要使用库中的多个函数,因此在函数中传递图像是家常便饭。同时不要忘了我们正在讨论的是计算量很大的图像处理算法,因此,除非万不得已,我们不应该拷贝 大 的图像,因为这会降低程序速度。
  为了搞定这个问题,OpenCV使用引用计数机制。其思路是让每个 Mat 对象有自己的信息头,但共享同一个矩阵。这通过让矩阵指针指向同一地址而实现。而拷贝构造函数则 只拷贝信息头和矩阵指针 ,而不拷贝矩阵。

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

构造函数:

Mat();
These are various constructors that form a matrix. As noted in the AutomaticAllocation, often the default constructor is enough, and the proper matrix will be allocated by an OpenCV function.The constructed matrix can further be assigned to another matrix or matrix expression or can be allocated with Mat::create . In the former case, the old content is de-referenced.
这些是形成矩阵的各种构造函数。 正如在 AutomaticAllocation 中所指出的,通常默认构造函数就足够了,并且适当的矩阵将由 OpenCV 函数分配。构造的矩阵可以进一步分配给另一个矩阵或矩阵表达式,或者可以使用 Mat::create 进行分配。 在前一种情况下,旧内容被取消引用。

Mat();

Mat(int rows, int cols, int type);

/** 
	@overload
	@param rows Number of rows in a 2D array.
	@param rows 二维数组中的行数。
	
	@param cols Number of columns in a 2D array.
	@param cols 二维数组中的列数。
	
	@param type Array type. Use CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices, or CV_8UC(n), ..., CV_64FC(n) to create multi-channel (up to CV_CN_MAX channels) matrices.
	@param type 数组类型。 使用 CV_8UC1, ..., CV_64FC4 创建 1-4 通道矩阵,或使用 CV_8UC(n), ..., CV_64FC(n) 创建多通道(最多 CV_CN_MAX 个通道)矩阵。
	
	@param size 2D array size: Size(cols, rows) . In the Size() 	constructor, the number of rows and the number of columns go in the reverse order.
	@param size 2D 数组大小: Size(cols, rows) 。 在 Size() 构造函数中,行数和列数以相反的顺序排列。
	
    @param s An optional value to initialize each matrix element with. To set all the matrix elements to the particular value after the construction, use the assignment operator Mat::operator=(const Scalar& value) .
    @param s 用于初始化每个矩阵元素的可选值。 要在构造后将所有矩阵元素设置为特定值,请使用赋值运算符 Mat::operator=(const Scalar& value)。
    
    @param ndims Array dimensionality.
    @param ndims 数组维度。
    
    @param sizes Array of integers specifying an n-dimensional array shape.
    @param sizes 指定 n 维数组形状的整数数组。
    
    @param m Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied by these constructors. Instead, the header pointing to m data or its sub-array is constructed and associated with it. The reference counter, if any, is incremented. So, when you modify the matrix formed using such a constructor, you also modify the corresponding elements of m . If you want to have an independent copy of the sub-array, use Mat::clone() .
    @param m (整体或部分)分配给构造矩阵的数组。 这些构造函数不会复制任何数据。 相反,指向 m 个数据或其子数组的标头被构造并与之关联。 引用计数器(如果有)会递增。 因此,当您修改使用此类构造函数形成的矩阵时,您也修改了 m 的相应元素。 如果您想拥有子数组的独立副本,请使用 Mat::clone() 。
    
    @param data Pointer to the user data. Matrix constructors that take data and step parameters do not allocate matrix data. Instead, they just initialize the matrix header that points to the specified data, which means that no data is copied. This operation is very efficient and can be used to process external data using OpenCV functions. The external data is not automatically deallocated, so you should take care of it.
    @param data 指向用户数据的指针。 采用数据和步骤参数的矩阵构造函数不分配矩阵数据。 相反,它们只是初始化指向指定数据的矩阵头,这意味着没有数据被复制。 此操作非常高效,可用于使用 OpenCV 函数处理外部数据。 外部数据不会自动释放,因此您应该注意它。
    
    @param step Number of bytes each matrix row occupies. The value should include the padding bytes at the end of each row, if any. If the parameter is missing (set to AUTO_STEP ), no padding is assumed and the actual step is calculated as cols*elemSize(). See **Mat::elemSize**.
    @param step 每个矩阵行占用的字节数。 该值应包括每行末尾的填充字节(如果有)。 如果缺少参数(设置为 AUTO_STEP ),则假定没有填充,实际步长计算为 cols*elemSize()。 见 **Mat::elemSize**。

	@param steps Array of ndims-1 steps in case of a multi-dimensional array (the last step is always set to the element size). If not specified, the matrix is assumed to be continuous.
	@param steps 在多维数组的情况下,ndims-1 步骤的数组(最后一步始终设置为元素大小)。 如果未指定,则假定矩阵是连续的。
	
	@param rowRange Range of the m rows to take. As usual, the range start is inclusive and the range end is exclusive. Use Range::all() to take all the rows.
	@param rowRange 要取的 m 行的范围。 像往常一样,范围开始是包容性的,范围结束是排斥性的。 使用 Range::all() 获取所有行。

	@param colRange Range of the m columns to take. Use Range::all() to take all the columns.
	@param colRange 要采用的 m 列的范围。 使用 Range::all() 获取所有列。
	@param roi Region of interest.
	@param roi 感兴趣的区域。

	@param ranges Array of selected ranges of m along each dimensionality.
	@param 范围 沿每个维度选择的 m 范围数组。

	@param vec STL vector whose elements form the matrix. The matrix has a single column and the number of rows equal to the number of vector elements. Type of the matrix matches the type of vector elements. The constructor can handle arbitrary types, for which there is a properly declared DataType . This means that the vector elements must be primitive numbers or uni-type numerical tuples of numbers. Mixed-type structures are not supported. The corresponding constructor is explicit. Since STL vectors are not automatically converted to Mat instances, you should write Mat(vec) explicitly. Unless you copy the data into the matrix ( copyData=true ), no new elements will be added to the vector because it can potentially yield vector data reallocation, and, thus,the matrix data pointer will be invalid.
	@param vec STL 向量,其元素构成矩阵。 矩阵只有一列,行数等于向量元素的数量。 矩阵的类型与向量元素的类型相匹配。 构造函数可以处理任意类型,其中有一个正确声明的 DataType 。 这意味着向量元素必须是原始数字或数字的单类型数字元组。 不支持混合类型的结构。 相应的构造函数是显式的。 由于 STL 向量不会自动转换为 Mat 实例,因此您应该显式编写 Mat(vec)。 除非您将数据复制到矩阵中( copyData=true ),否则不会将新元素添加到向量中,因为它可能会产生向量数据重新分配,因此矩阵数据指针将无效。

	@param copyData Flag to specify whether the underlying data of the STL vector should be copied to (true) or shared with (false) the newly constructed matrix. When the data is copied, the allocated buffer is managed using Mat reference counting mechanism. While the data is shared,the reference counter is NULL, and you should not deallocate the data until the matrix is not destructed.
	@param copyData 标志指定 STL 向量的底层数据是否应该复制到 (true) 或与 (false) 新构建的矩阵共享。 复制数据时,分配的缓冲区使用 Mat 引用计数机制进行管理。 共享数据时,引用计数器为 NULL,在矩阵未被破坏之前,您不应释放数据。
*/
 	Mat ()

 	Mat (int rows, int cols, int type)
  	
 	Mat (int ndims, const int *sizes, int type, const Scalar &s)
 	
 	Mat (Size size, int type)
 
 	Mat (int rows, int cols, int type, const Scalar &s)
 
 	Mat (Size size, int type, const Scalar &s)
 
 	Mat (int ndims, const int *sizes, int type)

 	Mat (const Mat &m)
 
 	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=0)
 
 	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)

	Mat (const Mat& m, const std::vector<Range>& ranges);

	template<typename _Tp> explicit Mat (const std::vector<_Tp>& vec, bool copyData=false);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

工地码哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值