矩阵和图像的操作
(1)cvSum函数
其结构
CvScalar cvSum(//计算arr各通道全部像素总和 CvArr* arr//目标矩阵 );
实例代码
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { IplImage *src1,*dst1,*dst2,*dst3,*dst4; src1=cvLoadImage("3.jpg",1); CvScalar cs = cvSum(src1); cout << "通道一总和:"; cout << cs.val[0] << endl; cout << "通道二总和:"; cout << cs.val[1] << endl; cout << "通道三总和:"; cout << cs.val[2] << endl; cout << "通道四总和:"; cout << cs.val[3] << endl; getchar(); return 0; }
输出结果
(2)cvSVD函数
其结构
void cvSVD(//计算 A = U*W*(V的转置) CvArr* A, CvArr* W, CvArr* U = NULL, CvArr* V = NULL, int flags = 0//标记位 );
ps : flag參数的取值
參数 | 结果 |
CV_SVD_MODIFY_A | 同意改变矩阵A |
CV_SVD_U_T | 返回U转置而不是U |
CV_SVD_V_T | 返回V转置而不是V |
实例代码
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { double a[5][5] = { {7,2,4,5,6}, {3,4,5,8,2}, {1,0,9,7,5}, {0,5,3,2,1}, {4,6,7,3,1} }; 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 ",cvmGet(&va,i,j)); cout << endl; } double b[5][5] = {0}; CvMat vb = cvMat(5,5, CV_64FC1,b); double c[5][5] = {0}; CvMat vc = cvMat(5,5, CV_64FC1,c); double d[5][5] = {0}; CvMat vd = cvMat(5,5, CV_64FC1,d); cvSVD(&va,&vb,&vc,&vd); cout<<"结果矩阵vb:"<<endl; for(int i=0;i<5;i++) { for(int j=0;j<5;j++) printf("%f ",cvmGet(&vb,i,j)); cout << endl; } cout<<"结果矩阵vc:"<<endl; for(int i=0;i<5;i++) { for(int j=0;j<5;j++) printf("%f ",cvmGet(&vc,i,j)); cout << endl; } cout<<"结果矩阵vd:"<<endl; for(int i=0;i<5;i++) { for(int j=0;j<5;j++) printf("%f ",cvmGet(&vd,i,j)); cout << endl; } getchar(); return 0; }
输出结果
(3)cvSVBkSb函数
其结构
void cvSVBKSb(//计算X=V*(W伴随)*(T转置)*B const CvArr* W, const CvArr* U, const CvArr* V, const CvArr* B, CvArr* X, int flage = 0//标记位 );
实例代码
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { double a[5][5] = { {7,2,4,5,6}, {3,4,5,8,2}, {1,0,9,7,5}, {0,5,3,2,1}, {4,6,7,3,1} }; CvMat va = cvMat(5,5, CV_64FC1,a); double b[5][5] = {0}; CvMat vb = cvMat(5,5, CV_64FC1,b); double c[5][5] = {0}; CvMat vc = cvMat(5,5, CV_64FC1,c); double d[5][5] = {0}; CvMat vd = cvMat(5,5, CV_64FC1,d); cvSVD(&va,&vb,&vc,&vd); cvSVBkSb(&vb,&vc,&vd,NULL,&va,0); cout<<"结果矩阵:"<<endl; for(int i=0;i<5;i++) { for(int j=0;j<5;j++) printf("%f ",cvmGet(&va,i,j)); cout << endl; } getchar(); return 0; }
输出结果
(4)cvTrace函数
其结构
CvScalar cvTrace(//求矩阵的迹 const CvArr* mat//目标矩阵 );
实例代码
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { double a[5][5] = { {7,2,4,5,6}, {3,4,5,8,2}, {1,0,9,7,5}, {0,5,3,2,1}, {4,6,7,3,1} }; 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 ",cvmGet(&va,i,j)); cout << endl; } CvScalar cs = cvTrace(&va); cout << " 矩阵的迹为:"; cout << cs.val[0] << endl; getchar(); return 0; }
输出结果
(5)cvTranspose函数
其结构
void cvTranspose(//矩阵转置 const CvArr* src,//目标矩阵 CvArr* dst//结果矩阵 );
实例代码
#include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { double a[5][5] = { {7,2,4,5,6}, {3,4,5,8,2}, {1,0,9,7,5}, {0,5,3,2,1}, {4,6,7,3,1} }; CvMat va = cvMat(5,5, CV_64FC1,a); double b[5][5] = {0}; CvMat vb = cvMat(5,5, CV_64FC1,b); cout<<"目标矩阵:"<<endl; for(int i=0;i<5;i++) { for(int j=0;j<5;j++) printf("%f ",cvmGet(&va,i,j)); cout << endl; } cvTranspose(&va,&vb); cout<<"结果矩阵:"<<endl; for(int i=0;i<5;i++) { for(int j=0;j<5;j++) printf("%f ",cvmGet(&vb,i,j)); cout << endl; } getchar(); return 0; }
输出结果
(6)cvXor函数
其结构
void cvXor(//矩阵进行异或操作 const CvArr* src1,//矩阵1 const CvArr* src2,//矩阵2 CvArr* dst,//结果矩阵 const CvArr* mask = NULL//矩阵“开关” );
实例代码
#include <cv.h> #include <highgui.h> #include <stdio.h> int main(int argc, char** argv) { IplImage *src1, *src2,*src3; src1 = cvLoadImage("1.jpg"); src2 = cvLoadImage("11.jpg"); src3 = cvLoadImage("7.jpg"); cvXor(src1,src2,src3); cvShowImage( "測试1", src1); cvShowImage( "測试2", src2); cvShowImage( "測试3", src3); cvWaitKey(); return 0; }
输出结果
(7)cvXorS函数
其结构
void cvXor(//矩阵与给定值进行异或操作 const CvArr* src1,//矩阵1 CvScalar value//给定值 CvArr* dst,//结果矩阵 const CvArr* mask = NULL//矩阵“开关” );
实例代码
#include <cv.h> #include <highgui.h> #include <stdio.h> int main(int argc, char** argv) { IplImage *src1, *src2; src1 = cvLoadImage("1.jpg"); src2 = cvLoadImage("7.jpg"); CvScalar cs; cs.val[0] = 100; cs.val[1] = 100; cs.val[2] = 255; cs.val[3] = 0; cvXorS(src1,cs,src2); cvShowImage( "測试1", src1); cvShowImage( "測试2", src2); cvWaitKey(); return 0; }
输出结果
(8)cvZero函数
其结构
void cvZero(//将图像全部通道设置为零 CvArr* arr//目标图像 );
实例代码
#include <cv.h> #include <highgui.h> #include <stdio.h> int main(int argc, char** argv) { IplImage *src1, *src2; src1 = cvLoadImage("1.jpg"); src2 = cvLoadImage("1.jpg"); cvZero(src1); cvShowImage( "结果图", src1); cvShowImage( "原图", src2); cvWaitKey(); return 0; }
输出结果
to be continued