//运行参数: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 )
{这儿的数组还不能用double型!
float arryA[]={1,2,1,
3,1,4,
5,3,0};
float arryb[]={14,20,15};
/*
double a[16];
CvMat Ma = cvMat(3, 4, CV_64FC1, a);
*/
CvMat* A = cvCreateMat(3,3,CV_32FC1);
CvMat* x = cvCreateMat(3,1,CV_32FC1);
CvMat* b = cvCreateMat(3,1,CV_32FC1);
/*
*A=cvMat(3,3,CV_32FC1, arryA);
*b=cvMat(3,1,CV_32FC1, arryb);
*/
/*
cvSetData(A,a,CV_AUTOSTEP);
cvSetData(B,b,CV_AUTOSTEP);
cvSolve(A, B, X, CV_LU); // solve (Ax=b) for x
*/
cvSetData(A,arryA,CV_AUTOSTEP);
cvSetData(b,arryb,CV_AUTOSTEP);
cvSolve(A,b,x,CV_LU); // solve (Ax=b) for x
cvDoubleMatPrint(x);
/*
//输出:不如matlab精度高
-0.093750
5.156250
3.781250
//matlab:
>> A=[1 2 1
3 1 4
5 2 0];
>> b=[14;20;15];
>> x=A\b
x =
1
5
3
*/
cvNamedWindow( "Image", 1 ); // 创建窗口
cvShowImage( "Image", pImg ); // 显示图像
cvWaitKey(0); // 等待按键
cvReleaseMat(&A);
cvReleaseMat(&b);
cvReleaseMat(&x);
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,i,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,i,j,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]
}
cvSetData(A,arryA,CV_AUTOSTEP)// cvSolve(A,b,x,CV_LU);
最新推荐文章于 2019-10-16 20:17:12 发布