GCoptimization * GraphCutConstr(float* DataCostIn, float* SmoothnessCostIn, float* hCueIn, float* vCueIn, int RIn, int CIn, int num_labels) { GCoptimization::PixelType width, height; //int num_labels; Graph::captype *DataCost; Graph::captype *SmoothnessCost; Graph::captype *hCue = NULL; Graph::captype *vCue = NULL; GCoptimization::LabelType *Labels; GCoptimization *MyGraph = NULL; width = CIn; height = RIn; DataCost = (Graph::captype*)DataCostIn; SmoothnessCost = (Graph::captype*)SmoothnessCostIn; vCue = (Graph::captype*)vCueIn; hCue = (Graph::captype*)hCueIn; MyGraph = new GCoptimization(width, height, num_labels, SET_ALL_AT_ONCE, SET_ALL_AT_ONCE); MyGraph->setData(DataCost); if ( vCue != NULL && vCue != NULL ) MyGraph->setSmoothness(SmoothnessCost, hCue, vCue); else MyGraph->setSmoothness(SmoothnessCost); return MyGraph; }
void mexFunction( int nlhs, /* number of expected outputs */ mxArray *plhs[], /* mxArray output pointer array */ int nrhs, /* number of inputs */ const mxArray *prhs[] /* mxArray input pointer array */ ) { /* check number of arguments */ if (nrhs != 3) { mexErrMsgIdAndTxt("GraphCut:NarginError","Wrong number of input argumnets"); } if (nlhs != 1) { mexErrMsgIdAndTxt("GraphCut:NargoutError","Wrong number of output argumnets"); } /* check inputs */ if (mxGetClassID(prhs[0]) != mxSINGLE_CLASS ) { mexErrMsgIdAndTxt("GraphCut:DataCost", "DataCost argument is not of type float"); } if (mxGetClassID(prhs[1]) != mxSINGLE_CLASS ) { mexErrMsgIdAndTxt("GraphCut:SmoothnessCost", "SmoothnessCost argument is not of type float"); } if (! mxIsSparse(prhs[2]) ) { mexErrMsgIdAndTxt("GraphCut:SmoothnessCost", "Graph Structure Matrix is not sparse"); } GCoptimization::PixelType num_pixels; int num_labels; num_pixels = mxGetN(prhs[2]); if (mxGetM(prhs[2]) != num_pixels) { mexErrMsgIdAndTxt("GraphCut:SmoothnessCost", "Graph Structure Matrix is no square"); } num_labels = mxGetNumberOfElements(prhs[0])/num_pixels; if (mxGetNumberOfElements(prhs[1]) != num_labels*num_labels) { mexErrMsgIdAndTxt("GraphCut:SmoothnessCost", "Size does not match number of labels"); } /* construct the graph */ GCoptimization* MyGraph = new GCoptimization(num_pixels, num_labels, SET_ALL_AT_ONCE, SET_ALL_AT_ONCE); /* set the nieghbors and weights according to sparse matrix */ double *pr; mwIndex *ir, *jc; mwSize col, total=0; mwIndex starting_row_index, stopping_row_index, current_row_index; /* Get the starting positions of all four data arrays. */ pr = mxGetPr(prhs[2]); ir = mxGetIr(prhs[2]); jc = mxGetJc(prhs[2]); for (col=0; col<num_pixels; 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++) { /* use only upper triangle of matrix */ if ( ir[current_row_index] >= col ) { MyGraph->setNeighbors(ir[current_row_index], col, pr[total++]); } else { total++; } } } } Graph::captype *DataCost = (Graph::captype*)mxGetData(prhs[0]); Graph::captype *SmoothnessCost = (Graph::captype*)mxGetData(prhs[1]); /* set data term */ MyGraph->setData(DataCost); /* set the smoothness term */ MyGraph->setSmoothness(SmoothnessCost); /* create a container for the pointer */ const mwSize dims[2] = {1,0}; plhs[0] = mxCreateNumericArray(1, /*(int*)*/dims, MATLAB_POINTER_TYPE, mxREAL); GraphHandle* gh; gh = (GraphHandle*) mxGetData(plhs[0]); *gh = (GraphHandle)(MyGraph); }
GCoptimization * GraphCut3dConstr(float* ContrastIn, float* DataCostIn, float* SmoothnessCostIn, int RIn, int CIn, int ZIn, int num_labels) { Graph::captype *DataCost; Graph::captype *SmoothnessCost; Graph::captype *Contrast = NULL; //GCoptimization::LabelType *Labels; GCoptimization::PixelType R, C, Z; GCoptimization *MyGraph = NULL; R = RIn; C = CIn; Z = ZIn; DataCost = (Graph::captype*)DataCostIn; SmoothnessCost = (Graph::captype*)SmoothnessCostIn; Contrast = (Graph::captype*)ContrastIn; //int uu1 = sizeof(ContrastIn[1]); //int uu2 = sizeof(Contrast[1]); //By Yousef: Estimate the number of needed edges long num_ed = 0; for ( int r = 0 ; r <= R - 2; r++ ) { for ( int c = 0 ; c <= C - 2; c++ ) { for ( int z = 0 ; z <= Z - 2; z++ ) { num_ed+=3; } num_ed+=2; } for ( int z = 0 ; z <= Z -2 ; z++ ) { num_ed+=2; } num_ed+=1; } for ( int c = 0 ; c <= C - 2; c++ ) { for ( int z = 0 ; z <= Z - 2; z++ ) { num_ed+=2; } num_ed+=1; } for ( int z = 0 ; z <= Z - 2; z++ ) { num_ed+=1; } MyGraph = new GCoptimization(R*C*Z, num_labels, SET_ALL_AT_ONCE, SET_ALL_AT_ONCE, num_ed); /* neighborhod setup */ GCoptimization::PixelType c(0), r(0), z(0), p(0), q(0); if (Contrast) { double sig = 30;//added by Yousef on 10-27-2008 for ( r = 0 ; r <= R - 2; r++ ) { for ( c = 0 ; c <= C - 2; c++ ) { for ( z = 0 ; z <= Z - 2; z++ ) { /* all nodes with 3 nieghbors */ p = r+c*R+z*R*C;/*c+r*C+z*R*C;*/ q = r+1+c*R+z*R*C;/*c+(r+1)*C+z*R*C;*/ MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])/sig)); q = r+(c+1)*R+z*R*C;/*c+1+r*C+z*R*C;*/ MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])/sig)); q = r+c*R+(z+1)*R*C;/*c+r*C+(z+1)*R*C;*/ MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])/sig)); } /* top of Z has 2 n in c and r */ p = r+c*R+z*R*C;/*c+r*C+z*R*C;*/ q = r+1+c*R+z*R*C;/*c+(r+1)*C+z*R*C;*/ MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])/sig)); q = r+(c+1)*R+z*R*C;/*c+1+r*C+z*R*C;*/ MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])/sig)); } /* end of c has 2 n in z and r */ for ( z = 0 ; z <= Z -2 ; z++ ) { p = r+c*R+z*R*C;/*c+r*C+z*R*C;*/ q = r+1+c*R+z*R*C;/*c+(r+1)*C+z*R*C;*/ MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])/sig)); q = r+c*R+(z+1)*R*C;/*c+r*C+(z+1)*R*C;*/ MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])/sig)); } /* end of c abd z has n in r */ p = r+c*R+z*R*C;/*c+r*C+z*R*C;*/ q = r+1+c*R+z*R*C;/*c+(r+1)*C+z*R*C;*/ MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q])/sig)); } /* end of r has n in z and c */ for ( c = 0 ; c <= C - 2; c++ ) { for ( z = 0 ; z <= Z - 2; z++ ) { p = r+c*R+z*R*C;/*c+r*C+z*R*C;*/ q = r+c*R+(z+1)*R*C;/*c+r*C+(z+1)*R*C;*/ MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q]))); q = r+(c+1)*R+z*R*C;/*c+1+r*C+z*R*C;*/ MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q]))); } p = r+c*R+z*R*C;/*c+r*C+z*R*C;*/ q = r+(c+1)*R+z*R*C;/*c+1+r*C+z*R*C;*/ MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q]))); } for ( z = 0 ; z <= Z - 2; z++ ) { p = r+c*R+z*R*C;/*c+r*C+z*R*C;*/ q = r+c*R+(z+1)*R*C;/*c+r*C+(z+1)*R*C;*/ MyGraph->setNeighbors(p, q, exp(-fabs(Contrast[p]-Contrast[q]))); } /* end of graph construction with contrast */ } else { for ( r = 0 ; r <= R - 2; r++ ) { for ( c = 0 ; c <= C - 2; c++ ) { for ( z = 0 ; z <= Z - 2; z++ ) { /* all nodes with 3 nieghbors */ MyGraph->setNeighbors(r+c*R+z*R*C,r+1+c*R+z*R*C); MyGraph->setNeighbors(r+c*R+z*R*C,r+(c+1)*R+z*R*C); MyGraph->setNeighbors(r+c*R+z*R*C,r+c*R+(z+1)*R*C); } /* top of Z has 2 n in c and r */ MyGraph->setNeighbors(r+c*R+z*R*C,r+1+c*R+z*R*C); MyGraph->setNeighbors(r+c*R+z*R*C,r+(c+1)*R+z*R*C); } /* end of c has 2 n in z and r */ for ( z = 0 ; z <= Z -2 ; z++ ) { MyGraph->setNeighbors(r+c*R+z*R*C,r+1+c*R+z*R*C); MyGraph->setNeighbors(r+c*R+z*R*C,r+c*R+(z+1)*R*C); } /* end of c abd z has n in r */ MyGraph->setNeighbors(r+c*R+z*R*C,r+1+c*R+z*R*C); } /* end of r has n in z and c */ for ( c = 0 ; c <= C - 2; c++ ) { for ( z = 0 ; z <= Z - 2; z++ ) { MyGraph->setNeighbors(r+c*R+z*R*C,r+c*R+(z+1)*R*C); MyGraph->setNeighbors(r+c*R+z*R*C,r+(c+1)*R+z*R*C); } MyGraph->setNeighbors(r+c*R+z*R*C,r+(c+1)*R+z*R*C); } for ( z = 0 ; z <= Z - 2; z++ ) { MyGraph->setNeighbors(r+c*R+z*R*C,r+c*R+(z+1)*R*C); } } MyGraph->setData(DataCost); MyGraph->setSmoothness(SmoothnessCost); return MyGraph; }