Matrix Basics
创建矩阵:
Mat image(240, 320, CV 8UC3);
重新分配矩阵的大小
image.create(480, 640, CV 8UC3);
创建矩阵并用常量赋值
Mat A33(3, 3, CV 32F, Scalar(5));
Mat B33(3, 3, CV 32F); B33 = Scalar(5);
Mat C33 = Mat::ones(3, 3, CV 32F)*5.;
Mat D33 = Mat::zeros(3, 3, CV 32F) + 5.;
创建矩阵并用特殊量赋值
double a = CV PI/3;
Mat A22 = (Mat <float>(2, 2) <<
cos(a), -sin(a), sin(a), cos(a));
float B22data[] = fcos(a), -sin(a), sin(a), cos(a)g;
Mat B22 = Mat(2, 2, CV 32F, B22data).clone();
使用随机值初始化矩阵
randu(image, Scalar(0), Scalar(256)); // uniform dist
randn(image, Scalar(128), Scalar(10)); // Gaussian dist
将矩阵转化为其他数据结构,并且不对数据部进行拷贝操作
//此处留出接口与其他库转化
Mat image alias = image;
float* Idata=new float[480*640*3];
Mat I(480, 640, CV 32FC3, Idata);
vector<Point> iptvec(10);
Mat iP(iptvec); // iP { 10x1 CV 32SC2 matrix
IplImage* oldC0 = cvCreateImage(cvSize(320,240),16,1);
Mat newC = cvarrToMat(oldC0);
IplImage oldC1 = newC; CvMat oldC2 = newC; //此方法需要拷贝数据,效率慢,但是安全
Mat newC2 = cvarrToMat(oldC0).clone();
vector<Point2f> ptvec = Mat <Point2f>(iP);