pairwiseModel/energyMinimization/trwsMex/src/example.cpp
// Example: minimizing an energy function with Potts terms.
// See type*.h files for other types of terms.
minimizing an energy function with Potts terms.
See type*.h files for other types of terms.
minimizing an energy function with Potts terms . See type * . h files for other types of terms .
void testPotts()
{
	MRFEnergy<TypePotts>* mrf;
	MRFEnergy<TypePotts>::NodeId* nodes;
	MRFEnergy<TypePotts>::Options options;
	TypePotts::REAL energy, lowerBound;
	const int nodeNum = 2; 
	const int K = 3; 
	TypePotts::REAL D[K];
	int x, y;
	mrf = new MRFEnergy<TypePotts>(TypePotts::GlobalSize(K));
	nodes = new MRFEnergy<TypePotts>::NodeId[nodeNum];
	D[0] = 0; D[1] = 1; D[2] = 2;
	nodes[0] = mrf->AddNode(TypePotts::LocalSize(), TypePotts::NodeData(D));
	D[0] = 3; D[1] = 4; D[2] = 5;
	nodes[1] = mrf->AddNode(TypePotts::LocalSize(), TypePotts::NodeData(D));
	mrf->AddEdge(nodes[0], nodes[1], TypePotts::EdgeData(6));
	options.m_iterMax = 30; 
	mrf->Minimize_TRW_S(options, lowerBound, energy);
	x = mrf->GetSolution(nodes[0]);
	y = mrf->GetSolution(nodes[1]);
	printf("Solution: %d %d\n", x, y);
	mrf->ZeroMessages(); 
	options.m_iterMax = 30; 
	mrf->Minimize_BP(options, energy);
	x = mrf->GetSolution(nodes[0]);
	y = mrf->GetSolution(nodes[1]);
	printf("Solution: %d %d\n", x, y);
	delete nodes;
	delete mrf;
}
void testPotts ( ) { MRFEnergy < TypePotts > * mrf ; MRFEnergy < TypePotts > :: NodeId * nodes ; MRFEnergy < TypePotts > :: Options options ; TypePotts :: REAL energy , lowerBound ; const int nodeNum = 2 ; const int K = 3 ; TypePotts :: REAL D [ K ] ; int x , y ; mrf = new MRFEnergy < TypePotts > ( TypePotts :: GlobalSize ( K ) ) ; nodes = new MRFEnergy < TypePotts > :: NodeId [ nodeNum ] ; D [ 0 ] = 0 ; D [ 1 ] = 1 ; D [ 2 ] = 2 ; nodes [ 0 ] = mrf -> AddNode ( TypePotts :: LocalSize ( ) , TypePotts :: NodeData ( D ) ) ; D [ 0 ] = 3 ; D [ 1 ] = 4 ; D [ 2 ] = 5 ; nodes [ 1 ] = mrf -> AddNode ( TypePotts :: LocalSize ( ) , TypePotts :: NodeData ( D ) ) ; mrf -> AddEdge ( nodes [ 0 ] , nodes [ 1 ] , TypePotts :: EdgeData ( 6 ) ) ; options . m_iterMax = 30 ; mrf -> Minimize_TRW_S ( options , lowerBound , energy ) ; x = mrf -> GetSolution ( nodes [ 0 ] ) ; y = mrf -> GetSolution ( nodes [ 1 ] ) ; printf ( " \n " , x , y ) ; mrf -> ZeroMessages ( ) ; options . m_iterMax = 30 ; mrf -> Minimize_BP ( options , energy ) ; x = mrf -> GetSolution ( nodes [ 0 ] ) ; y = mrf -> GetSolution ( nodes [ 1 ] ) ; printf ( " \n " , x , y ) ; delete nodes ; delete mrf ; }
Example: minimizing an energy function with Potts terms.
Example : minimizing an energy function with Potts terms .
// number of nodes // number of labels // construct energy // Function below is optional - it may help if, for example, nodes are added in a random order // mrf->SetAutomaticOrdering(); /////////////////////// TRW-S algorithm ////////////////////// // maximum number of iterations // read solution //////////////////////// BP algorithm //////////////////////// // in general not necessary - it may be faster to start  // with messages computed in previous iterations. // NOTE: in most cases, immediately after creating the energy // all messages are zero. EXCEPTION: typeBinary and typeBinaryFast. // So calling ZeroMessages for these types will NOT transform // the energy to the original state.  // maximum number of iterations // read solution // done