Professional Documents
Culture Documents
Pi19404
February 21, 2014
The mutivariate gaussian distribution is parameterized by mean vector and covariance matrix .
fx (x1 ; : : : ; xk ) = p
(2 )k
jj
exp
2 (x )
1
1(x ) ;
where is a Nx1 mean vector is NxN matrix covariance matrix j j is the determinant of
The mutivariate gaussian is also used as probability density function of the vector X A naive way of implementation is to directly perform the computation as in the equation to get the result. A Gaussian is defined Since ill be using a lot of opencv conversion routines are defined for Opencv cv::Mat data structure to Eigen::MatrixXf data structure. when loading the covarince matrix ,the determinant,inverse etc are also computed which would be later required for probability calculation
2|4
} void setSigma(cv::Mat &v) { Map<Eigen::Matrix<float,Dynamic,Dynamic,Eigen::RowMajor> > mappedMat((float *)v.data,v.rows,v.cols) ; _Sigma=mappedMat; _dim=v.rows; _det=_Sigma.determinant(); _scale=1.f/(pow(2*PI*_dim*_det,0.5)); _invsigma=_Sigma.inverse(); } MatrixXf setData(cv::Mat &v) { Map<Eigen::Matrix<float,Dynamic,Dynamic,Eigen::RowMajor> > mappedMat((float *)v.data,1,v.cols) ; MatrixXf ref=mappedMat; return ref; }
void validate(float &res) { if(res<0) res=0; if(res>1) res=1; if(isnan(res)) res=0; } float Prob(Mat &x) { MatrixXf tmp=setData(x); float res=0; MatrixXf tmp1=(tmp-_mu); MatrixXf tmp2=tmp1*_invsigma*tmp1.transpose(); res=scale*tmp2(0,0); validate(res); return res; }
3|4
The covariance matrix is postive semidefinate and symmteric The Cholesky decomposition of a symmetric, positive definite matrix decom- poses A into a product of a lower triangular matrix L and its transpose.
Thus we can perform the Cholskey factorization of the covariance matrix to find
float Prob(Mat &x) { MatrixXf tmp=setData(x); float res=0; MatrixXf tmp1=(tmp-_mu).transpose(); tmp1=_LI*tmp1; MatrixXf tmp2=tmp1.transpose()*tmp1; res=tmp2(0,0); validate(res); return res;
0.1.1 Code
The code can be found at git repository https://github.com/ pi19404/OpenVision in files ImgML/gaussian.hpp and ImgML.gaussian.cpp files.
4|4