标签:
IplImage 与 Mat
IplImage是OpenCV1中的图像存储结构体,基于C接口创建。在退出之前必须release,否则就会造成内存泄露。在一些只能使用C语言的嵌入式系统中,不得不使用。
IplImage* img = cvLoadImage("imagename.jpg",1);
Mat类内存分配是自动完成的,不必手动开辟空间(非必须),不必在不需要时释放空间。
Mat类的构成
Mat由矩阵头和一个指向存储图像矩阵的指针组成。为应该尽量避免图像的复制,加快程序运行速度,Mat的拷贝构造函数只复制信息头和矩阵指针,不复制矩阵。因此通过任何一个对象所做的改变也会影响其他对象。如果想要创建一个感兴趣的区域(Region of Interest (ROI))只需要创建包含边界信息的信息头:
Mat ROI1(matrix, Rect(x,y,width,height)); // use rectangle to set the boundary Mat ROI2 = matrix(Range:all(), Range(1,3)); // use rows and cols
Mat通过对象计数机制来实现最后一个使用它的对象自动负责清理存储空间。如果想要复制矩阵本身,可以使用:
Mat duplicate1 = matrix.clone();
Mat duplicate2;
matrix.copyTo(duplicate2);
改变duplicate1和duplicate2不会影响matrix指向的矩阵。
构造函数
Mat()
Mat(int rows, int cols, int type, const Scalar & s)
Mat(Size size, int type, const Scalar & s)
Mat(int ndims, const int* sizes, type, const Scalar & s)
Mat (const Mat &m) 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 (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::Mat(const IplImage* img, bool copyData=false)
Mat (const std::vector< _Tp > &vec, bool copyData=false)
其他构造函数可以参见OpenCV Documentation:
其他初始化方法
void create (int rows, int cols, int type) void create (Size size, int type) void create (int ndims, const int *sizes, int type)
分配新内存,但不能为矩阵设置初值。大部分需要分配空间的OpenCV函数都会从内部调用这个函数,所以在调用他们之前不用手动分配内存空间
static MatExpr eye (int rows, int cols, int type) static MatExpr eye (Size size, 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 zeros (int rows, int cols, int type) static MatExpr zeros (Size size, int type) static MatExpr zeros (int ndims, const int *sz, int type)
Matlab形式的初始化方式。MatExpr是Matrix expression representation,可以进行各种矩阵运算操作。用法如下,
Mat E = Mat::eye(4,4,CV_64F); Mat O = Mat::ones(2,2,CV_32F)*0.1;
逗号分隔式初始化
Mat C = (Mat_<double>(3,3) << 0,-1,0, -1,5,-1, 0,-1,0);
Mat clone () const void copyTo (OutputArray m) const void copyTo (OutputArray m, InputArray mask) const
拷贝矩阵,回先调用create函数分配内存空间,
格式化输出方法
5种输出风格:OpenCV默认,Python,逗号分隔,Numpy, C语言风格。事例程序和运行结果如下:
#include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> // main int main( int argc, char** argv ) { cv::Mat r = cv::Mat(10,3,CV_8UC3); cv::randu(r, cv::Scalar::all(0), cv::Scalar::all(255)); // [1] OpenCV default style std::cout << "r (OpenCV default style) = \n" << r << ";" << std::endl << std::endl; // [2] Python style // OpenCV2 // std::cout << "r (Python style) = " << format(r,"python") << ";" << std::endl << std::endl; // OpenCV3 std::cout << "r (Python style) = \n" << format(r,cv::Formatter::FMT_PYTHON) << ";" << std::endl << std::endl; // [3] Comma separated values (CSV) // OpenCV2 // std::cout << "r (CSV style) = " << format(r,"csv") << ";" << std::endl << std::endl; // OpenCV3 std::cout << "r (CSV style) = \n" << format(r,cv::Formatter::FMT_CSV) << ";" << std::endl << std::endl; // [4] Numpy style // OpenCV2 // std::cout << "r (Numpy style) = " << format(r,"numpy") << ";" << std::endl << std::endl; // OpenCV3 std::cout << "r (Numpy style) = \n" << format(r,cv::Formatter::FMT_NUMPY) << ";" << std::endl << std::endl; // [3] C style // OpenCV2 // std::cout << "r (C style) = " << format(r,"C") << ";" << std::endl << std::endl; // OpenCV3 std::cout << "r (C style) = \n" << format(r,cv::Formatter::FMT_C) << ";" << std::endl << std::endl; return 0; }
r (OpenCV default style) = [ 91, 2, 79, 179, 52, 205, 236, 8, 181; 239, 26, 248, 207, 218, 45, 183, 158, 101; 102, 18, 118, 68, 210, 139, 198, 207, 211; 181, 162, 197, 191, 196, 40, 7, 243, 230; 45, 6, 48, 173, 242, 125, 175, 90, 63; 90, 22, 112, 221, 167, 224, 113, 208, 123; 214, 35, 229, 6, 143, 138, 98, 81, 118; 187, 167, 140, 218, 178, 23, 43, 133, 154; 150, 76, 101, 8, 38, 238, 84, 47, 7; 117, 246, 163, 237, 69, 129, 60, 101, 41]; r (Python style) = [[[ 91, 2, 79], [179, 52, 205], [236, 8, 181]], [[239, 26, 248], [207, 218, 45], [183, 158, 101]], [[102, 18, 118], [ 68, 210, 139], [198, 207, 211]], [[181, 162, 197], [191, 196, 40], [ 7, 243, 230]], [[ 45, 6, 48], [173, 242, 125], [175, 90, 63]], [[ 90, 22, 112], [221, 167, 224], [113, 208, 123]], [[214, 35, 229], [ 6, 143, 138], [ 98, 81, 118]], [[187, 167, 140], [218, 178, 23], [ 43, 133, 154]], [[150, 76, 101], [ 8, 38, 238], [ 84, 47, 7]], [[117, 246, 163], [237, 69, 129], [ 60, 101, 41]]]; r (CSV style) = 91, 2, 79, 179, 52, 205, 236, 8, 181 239, 26, 248, 207, 218, 45, 183, 158, 101 102, 18, 118, 68, 210, 139, 198, 207, 211 181, 162, 197, 191, 196, 40, 7, 243, 230 45, 6, 48, 173, 242, 125, 175, 90, 63 90, 22, 112, 221, 167, 224, 113, 208, 123 214, 35, 229, 6, 143, 138, 98, 81, 118 187, 167, 140, 218, 178, 23, 43, 133, 154 150, 76, 101, 8, 38, 238, 84, 47, 7 117, 246, 163, 237, 69, 129, 60, 101, 41 ; r (Numpy style) = array([[[ 91, 2, 79], [179, 52, 205], [236, 8, 181]], [[239, 26, 248], [207, 218, 45], [183, 158, 101]], [[102, 18, 118], [ 68, 210, 139], [198, 207, 211]], [[181, 162, 197], [191, 196, 40], [ 7, 243, 230]], [[ 45, 6, 48], [173, 242, 125], [175, 90, 63]], [[ 90, 22, 112], [221, 167, 224], [113, 208, 123]], [[214, 35, 229], [ 6, 143, 138], [ 98, 81, 118]], [[187, 167, 140], [218, 178, 23], [ 43, 133, 154]], [[150, 76, 101], [ 8, 38, 238], [ 84, 47, 7]], [[117, 246, 163], [237, 69, 129], [ 60, 101, 41]]], dtype=‘uint8‘); r (C style) = { 91, 2, 79, 179, 52, 205, 236, 8, 181, 239, 26, 248, 207, 218, 45, 183, 158, 101, 102, 18, 118, 68, 210, 139, 198, 207, 211, 181, 162, 197, 191, 196, 40, 7, 243, 230, 45, 6, 48, 173, 242, 125, 175, 90, 63, 90, 22, 112, 221, 167, 224, 113, 208, 123, 214, 35, 229, 6, 143, 138, 98, 81, 118, 187, 167, 140, 218, 178, 23, 43, 133, 154, 150, 76, 101, 8, 38, 238, 84, 47, 7, 117, 246, 163, 237, 69, 129, 60, 101, 41};
标签:
原文地址:http://www.cnblogs.com/Xiaoyan-Li/p/5677309.html