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

📄 gcoptimization.cpp

📁 GraphCut Minimization Library 转换成 VC++6.0 Class File
💻 CPP
📖 第 1 页 / 共 4 页
字号:

	
	return(eng);
	
}
/**************************************************************************************/

GCoptimization::EnergyType GCoptimization::giveSmoothEnergy_G_ARRAY()
{

	EnergyType eng = (EnergyType) 0;
	int x,y,pix;


	for ( y = 0; y < m_height; y++ )
		for ( x = 1; x < m_width; x++ )
		{
			pix = x+y*m_width;
			eng = eng + m_smoothcost(m_labeling[pix],m_labeling[pix-1]);
		}

	for ( y = 1; y < m_height; y++ )
		for ( x = 0; x < m_width; x++ )
		{
			pix = x+y*m_width;
			eng = eng + m_smoothcost(m_labeling[pix],m_labeling[pix-m_width]);
		}

	return(eng);
	
}
/**************************************************************************************/

GCoptimization::EnergyType GCoptimization::giveSmoothEnergy_G_FnPix()
{

	EnergyType eng = (EnergyType) 0;
	int x,y,pix;


	for ( y = 0; y < m_height; y++ )
		for ( x = 1; x < m_width; x++ )
		{
			pix = x+y*m_width;
			eng = eng + m_smoothFnPix(pix,pix-1,m_labeling[pix],m_labeling[pix-1]);
		}

	for ( y = 1; y < m_height; y++ )
		for ( x = 0; x < m_width; x++ )
		{
			pix = x+y*m_width;
			eng = eng + m_smoothFnPix(pix,pix-m_width,m_labeling[pix],m_labeling[pix-m_width]);
		}

	return(eng);
	
}
/**************************************************************************************/

GCoptimization::EnergyType GCoptimization::giveSmoothEnergy_G_FnCoord()
{

	EnergyType eng = (EnergyType) 0;
	int x,y,pix;


	for ( y = 0; y < m_height; y++ )
		for ( x = 1; x < m_width; x++ )
		{
			pix = x+y*m_width;
			eng = eng + m_horz_cost(x-1,y,m_labeling[pix],m_labeling[pix-1]);
		}

	for ( y = 1; y < m_height; y++ )
		for ( x = 0; x < m_width; x++ )
		{
			pix = x+y*m_width;
			eng = eng + m_vert_cost(x,y-1,m_labeling[pix],m_labeling[pix-m_width]);
		}

	return(eng);
	
}

/**************************************************************************************/
 
GCoptimization::EnergyType GCoptimization::compute_energy()
{
	return(giveDataEnergy()+giveSmoothEnergy());
}

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

GCoptimization::EnergyType GCoptimization::expansion(int max_num_iterations)
{
	return(start_expansion(max_num_iterations)); 
}

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


GCoptimization::EnergyType GCoptimization::expansion()
{
	return(start_expansion(MAX_INTT));
}

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


GCoptimization::EnergyType GCoptimization::start_expansion(int max_num_iterations )
{
	
	int curr_cycle = 1;
	EnergyType new_energy,old_energy;
	

	new_energy = compute_energy();

	old_energy = 2*new_energy;

	while ( old_energy > new_energy  && curr_cycle <= max_num_iterations)
	{
		old_energy = new_energy;
		new_energy = oneExpansionIteration();
		
		curr_cycle++;	
	}

	return(new_energy);
}

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

void GCoptimization::scramble_label_table()
{
   LabelType r1,r2,temp;
   int num_times,cnt;


   num_times = m_num_labels*2;

   for ( cnt = 0; cnt < num_times; cnt++ )
   {
      r1 = rand()%m_num_labels;  
      r2 = rand()%m_num_labels;  

      temp             = m_labelTable[r1];
      m_labelTable[r1] = m_labelTable[r2];
      m_labelTable[r2] = temp;
   }
}

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

GCoptimization::EnergyType GCoptimization::alpha_expansion(LabelType label)
{
	terminateOnError( label < 0 || label >= m_num_labels,"Illegal Label to Expand On");

	perform_alpha_expansion(label);
	return(compute_energy());
}

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

void GCoptimization::add_t_links_ARRAY(Energy *e,Energy::Var *variables,int size,LabelType alpha_label)
{
	for ( int i = 0; i < size; i++ )
		e -> add_term1(variables[i], m_datacost(m_lookupPixVar[i],alpha_label),
		                             m_datacost(m_lookupPixVar[i],m_labeling[m_lookupPixVar[i]]));

}

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

void GCoptimization::add_t_links_FnPix(Energy *e,Energy::Var *variables,int size,LabelType alpha_label)
{
	for ( int i = 0; i < size; i++ )
		e -> add_term1(variables[i], m_dataFnPix(m_lookupPixVar[i],alpha_label),
		                             m_dataFnPix(m_lookupPixVar[i],m_labeling[m_lookupPixVar[i]]));

}
/**************************************************************************************/

void GCoptimization::add_t_links_FnCoord(Energy *e,Energy::Var *variables,int size,LabelType alpha_label)
{
	int x,y;

	for ( int i = 0; i < size; i++ )
	{

		y = m_lookupPixVar[i]/m_width;
		x = m_lookupPixVar[i] - y*m_width;

		e -> add_term1(variables[i], m_dataFnCoord(x,y,alpha_label),
		                             m_dataFnCoord(x,y,m_labeling[m_lookupPixVar[i]]));
	}
}

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

void GCoptimization::perform_alpha_expansion(LabelType alpha_label)
{
	PixelType i,size = 0; 
	Energy *e = new Energy();
	

	
	for ( i = 0; i < m_num_pixels; i++ )
	{
		if ( m_labeling[i] != alpha_label )
		{
			m_lookupPixVar[size] = i;
			size++;
		}
	}

		
	if ( size > 0 ) 
	{
		Energy::Var *variables = (Energy::Var *) new Energy::Var[size];

		for ( i = 0; i < size; i++ )
			variables[i] = e ->add_variable();

		if ( m_dataType == ARRAY ) add_t_links_ARRAY(e,variables,size,alpha_label);
		else  if  ( m_dataType == FUNCTION_PIX ) add_t_links_FnPix(e,variables,size,alpha_label);
		else  add_t_links_FnCoord(e,variables,size,alpha_label);


		if ( m_grid_graph )
		{
			if ( m_smoothType == ARRAY )
			{
				if (m_varying_weights) set_up_expansion_energy_G_ARRAY_VW(size,alpha_label,e,variables);
				else set_up_expansion_energy_G_ARRAY(size,alpha_label,e,variables);
			}
			else if ( m_smoothType == FUNCTION_PIX ) set_up_expansion_energy_G_FnPix(size,alpha_label,e,variables);
			else  set_up_expansion_energy_G_FnCoord(size,alpha_label,e,variables);
			
		}
		else
		{
			if ( m_smoothType == ARRAY ) set_up_expansion_energy_NG_ARRAY(size,alpha_label,e,variables);
			else if ( m_smoothType == FUNCTION_PIX ) set_up_expansion_energy_NG_FnPix(size,alpha_label,e,variables);
		}
		
		e -> minimize();
	
	

		for ( i = 0,size = 0; i < m_num_pixels; 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 GCoptimization::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 GCoptimization::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 GCoptimization::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]);
		}	

⌨️ 快捷键说明

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