⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gcoptimization.cpp

📁 markov random field in matlab code
💻 CPP
📖 第 1 页 / 共 4 页
字号:
            if ( m_smoothType != FUNCTION ) set_up_expansion_energy_NG_ARRAY(size,alpha_label,e,variables);
            else if ( m_smoothType == FUNCTION) set_up_expansion_energy_NG_FnPix(size,alpha_label,e,variables);
        }
        
        e -> minimize();
    
        for ( i = 0,size = 0; i < m_nPixels; i++ )
        {
            if ( m_labeling[i] != alpha_label )
            {
                if ( e->get_var(variables[size]) == 0 )
                    m_labeling[i] = alpha_label;

                size++;
            }
        }

        delete [] variables;
    }

    delete e;
}

/**********************************************************************************************/
/* Performs alpha-expansion for non regular grid graph for case when energy terms are NOT     */
/* specified by a function */

void Expansion::set_up_expansion_energy_NG_ARRAY(int size, LabelType alpha_label,Energy *e,Energy::Var *variables )
{
    EnergyTermType weight;
    Neighbor *tmp;
    int i,nPix,pix;;



    for ( i = size - 1; i >= 0; i-- )
    {
        pix = m_lookupPixVar[i];
        m_lookupPixVar[pix] = i;

        if ( !m_neighbors[pix].isEmpty() )
        {
            m_neighbors[pix].setCursorFront();
            
            while ( m_neighbors[pix].hasNext() )
            {
                tmp = (Neighbor *) (m_neighbors[pix].next());
                nPix   = tmp->to_node;
                weight = tmp->weight;
                
                if ( m_labeling[nPix] != alpha_label )
                {
                    if ( pix < nPix )
                        e ->add_term2(variables[i],variables[m_lookupPixVar[nPix]],
                                      m_smoothcost(alpha_label,alpha_label)*weight,
                                      m_smoothcost(alpha_label,m_labeling[nPix])*weight,
                                      m_smoothcost(m_labeling[pix],alpha_label)*weight,
                                      m_smoothcost(m_labeling[pix],m_labeling[nPix])*weight);
                }
                else
                    e ->add_term1(variables[i],m_smoothcost(alpha_label,alpha_label)*weight,
                                           m_smoothcost(m_labeling[pix],alpha_label)*weight);
                
            }
        }
    }

    
}

/**********************************************************************************************/
/* Performs alpha-expansion for non regular grid graph for case when energy terms are        */
/* specified by a function */

void Expansion::set_up_expansion_energy_NG_FnPix(int size, LabelType alpha_label,Energy *e,Energy::Var *variables )
{
    Neighbor *tmp;
    int i,nPix,pix;
    


    for ( i = size - 1; i >= 0; i-- )
    {
        pix = m_lookupPixVar[i];
        m_lookupPixVar[pix] = i;


        if ( !m_neighbors[pix].isEmpty() )
        {
            m_neighbors[pix].setCursorFront();
            
            while ( m_neighbors[pix].hasNext() )
            {
                tmp = (Neighbor *) (m_neighbors[pix].next());
                nPix   = tmp->to_node;
                
                if ( m_labeling[nPix] != alpha_label )
                {
                    if ( pix < nPix )
                        e ->add_term2(variables[i],variables[m_lookupPixVar[nPix]],
                                      m_smoothFnPix(pix,nPix,alpha_label,alpha_label),
                                      m_smoothFnPix(pix,nPix,alpha_label,m_labeling[nPix]),
                                      m_smoothFnPix(pix,nPix,m_labeling[pix],alpha_label),
                                      m_smoothFnPix(pix,nPix,m_labeling[pix],m_labeling[nPix]));
                }
                else
                    e ->add_term1(variables[i],m_smoothFnPix(pix,nPix,alpha_label,m_labeling[nPix]),
                                               m_smoothFnPix(pix,nPix,m_labeling[pix],alpha_label));
                
            }
        }
    }
}

/**********************************************************************************************/
/* Performs alpha-expansion for  regular grid graph for case when energy terms are NOT        */
/* specified by a function */
void Expansion::set_up_expansion_energy_G_ARRAY_VW(int size, LabelType alpha_label,Energy *e,
                                                      Energy::Var *variables )
{
    int i,nPix,pix,x,y;
    EnergyTermType weight;


    for ( i = size - 1; i >= 0; i-- )
    {
        pix = m_lookupPixVar[i];
        y = pix/m_width;
        x = pix - y*m_width;

        m_lookupPixVar[pix] = i;

        if ( x < m_width - 1 )
        {
            nPix = pix + 1;
            weight = m_horizWeights[pix];
            if ( m_labeling[nPix] != alpha_label )
                e ->add_term2(variables[i],variables[m_lookupPixVar[nPix]],
                              m_smoothcost(alpha_label,alpha_label)*weight,
                              m_smoothcost(alpha_label,m_labeling[nPix])*weight,
                              m_smoothcost(m_labeling[pix],alpha_label)*weight,
                              m_smoothcost(m_labeling[pix],m_labeling[nPix])*weight);
            else   e ->add_term1(variables[i],m_smoothcost(alpha_label,m_labeling[nPix])*weight,
                                 m_smoothcost(m_labeling[pix],alpha_label)*weight);
        }   

        if ( y < m_height - 1 )
        {
            nPix = pix + m_width;
            weight = m_vertWeights[pix];
            if ( m_labeling[nPix] != alpha_label )
                e ->add_term2(variables[i],variables[m_lookupPixVar[nPix]],
                              m_smoothcost(alpha_label,alpha_label)*weight ,
                              m_smoothcost(alpha_label,m_labeling[nPix])*weight,
                              m_smoothcost(m_labeling[pix],alpha_label)*weight ,
                              m_smoothcost(m_labeling[pix],m_labeling[nPix])*weight );
            else   e ->add_term1(variables[i],m_smoothcost(alpha_label,m_labeling[nPix])*weight,
                                 m_smoothcost(m_labeling[pix],alpha_label)*weight);
        }   
        if ( x > 0 )
        {
            nPix = pix - 1;
    
            if ( m_labeling[nPix] == alpha_label )
               e ->add_term1(variables[i],m_smoothcost(alpha_label,m_labeling[nPix])*m_horizWeights[nPix],
                                 m_smoothcost(m_labeling[pix],alpha_label)*m_horizWeights[nPix]);
        }   

        if ( y > 0 )
        {
            nPix = pix - m_width;
    
            if ( m_labeling[nPix] == alpha_label )
               e ->add_term1(variables[i],m_smoothcost(alpha_label,alpha_label)*m_vertWeights[nPix],
                                 m_smoothcost(m_labeling[pix],alpha_label)*m_vertWeights[nPix]);
        }   
            
    }
    
}

/**********************************************************************************************/
/* Performs alpha-expansion for  regular grid graph for case when energy terms are NOT        */
/* specified by a function */
void Expansion::set_up_expansion_energy_G_ARRAY(int size, LabelType alpha_label,Energy *e,
                                                     Energy::Var *variables )
{
    int i,nPix,pix,x,y;


    for ( i = size - 1; i >= 0; i-- )
    {
        pix = m_lookupPixVar[i];
        y = pix/m_width;
        x = pix - y*m_width;

        m_lookupPixVar[pix] = i;

        if ( x < m_width - 1 )
        {
            nPix = pix + 1;

            if ( m_labeling[nPix] != alpha_label )
                e ->add_term2(variables[i],variables[m_lookupPixVar[nPix]],
                              m_smoothcost(alpha_label,alpha_label),
                              m_smoothcost(alpha_label,m_labeling[nPix]),
                              m_smoothcost(m_labeling[pix],alpha_label),
                              m_smoothcost(m_labeling[pix],m_labeling[nPix]));
            else   e ->add_term1(variables[i],m_smoothcost(alpha_label,m_labeling[nPix]),
                                 m_smoothcost(m_labeling[pix],alpha_label));
        }   

        if ( y < m_height - 1 )
        {
            nPix = pix + m_width;

            if ( m_labeling[nPix] != alpha_label )
                e ->add_term2(variables[i],variables[m_lookupPixVar[nPix]],
                              m_smoothcost(alpha_label,alpha_label) ,
                              m_smoothcost(alpha_label,m_labeling[nPix]),
                              m_smoothcost(m_labeling[pix],alpha_label) ,
                              m_smoothcost(m_labeling[pix],m_labeling[nPix]) );
            else   e ->add_term1(variables[i],m_smoothcost(alpha_label,m_labeling[nPix]),
                                 m_smoothcost(m_labeling[pix],alpha_label));
        }   
        if ( x > 0 )
        {
            nPix = pix - 1;
    
            if ( m_labeling[nPix] == alpha_label )
               e ->add_term1(variables[i],m_smoothcost(alpha_label,m_labeling[nPix]),
                                 m_smoothcost(m_labeling[pix],alpha_label) );
        }   

        if ( y > 0 )
        {
            nPix = pix - m_width;
    
            if ( m_labeling[nPix] == alpha_label )
               e ->add_term1(variables[i],m_smoothcost(alpha_label,alpha_label),
                                 m_smoothcost(m_labeling[pix],alpha_label));
        }   
            
    }
    
}


/**********************************************************************************************/
/* Performs alpha-expansion for  regular grid graph for case when energy terms are NOT        */
/* specified by a function */
void Expansion::set_up_expansion_energy_G_FnPix(int size, LabelType alpha_label,Energy *e,
                                                     Energy::Var *variables )
{
    int i,nPix,pix,x,y;


    for ( i = size - 1; i >= 0; i-- )
    {
        pix = m_lookupPixVar[i];
        y = pix/m_width;
        x = pix - y*m_width;

        m_lookupPixVar[pix] = i;

        if ( x < m_width - 1 )
        {
            nPix = pix + 1;

            if ( m_labeling[nPix] != alpha_label )
                e ->add_term2(variables[i],variables[m_lookupPixVar[nPix]],
                              m_smoothFnPix(pix,nPix,alpha_label,alpha_label),
                              m_smoothFnPix(pix,nPix,alpha_label,m_labeling[nPix]),
                              m_smoothFnPix(pix,nPix,m_labeling[pix],alpha_label),
                              m_smoothFnPix(pix,nPix,m_labeling[pix],m_labeling[nPix]));
            else   e ->add_term1(variables[i],m_smoothFnPix(pix,nPix,alpha_label,m_labeling[nPix]),
                                 m_smoothFnPix(pix,nPix,m_labeling[pix],alpha_label));
        }   

        if ( y < m_height - 1 )
        {
            nPix = pix + m_width;

            if ( m_labeling[nPix] != alpha_label )
                e ->add_term2(variables[i],variables[m_lookupPixVar[nPix]],
                              m_smoothFnPix(pix,nPix,alpha_label,alpha_label) ,
                              m_smoothFnPix(pix,nPix,alpha_label,m_labeling[nPix]),
                              m_smoothFnPix(pix,nPix,m_labeling[pix],alpha_label) ,
                              m_smoothFnPix(pix,nPix,m_labeling[pix],m_labeling[nPix]) );
            else   e ->add_term1(variables[i],m_smoothFnPix(pix,nPix,alpha_label,m_labeling[nPix]),
                                 m_smoothFnPix(pix,nPix,m_labeling[pix],alpha_label));
        }   
        if ( x > 0 )
        {
            nPix = pix - 1;
    
            if ( m_labeling[nPix] == alpha_label )
               e ->add_term1(variables[i],m_smoothFnPix(pix,nPix,alpha_label,m_labeling[nPix]),
                                 m_smoothFnPix(pix,nPix,m_labeling[pix],alpha_label) );
        }   

        if ( y > 0 )
        {
            nPix = pix - m_width;
    
            if ( m_labeling[nPix] == alpha_label )
               e ->add_term1(variables[i],m_smoothFnPix(pix,nPix,alpha_label,alpha_label),
                                 m_smoothFnPix(pix,nPix,m_labeling[pix],alpha_label));
        }   
            
    }
    
}


/**************************************************************************************/

GCoptimization::EnergyType Expansion::oneExpansionIteration()
{
    int next;
   
    terminateOnError( m_dataType == NONE,"You have to set up the data cost before running optimization");
    terminateOnError( m_smoothType == NONE,"You have to set up the smoothness cost before running optimization");

    if (m_random_label_order) scramble_label_table();
    

    for (next = 0;  next < m_nLabels;  next++ )
        perform_alpha_expansion(m_labelTable[next]);
    
    
    return(dataEnergy()+smoothnessEnergy());
}


/**************************************************************************************/

void Expansion::optimizeAlg(int nIterations)
{
    expansion(nIterations);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -