分配矩阵空间:
CvMat* cvCreateMat(int rows, int cols, int type);
释放矩阵空间:
cvReleaseMat(&M);
数据类型宏定义:
CV_<bit_depth>(S|U|F)C<number_of_channels>
复制矩阵:
CvMat* M2; M2=cvCloneMat(M1);
初始化矩阵:
double a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
要么
CvMat Ma=cvMat(3, 4, CV_64FC1, a);
或者
CvMat Ma;
cvInitMatHeader(&Ma, 3, 4, CV_64FC1, a);
间接存取矩阵元素:
cvmSet(M,i,j,2.0);
t = cvmGet(M,i,j);
直接存取,校正字节任意:
CvMat* M = cvCreateMat(4,4,CV_32FC1);
int step = M->step/sizeof(float);
float *data = M->data.fl;
(data+i*step)[j] = 3.0;
矩阵间的运算:
CvMat *Ma, *Mb, *Mc; cvAdd(Ma, Mb, Mc); // Ma+Mb -> Mc cvSub(Ma, Mb, Mc); // Ma-Mb -> Mc cvMatMul(Ma, Mb, Mc); // Ma*Mb -> Mc
矩阵元素间的运算:
CvMat *Ma, *Mb, *Mc; cvMul(Ma, Mb, Mc); // Ma.*Mb -> Mc cvDiv(Ma, Mb, Mc); // Ma./Mb -> Mc cvAddS(Ma, cvScalar(-10.0), Mc); // Ma.-10 -> Mc
单矩阵操作:
CvMat *Ma, *Mb; cvTranspose(Ma, Mb); // transpose(Ma) -> Mb (不能对自身进行转置) CvScalar t = cvTrace(Ma); // trace(Ma) -> t.val[0] double d = cvDet(Ma); // det(Ma) -> d cvInvert(Ma, Mb); // inv(Ma) -> Mb
向量乘积:
double va[] = {1, 2, 3}; double vb[] = {0, 0, 1}; double vc[3]; CvMat Va=cvMat(3, 1, CV_64FC1, va); CvMat Vb=cvMat(3, 1, CV_64FC1, vb); CvMat Vc=cvMat(3, 1, CV_64FC1, vc); double res=cvDotProduct(&Va,&Vb); // 点乘:Va . Vb -> res cvCrossProduct(&Va, &Vb, &Vc); // 向量积: Va x Vb -> Vcend{verbatim}
时间: 2024-10-26 05:28:03