3 #include "GCoptimization.h"
\r
4 #include "GraphCut.h"
\r
11 * Matlab wrapper for Weksler graph cut implementation
\r
14 * [gch] = GraphCutConstr(width, height, num_labels, DataCost, SmoothnessCost,[vCost,hCost])
\r
16 * Note that data types are crucials!
\r
19 * width, hieght - 2D grid dimensions. (do not cast to int)
\r
20 * num_labels - number of labels. (do not cast to int)
\r
21 * DataCost - of type float, array size [width*height*#labels], the data term for pixel
\r
22 * x,y recieving label l is stroed at [(x+y*width)*#labels + l]
\r
23 * SmoothnessCost - of type float, array size [#labels.^2] the cost between l1 and l2 is
\r
24 * stored at [l1+l2*#labels] = Vpq(lp,lq)
\r
25 * vCost, hCost - of type float, array size[width*height] each,
\r
26 * horizontal and vertical cues for smoothness term
\r
29 * gch - of type int32, graph cut handle - do NOT mess with it!
\r
32 int nlhs, /* number of expected outputs */
\r
33 mxArray *plhs[], /* mxArray output pointer array */
\r
34 int nrhs, /* number of inputs */
\r
35 const mxArray *prhs[] /* mxArray input pointer array */
\r
39 GCoptimization::PixelType width, height;
\r
41 Graph::captype *DataCost;
\r
42 Graph::captype *SmoothnessCost;
\r
43 Graph::captype *hCue = NULL;
\r
44 Graph::captype *vCue = NULL;
\r
45 GCoptimization::LabelType *Labels;
\r
46 int dims[2]; /* for labels allocation */
\r
48 GCoptimization *MyGraph = NULL;
\r
50 /* check number of inout arguments - must be 5 */
\r
51 if ((nrhs != 5)&&(nrhs!=7)) {
\r
52 mexErrMsgIdAndTxt("GraphCut:NarginError","Wrong number of input argumnets");
\r
55 /* check first input width: must be 1 int element */
\r
56 GetScalar(prhs[0], width);
\r
59 /* check second input height: must be 1 element */
\r
60 GetScalar(prhs[1], height);
\r
63 /* check third input #labels: must be single element */
\r
64 GetScalar(prhs[2], num_labels);
\r
66 /* check fourth input DataCost: must have #labels*height*width elements of type float */
\r
67 if ( mxGetNumberOfElements(prhs[3]) != num_labels*width*height ) {
\r
68 mexErrMsgIdAndTxt("GraphCut:DataCost",
\r
69 "DataCost argument does not contains the right number of elements");
\r
71 if (mxGetClassID(prhs[3]) != mxSINGLE_CLASS ) {
\r
72 mexErrMsgIdAndTxt("GraphCut:DataCost",
\r
73 "DataCost argument is not of type float");
\r
75 DataCost = (Graph::captype*)mxGetData(prhs[3]);
\r
77 /* check fifth input SmoothnessCost: must have #labels.^2 elements of type float */
\r
78 if ( mxGetNumberOfElements(prhs[4]) != num_labels*num_labels ) {
\r
79 mexErrMsgIdAndTxt("GraphCut:SmoothnessCost",
\r
80 "SmoothnessCost argument does not contains the right number of elements");
\r
82 if (mxGetClassID(prhs[4]) != mxSINGLE_CLASS ) {
\r
83 mexErrMsgIdAndTxt("GraphCut:SmoothnessCost",
\r
84 "SmoothnessCost argument is not of type float");
\r
86 SmoothnessCost = (Graph::captype*)mxGetData(prhs[4]);
\r
89 /* add hCue and vCue */
\r
90 if ( mxGetNumberOfElements(prhs[5]) != width*height ) {
\r
91 mexErrMsgIdAndTxt("GraphCut:SmoothnessCost",
\r
92 "vCue argument does not contains the right number of elements");
\r
94 if (mxGetClassID(prhs[5]) != mxSINGLE_CLASS ) {
\r
95 mexErrMsgIdAndTxt("GraphCut:SmoothnessCost",
\r
96 "vCue argument is not of type float");
\r
98 vCue = (Graph::captype*)mxGetData(prhs[5]);
\r
100 if ( mxGetNumberOfElements(prhs[6]) != width*height ) {
\r
101 mexErrMsgIdAndTxt("GraphCut:SmoothnessCost",
\r
102 "hCue argument does not contains the right number of elements");
\r
104 if (mxGetClassID(prhs[6]) != mxSINGLE_CLASS ) {
\r
105 mexErrMsgIdAndTxt("GraphCut:SmoothnessCost",
\r
106 "hCue argument is not of type float");
\r
108 hCue = (Graph::captype*)mxGetData(prhs[6]);
\r
111 /* prepare the output argument */
\r
113 mexErrMsgIdAndTxt("GraphCut:OutputArg","Wrong number of output arguments");
\r
116 MyGraph = new GCoptimization(width, height, num_labels, SET_ALL_AT_ONCE, SET_ALL_AT_ONCE);
\r
117 MyGraph->setData(DataCost);
\r
118 if ( vCue != NULL && vCue != NULL )
\r
119 MyGraph->setSmoothness(SmoothnessCost, hCue, vCue);
\r
121 MyGraph->setSmoothness(SmoothnessCost);
\r
124 /* create a container for the pointer */
\r
125 dims[0] = 1; dims[1] = 0;
\r
126 plhs[0] = mxCreateNumericMatrix(1, 1, MATLAB_POINTER_TYPE, mxREAL);
\r
129 gh = (GraphHandle*) mxGetData(plhs[0]);
\r
130 *gh = (GraphHandle)(MyGraph);
\r