矩阵和图像的操作
(1)cvGEMM函数
其结构
double cvGEMM(//矩阵的广义乘法运算 const CvArr* src1,//乘数矩阵 const CvArr* src2,//乘数矩阵 double alpha,//1号矩阵系数 const CvArr* src3,//加权矩阵 double beta,//2号矩阵系数 CvArr* dst,//结果矩阵 int tABC = 0//变换标记 );
tABC变换标记及其对应的含义
CV_GEMM_A_T |
转置 src1 |
CV_GEMM_B_T |
转置 src2 |
CV_GEMM_C_T |
转置 src3 |
实例代码
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { double a[3][3] = { {1,0,0}, {0,2,0}, {0,0,3} }; CvMat va=cvMat(3,3, CV_64FC1,a); cout<<"目标矩阵1:"<<endl; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) printf("%f\t",cvmGet(&va,i,j)); cout << endl; } double b[3][3] = { {1,1,1}, {1,1,1}, {1,1,1} }; CvMat vb =cvMat(3, 3, CV_64FC1, b); cout<<"目标矩阵2:"<<endl; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) printf("%f\t",cvmGet(&vb,i,j)); cout << endl; } double c[3][3] = {0,0,0,0,0,0,0,0,0}; CvMat vc = cvMat(3,3, CV_64FC1, c); cvGEMM(&va,&vb,1,&vc,1,&vc); cout << "结果矩阵:"<< endl; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) printf("%f\t",cvmGet(&vc,i,j)); cout << endl; } getchar(); return 0; }
(2)cvGetCol函数
其结构
CvMat* cvGetCol(//取出矩阵中的一列 const CvArr* arr,//目标矩阵 CvMat* submat,//矩阵指针 int col//所要取的列的索引数 );
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { double a[3][3] = { {1,0,0}, {0,2,0}, {0,0,3} }; CvMat va=cvMat(3,3, CV_64FC1,a); cout<<"目标矩阵:"<<endl; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) printf("%f\t",cvmGet(&va,i,j)); cout << endl; } CvMat vb =cvMat(3,3, CV_64FC1); CvMat vc =cvMat(3,1, CV_64FC1); vc = *(cvGetCol(&va,&vb,1)); cout << "所要取的列为:"<< endl; for(int i=0;i<3;i++) { printf("%f\t",cvmGet(&vc,i,0)); cout << endl; } getchar(); return 0; }
(3)cvGetCols函数
其结构
CvMat* cvGetCols(//取出矩阵中的一些列 const CvArr* arr,//目标矩阵 CvMat* submat,//矩阵指针 int start_col,//开始列,包含 int end_col//结束列,不包含 );
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { double a[5][5] = { {1,0,0,0,0}, {0,2,0,0,0}, {0,0,3,0,0}, {0,0,0,4,0}, {0,0,0,0,5} }; CvMat va=cvMat(5,5, CV_64FC1,a); cout<<"目标矩阵:"<<endl; for(int i=0;i<5;i++) { for(int j=0;j<5;j++) printf("%f\t",cvmGet(&va,i,j)); cout << endl; } CvMat vb =cvMat(5,5, CV_64FC1); CvMat vc =cvMat(5,5, CV_64FC1); vc = *(cvGetCols(&va,&vb,0,3)); cout << "所要取的列为:"<< endl; for(int i=0;i<vc.rows;i++) { for(int j=0;j<vc.cols;j++) printf("%f\t",cvmGet(&vc,i,j)); cout << endl; } getchar(); return 0; }
(4)cvGetDiag函数
其结构
CvMat* cvGetDiag(//取出矩阵的特定对角线 const CvArr* arr,//目标矩阵 CvMat* submat,//矩阵指针 int diag = 0//控制标量,0为主对角,1或-1为0相邻对角,以此类推。 );
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { double a[5][5] = { {1,0,0,0,6}, {0,2,0,7,0}, {0,0,3,0,0}, {0,9,0,4,0}, {8,0,0,0,5} }; CvMat va=cvMat(5,5, CV_64FC1,a); cout<<"目标矩阵:"<<endl; for(int i=0;i<5;i++) { for(int j=0;j<5;j++) printf("%f\t",cvmGet(&va,i,j)); cout << endl; } CvMat vb =cvMat(5,5, CV_64FC1); CvMat vc =cvMat(5,5, CV_64FC1); vc = *(cvGetDiag(&va,&vb,0)); cout << "所要取的对角线为:"<< endl; for(int i=0;i<vc.rows;i++) { for(int j=0;j<vc.cols;j++) printf("%f\t",cvmGet(&vc,i,j)); cout << endl; } getchar(); return 0; }
to be continued