本文整理汇总了C++中Energy::SetPairCost方法的典型用法代码示例。如果您正苦于以下问题:C++ Energy::SetPairCost方法的具体用法?C++ Energy::SetPairCost怎么用?C++ Energy::SetPairCost使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Energy
的用法示例。
在下文中一共展示了Energy::SetPairCost方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: robustpn
void robustpn(int nout, mxArray* pout[], int nin, const mxArray* pin[])
{
/*
*********************************************************
* Check inputs and construct the energy
**********************************************************
*/
int nLabel, nVar, nPair, nHigher, ii;
int hop_fields_indices[HOP_N_OF_FIELDS]; // indices to fields in hop struct
// check pin[0] is sparse
if ( ! mxIsSparse(pin[0]) || ! mxIsDouble(pin[0]) || mxIsComplex(pin[0]))
mexErrMsgIdAndTxt("robustpn:inputs","sparseG must be a sparse double matrix");
// check pin[0] is square
const mwSize *spd = mxGetDimensions(pin[0]);
if (spd[0] != spd[1])
mexErrMsgIdAndTxt("robustpn:inputs","sparseG must be a square matrix");
nVar = spd[0];
nPair = 0;
// read the sparse matrix
double* Pr = mxGetPr(pin[0]);
mwIndex *ir = mxGetIr(pin[0]);
mwIndex *jc = mxGetJc(pin[0]);
mwIndex col, starting_row_index, stopping_row_index, current_row_index, tot(0);
mwSize max_npair = mxGetNzmax(pin[0]);
int * pairs = new int[2 * max_npair]; // will be de-alocate on ~Energy
termType* sc = new termType[max_npair]; // will be de-alocate on ~Energy
// mexPrintf("Preparing to read sG\n");
// traverse the sparseG matrix - pick only connections from the upper tri of the matrix
// (since its symmetric and we don't want to count each potential twice).
for (col=0; col<nVar; col++) {
starting_row_index = jc[col];
stopping_row_index = jc[col+1];
if (starting_row_index == stopping_row_index)
continue;
else {
for (current_row_index = starting_row_index;
current_row_index < stopping_row_index ;
current_row_index++) {
if ( ir[current_row_index] >= col ) { // ignore lower tri of matrix
pairs[nPair*2] = ir[current_row_index]; // from
pairs[nPair*2 + 1] = col; // to
sc[nPair] = (termType)Pr[tot]; // potential weight
nPair++;
}
tot++;
}
}
}
// mexPrintf("Done reading sG got %d pairs\n", nPair);
// check pin[1] has enough columns (=#nodes)
const mwSize *sdc = mxGetDimensions(pin[1]);
if (sdc[1] != spd[0])
mexErrMsgIdAndTxt("robustpn:inputs","Dc must have %d columns to match graph structure", spd[0]);
nLabel = sdc[0];
// check pin[2] is struct array with proper feilds
if ( mxGetClassID(pin[2]) != mxSTRUCT_CLASS )
mexErrMsgIdAndTxt("robustpn:inputs","hop must be a struct array");
nHigher = mxGetNumberOfElements(pin[2]);
// expecting HOP_N_OF_FIELDS fieds
if ( mxGetNumberOfFields(pin[2]) != HOP_N_OF_FIELDS )
mexErrMsgIdAndTxt("robustpn:inputs","hop must have %d fields", HOP_N_OF_FIELDS);
// chack that we have the right fields
for ( ii = 0; ii < HOP_N_OF_FIELDS ; ii++ ) {
hop_fields_indices[ii] = mxGetFieldNumber(pin[2], HOP_FIELDS[ii]);
if ( hop_fields_indices[ii] < 0 )
mexErrMsgIdAndTxt("robustpn:inputs","hop is missing %s field", HOP_FIELDS[ii]);
}
Energy<termType> *energy = new Energy<termType>(nLabel, nVar, nPair, nHigher);
energy->SetUnaryCost( (termType*)mxGetData(pin[1]) );
energy->SetPairCost(pairs, sc);
delete[] pairs; // were copied into energy
delete[] sc;
// Add the HO potentials
mxArray *xind, *xw, *xgamma, *xQ;
int * ind, n;
termType* w;
termType* gamma;
termType Q;
for ( ii = 0 ; ii < nHigher; ii++ ) {
xind = mxGetFieldByNumber(pin[2], ii, hop_fields_indices[0]);
n = mxGetNumberOfElements(xind);
ind = new int[n]; // allocation for energy
GetArr(xind, ind, -1); // bias = -1 convert from 1-ind of matlab to 0-ind of C
xw = mxGetFieldByNumber(pin[2], ii, hop_fields_indices[1]);
if ( mxGetNumberOfElements(xw) != n ) {
//.........这里部分代码省略.........