cvSVD(&A, &U, &S, &V, CV_SVD_U_T);

来源:互联网 发布:我打打单软件 编辑:程序博客网 时间:2024/05/22 03:20
#include <iostream>#include <algorithm>#include <string>#include <io.h>#include "highgui.h"#include "cv.h"#pragma comment(lib, "cxcore.lib")using namespace std; inline void cvDoubleMatPrint( const CvMat* mat );void SVD(double *SA, double *SU, double *SS, double *SV);int main(){   double A[16]={1,1,1,1,0,1,0,0,0,0,1,1,0,0,0,0}, U[16], S[16], V[16];  SVD(A, U, S, V);printf("V:============\n");for(int j=0,i=0;i<16;i++){cout<<V[i]<<',';j++;if(j%4==0) cout<<endl;}return 0;} void SVD(double *SA, double *SU, double *SS, double *SV) { CvMat A, U, S, V; cvInitMatHeader(&A,4,4,CV_64FC1,SA,CV_AUTOSTEP); cvInitMatHeader(&U,4,4,CV_64FC1,SU,CV_AUTOSTEP); cvInitMatHeader(&S,4,4,CV_64FC1,SS,CV_AUTOSTEP); cvInitMatHeader(&V,4,4,CV_64FC1,SV,CV_AUTOSTEP); cvSVD(&A, &U, &S, &V, CV_SVD_U_T); printf("A:============\n"); cvDoubleMatPrint( &A); printf("U:============\n"); cvDoubleMatPrint( &U);  printf("S:============\n"); cvDoubleMatPrint( &S);  printf("V:============\n"); cvDoubleMatPrint( &V); }/*  个别小知识介绍:对A进行SVD分解后,矩阵V的最后一列其实是齐次方程Ax=0的一组解//输出:A:============1.000000 1.000000 1.000000 1.0000000.000000 1.000000 0.000000 0.0000000.000000 0.000000 1.000000 1.0000000.000000 0.000000 0.000000 0.000000U:============2.324366 0.000000 0.000000 0.0000000.000000 1.147184 0.000000 0.0000000.000000 0.000000 0.530368 0.0000000.000000 0.000000 0.000000 0.000000S:============-0.846041 -0.192165 -0.497279 0.000000-0.226091 -0.715409 0.661115 0.000000-0.482801 0.671761 0.561818 0.0000000.000000 0.000000 0.000000 1.000000V:============-0.363988 -0.197084 -0.910314 0.000000-0.446662 -0.820705 0.356281 0.000000-0.577930 0.379210 0.148985 0.707107-0.577930 0.379210 0.148985 -0.707107V:============-0.363988,-0.197084,-0.910314,0,-0.446662,-0.820705,0.356281,0,-0.57793,0.37921,0.148985,0.707107,-0.57793,0.37921,0.148985,-0.707107,Press any key to continue//matlab:A =     1     1     1     1     0     1     0     0     0     0     1     1     0     0     0     0>> [S V D]=svd(A)S =    0.8460    0.2261    0.4828         0    0.1922    0.7154   -0.6718         0    0.4973   -0.6611   -0.5618         0         0         0         0    1.0000V =    2.3244         0         0         0         0    1.1472         0         0         0         0    0.5304         0         0         0         0         0D =    0.3640    0.1971    0.9103         0    0.4467    0.8207   -0.3563         0    0.5779   -0.3792   -0.1490   -0.7071    0.5779   -0.3792   -0.1490    0.7071*/ 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" );    }}

原创粉丝点击