前端之家收集整理的这篇文章主要介绍了
单一矩阵的运算,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
- //运行参数:girl.jpg
- #pragma comment(lib,"highgui.lib")
- #pragma comment(lib,"cxcore.lib")
- #pragma comment(lib,"cv.lib")
- #pragma comment(lib,"ml.lib")
- #pragma comment(lib,"cvaux.lib")
- #pragma comment(lib,"cvcam.lib")
-
- #include "cv.h"
- #include "highgui.h"
- #include <stdio.h>
-
- inline void cvDoubleMatPrint( const CvMat* mat );
- inline void cvDoubleMatSet( CvMat* mat,double v );
- inline void cvDoubleMatSet( CvMat* mat);
- inline void cvScalarPrint(const CvScalar& s);
-
- int main( int argc,char** argv )
- {
- IplImage* pImg; //声明IplImage指针
-
- if( argc == 2 && (pImg = cvLoadImage( argv[1],CV_LOAD_IMAGE_UNCHANGED)) != 0 )
- {
-
- // 单一矩阵的运算:
- CvMat *Ma,*Mb;
- Ma=cvCreateMat(4,4,CV_32FC1);
- Mb=cvCreateMat(4,CV_32FC1);
- //cvDoubleMatSet(Ma);
- cvSetIdentity(Ma);
-
- cvTranspose(Ma,Mb); // 转置:transpose(Ma) -> Mb (注意转置阵不能返回给Ma本身)
- cvDoubleMatPrint(Ma);
- printf("转置后=====================\n");
- cvDoubleMatPrint(Mb);
-
- printf("迹:=====================\n");
- CvScalar t = cvTrace(Ma); // 迹:trace(Ma) -> t.val[0]
- cvScalarPrint(t);
-
- double d = cvDet(Ma); // 行列式:det(Ma) -> d
- printf("行列式:%lf=====================\n",d);
-
- cvInvert(Ma,Mb); // 逆矩阵:inv(Ma) -> Mb
- printf("逆矩阵=====================\n");
- cvDoubleMatPrint(Mb);
-
- cvNamedWindow( "Image",1 ); // 创建窗口
- cvShowImage( "Image",pImg ); // 显示图像
- cvWaitKey(0); // 等待按键
-
- cvReleaseMat(&Ma);
- cvReleaseMat(&Mb);
-
- cvDestroyWindow( "Image" ); // 销毁窗口
- cvReleaseImage( &pImg ); // 释放图像
-
- return 0;
- }
-
- return -1;
- }
-
- inline void cvDoubleMatPrint( const CvMat* mat )
- {
- int i,j;
- for( i = 0; i < mat->rows; i++ )
- {
- for( j = 0; j < mat->cols; j++ )
- {
- printf( "%lf ",cvmGet( mat,i,j ) );
- }
- printf( "\n" );
- }
- }
-
- inline void cvDoubleMatSet( CvMat* mat,double v )
- {
- int i,j;
- for( i = 0; i < mat->rows; i++ )
- {
- for( j = 0; j < mat->cols; j++ )
- {
- cvmSet( mat,j,v);
- }
-
- }
- }
-
- inline void cvDoubleMatSet( CvMat* mat)
- {
- int i,j;
- double v;
- for( i = 0; i < mat->rows; i++ )
- {
- for( j = 0; j < mat->cols; j++ )
- {
- v=i*10+j;
- cvmSet( mat,v);
- }
-
- }
- }
-
- inline void cvScalarPrint(const CvScalar& s)
- {
- printf("\n%f %f %f %f\n",s.val[0],s.val[1],s.val[2],s.val[3]);
- //CvScalar t = cvTrace(Ma); // 迹:trace(Ma) -> t.val[0]
- }