目录
Mat初始化函数
//默认构造函数 cv::Mat //拷贝构造 cv::Mat(const Mat& mat) // 指定行和列的拷贝构造 cv::Mat(const Mat& mat, const cv::Range& rows, const cv::Range& cols); // 指定ROI(感兴趣的区域)的拷贝构造 cv::Mat(const Mat& mat, const cv::Rect& roi); // 使用n维数组中指定范围内的数据的拷贝构造 cv::Mat(const Mat& mat, const cv::Range* ranges); // 指定类型的二维数组 cv::Mat(int rows, int cols, int type); // 指定类型的二维数据,并指定初始化值 cv::Mat(int rows, int cols, int type, const Scalar& s); // 使用预先存在的数据,并指定类型的二维数组 cv::Mat(int rows, int cols, int type, void *data, size_t step = AUTO_STEP); // 指定大小和类型的二维数组 cv::Mat(cv::Size sz, int type) // 指定大小和类型的二维数组,并指定初始值 cv::Mat(cv::Size sz, int type, const Scalar& s); // 使用预先存在的数据,指定类型的二维数组 cv::Mat(cv::Size sz, int type, void *data, size_t step = AUTO_STEP); // 指定类型的多维数据 cv::Mat(int ndims, const int *sizes, int type); // 指定类型的多维数据,并初始值 cv::Mat(int ndims, const int *sizes, int type, const Scalar& s); // 使用预先存在的数据,指定类型的多维数组 cv::Mat(int ndims, const int* sizes, int type, void* data, size_t step = AUTO_STEP); //mat的模板构造函数 // 构造如同cv::Vec所指定的数据类型、大小为n的一维数组 cv::Mat(const cv::Vec<T, n>& vec, bool copyData = true); // 构造如同cv::Matx所指定的数据类型、大小为mXn的二维数组 cv::Mat(const cv::Matx<T, m, n>& vec, bool copyData = true); // 构造STL的vector所指定的数据类型的一维数组 cv::Mat mat(const std::vector<T>& vec, bool copyData = true); //构造mat的静态方法 // 使用zeros()函数定义指定大小(rows X cols)和类型(type)的cv::Mat(全为0)的矩阵 cv::Mat::zeros(int rows, int cols, int type); // 使用ones()函数定义指定大小(rows X cols)和类型(type)的cv::Mat(全为1)的矩阵 cv::Mat::ones(int rows, int cols, int type); // 使用eye()函数定义指定大小(rows X cols)和类型(type)的单位矩阵 cv::Mat::eye(int rows, int cols, int type);
访问像素
方法一:使用at函数
//直接访问 at函数 cv::Mat mat = cv::Mat::eye(10, 10, 32FC1); m.at<float>(3, 3) //多通道数组操作 cv::Mat mat = cv::Mat::eye(10, 10, 32FC2); m.at<cv::Vec2f>(3, 3)[0]; m.at<cv::Vec2f>(3, 3)[1];
方法二:使用迭代器
int sz[3] = {4, 4, 4}; Mat m(3, sz, CV_32FC3); cv::MatIterator<cv::Vec3b> it = m.begin(); while(it != m.end()) { cout << (*)it[0]; it++; }