矩阵和图像的操作
(1)cvSetIdentity函数
其结构
void cvSetIdentity(//将矩阵行与列相等的元素置为1,其余元素置为0 CvArr* arr//目标矩阵 );
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { double a[3][3] = { {1,2,3}, {4,5,6}, {7,8,9} }; 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; } cvSetIdentity(&va); 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; } getchar(); return 0; }
(2)cvSolve函数
其结构
int cvSolve(//求解线性方程组解 src*dst = src2 const CvArr* src1,//系数矩阵 const CvArr* src2,//常数矩阵 CvArr* dst,//解矩阵 int method = CV_LU//使用方法 );
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { cout<< "计算A*X = B中的解X:" <<endl; double a[3][3] = { {1,0,0}, {0,2,0}, {0,0,2} }; CvMat va = cvMat(3,3,CV_64FC1,a); cout<<"A矩阵:"<<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]={1,2,2}; CvMat vb = cvMat(3,1,CV_64FC1,b); cout<<"B矩阵:"<<endl; for(int i=0;i<3;i++) { printf("%f\t",cvmGet(&vb,i,0)); cout << endl; } double c[3]={0,0,0}; CvMat vc = cvMat(3,1,CV_64FC1,c); cvSolve(&va,&vb,&vc); cout<<"解为:"<<endl; for(int i=0;i<3;i++) { printf("%f\t",cvmGet(&vc,i,0)); cout << endl; } getchar(); return 0; }
(3)cvSplit函数
其结构
void cvSplit(//分解多通道图像为各个单通道 const CvArr* src,//目标图像 CvArr* dst0,//单通道图像1 CvArr* dst1,//单通道图像2 CvArr* dst2,//单通道图像3 CvArr* dst3//单通道图像4 );
#include <cv.h> #include <highgui.h> #include <stdio.h> int main() { IplImage *src1,*dst1,*dst2,*dst3,*dst4; src1=cvLoadImage("3.jpg",1); dst1 = cvCreateImage(cvSize(src1->width, src1->height), IPL_DEPTH_8U, 1); dst2 = cvCreateImage(cvSize(src1->width, src1->height), IPL_DEPTH_8U, 1); dst3 = cvCreateImage(cvSize(src1->width, src1->height), IPL_DEPTH_8U, 1); cvSplit(src1, dst1, dst2, dst3, 0); cvShowImage("1",src1); cvShowImage("2",dst1); cvShowImage("3",dst2); cvShowImage("4",dst3); cvWaitKey(); return 0; }
(4)cvSub函数
其结构
void cvSub(//两个矩阵做减法 const CvArr* src1,//被减矩阵 const CvArr* src2,//减矩阵 CvArr* dst,//结果矩阵 const CvArr* mask = NULL//矩阵开关 );
#include <cv.h> #include <highgui.h> #include <stdio.h> int main() { IplImage *src1,*src2,*src3; src1 = cvLoadImage("3.jpg"); src2 = cvLoadImage("1.jpg"); src3 = cvLoadImage("7.jpg"); cvSub(src1,src2,src3); cvShowImage("1",src1); cvShowImage("2",src2); cvShowImage("3",src3); cvWaitKey(); return 0; }
(5)cvSubS函数
其结构
void cvSubS(//矩阵和值做减法 const CvArr* src,//被减矩阵 CvScalar value,//减数值 CvArr* dst,//结果矩阵 const CvArr* mask = NULL//矩阵“开关” );
#include <cv.h> #include <highgui.h> #include <stdio.h> int main() { IplImage *src1, *src2; src1 = cvLoadImage("1.jpg"); src2 = cvLoadImage("7.jpg"); CvScalar cs; cs.val[0] = 0; cs.val[1] = 255; cs.val[2] = 0; cs.val[3] = 0; cvSubS(src1,cs,src2); cvShowImage( "测试1", src1); cvShowImage( "测试2", src2); cvWaitKey(); return 0; }
(6)cvSubRS函数
其结构
void cvSubRS(//给定值减去矩阵 const CvArr* src,//减矩阵 CvScalar value,//被减数值 CvArr* dst,//结果矩阵 const CvArr* mask = NULL//矩阵“开关” );
#include <cv.h> #include <highgui.h> #include <stdio.h> int main() { IplImage *src1, *src2; src1 = cvLoadImage("1.jpg"); src2 = cvLoadImage("7.jpg"); CvScalar cs; cs.val[0] = 0; cs.val[1] = 255; cs.val[2] = 0; cs.val[3] = 0; cvSubRS(src1,cs,src2); cvShowImage( "测试1", src1); cvShowImage( "测试2", src2); cvWaitKey(); return 0; }
to be continued