コード例 #1
0
    void ConjGradSolverImpl::minimizeOnTheLine(Ptr<MinProblemSolver::Function> _f,Mat_<double>& x,const Mat_<double>& d,Mat_<double>& buf1,
            Mat_<double>& buf2){
        double sigma=INITIAL_SEC_METHOD_SIGMA;
        buf1=0.0;
        buf2=0.0;

        dprintf(("before minimizeOnTheLine\n"));
        dprintf(("x:\n"));
        print_matrix(x);
        dprintf(("d:\n"));
        print_matrix(d);

        for(int i=0;i<SEC_METHOD_ITERATIONS;i++){
            _f->getGradient((double*)x.data,(double*)buf1.data);
            dprintf(("buf1:\n"));
            print_matrix(buf1);
            x=x+sigma*d;
            _f->getGradient((double*)x.data,(double*)buf2.data);
            dprintf(("buf2:\n"));
            print_matrix(buf2);
            double d1=buf1.dot(d), d2=buf2.dot(d);
            if((d1-d2)==0){
                break;
            }
            double alpha=-sigma*d1/(d2-d1);
            dprintf(("(buf2.dot(d)-buf1.dot(d))=%f\nalpha=%f\n",(buf2.dot(d)-buf1.dot(d)),alpha));
            x=x+(alpha-sigma)*d;
            sigma=-alpha;
        }

        dprintf(("after minimizeOnTheLine\n"));
        print_matrix(x);
    }
コード例 #2
0
    double ConjGradSolverImpl::minimize(InputOutputArray x){
        CV_Assert(_Function.empty()==false);
        dprintf(("termcrit:\n\ttype: %d\n\tmaxCount: %d\n\tEPS: %g\n",_termcrit.type,_termcrit.maxCount,_termcrit.epsilon));

        Mat x_mat=x.getMat();
        CV_Assert(MIN(x_mat.rows,x_mat.cols)==1);
        int ndim=MAX(x_mat.rows,x_mat.cols);
        CV_Assert(x_mat.type()==CV_64FC1);

        if(d.cols!=ndim){
            d.create(1,ndim);
            r.create(1,ndim);
            r_old.create(1,ndim);
            minimizeOnTheLine_buf1.create(1,ndim);
            minimizeOnTheLine_buf2.create(1,ndim);
        }

        Mat_<double> proxy_x;
        if(x_mat.rows>1){
            buf_x.create(1,ndim);
            Mat_<double> proxy(ndim,1,buf_x.ptr<double>());
            x_mat.copyTo(proxy);
            proxy_x=buf_x;
        }else{
            proxy_x=x_mat;
        }
        _Function->getGradient(proxy_x.ptr<double>(),d.ptr<double>());
        d*=-1.0;
        d.copyTo(r);

        //here everything goes. check that everything is setted properly
        dprintf(("proxy_x\n"));print_matrix(proxy_x);
        dprintf(("d first time\n"));print_matrix(d);
        dprintf(("r\n"));print_matrix(r);

        for(int count=0;count<_termcrit.maxCount;count++){
            minimizeOnTheLine(_Function,proxy_x,d,minimizeOnTheLine_buf1,minimizeOnTheLine_buf2);
            r.copyTo(r_old);
            _Function->getGradient(proxy_x.ptr<double>(),r.ptr<double>());
            r*=-1.0;
            double r_norm_sq=norm(r);
            if(_termcrit.type==(TermCriteria::MAX_ITER+TermCriteria::EPS) && r_norm_sq<_termcrit.epsilon){
                break;
            }
            r_norm_sq=r_norm_sq*r_norm_sq;
            double beta=MAX(0.0,(r_norm_sq-r.dot(r_old))/r_norm_sq);
            d=r+beta*d;
        }



        if(x_mat.rows>1){
            Mat(ndim, 1, CV_64F, proxy_x.ptr<double>()).copyTo(x);
        }
        return _Function->calc(proxy_x.ptr<double>());
    }
コード例 #3
0
ファイル: VO_AAMInverseIA.cpp プロジェクト: haifaben/vosm
/**
 * @author     	JIA Pei
 * @version    	2010-04-03
 * @brief      	Calculate modified steepest descent image for template face - project out appearance variation
 * @return		void
*/
void VO_AAMInverseIA::VO_CalcModifiedSDI()
{
    //project out appearance variation i.e. modify the steepest descent image
    this->m_MatSteepestDescentImages = Mat_<float>::zeros(this->m_iNbOfTextures, this->m_iNbOfShapeEigens+4);
    this->m_MatModifiedSteepestDescentImages = Mat_<float>::zeros(this->m_iNbOfTextures, this->m_iNbOfShapeEigens+4);

    for (unsigned int i = 0; i < this->m_iNbOfTextures; i++)
    {
        // AAM Revisited (63)
        for (unsigned int j = 0; j < 4; j++)
        {
            this->m_MatSteepestDescentImages(i, j) = this->m_MatSteepestDescentImages4GlobalShapeNormalization(i, j);
        }
        // AAM Revisited (64)
        for (unsigned int j = 0; j < this->m_iNbOfShapeEigens; j++)
        {
            this->m_MatSteepestDescentImages(i, 4+j) = this->m_MatSteepestDescentImages4ShapeModel(i, j);
        }
    }

    Mat_<float> oneCol 				= Mat_<float>::zeros(this->m_iNbOfTextures, 1);
    Mat_<float> spanedsum 			= Mat_<float>::zeros(this->m_iNbOfTextures, 1);
    Mat_<float> modifiedoneCol 		= Mat_<float>::zeros(this->m_iNbOfTextures, 1);
    Mat_<float> oneSpanRowTranspose = Mat_<float>::zeros(this->m_iNbOfTextures, 1);

    for (unsigned int i = 0; i < this->m_MatSteepestDescentImages.cols; i++)
    {
        spanedsum = Mat_<float>::zeros(this->m_iNbOfTextures, 1);
		oneCol = this->m_MatSteepestDescentImages.col(i);
        for (unsigned int j = 0; j < this->m_iNbOfTextureEigens; j++)
        {
            oneSpanRowTranspose = this->m_PCANormalizedTexture.eigenvectors.row(j).t();
            double weight = oneSpanRowTranspose.dot(oneCol);

            // dst(I)=src1(I)*alpha+src2(I)*beta+gamma
            cv::addWeighted( spanedsum, 1.0, oneSpanRowTranspose, weight, 0.0, spanedsum );
        }

        cv::subtract(oneCol, spanedsum, modifiedoneCol);
		Mat_<float> tmpCol = this->m_MatModifiedSteepestDescentImages.col(i);
		modifiedoneCol.copyTo(tmpCol);
    }
}