📄 graphcutmex.cpp
字号:
#include "mex.h"
#include "GCoptimization.h"
#include "GraphCut.h"
#include <stdlib.h>
/* Declarations */
void SetLabels(GCoptimization *MyGraph, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]);
void GetLabels(GCoptimization *MyGraph, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]);
void ABswaps(GCoptimization *MyGraph, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]);
void Expand(GCoptimization *MyGraph, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]);
void Energy(GCoptimization *MyGraph, int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]);
GCoptimization* GetGCHandle(const mxArray *x); /* extract ahndle from mxArry */
/*
* Matlab wrapper for Weksler graph cut implementation
*
* GCoptimization cde by Olga Veksler.
* Wrapper code by Shai Bagon.
*
*
* Performing Graph Cut operations on a 2D grid.
*
* Usage:
* [gch ...] = GraphCutMex(gch, mode ...);
*
* Notes:
* 1. Data types are crucial!
* 2. The embedded implementation treat matrices as a row stack, while
* matlab treats them as column stack; thus there is a need to
* transpose the label matrices and the indices passed to expand
* algorithm.
*
* Inputs:
* - gch: a valid Graph Cut handle (use GraphCutConstr to create a handle).
* - mode: a char specifying mode of operation. See details below.
*
* Output:
* - gch: A handle to the constructed graph. Handle this handle with care
* and don't forget to close it in the end!
*
* Possible modes:
*
* - 's': Set labels
* [gch] = GraphCutMex(gch, 's', labels)
*
* Inputs:
* - labels: a width*height array of type int32, containing a
* label per pixel. note that labels values must be is range
* [0..num_labels-1].
*
* - 'g': Get current labeling
* [gch labels] = GraphCutMex(gch, 'g')
*
* Outputs:
* - labels: a width*height array of type int32, containing a
* label per pixel. note that labels values must be is range
* [0..num_labels-1].
*
* - 'n': Get current values of energy terms
* [gch se de] = GraphCutMex(gch, 'n')
*
* Outputs:
* - se: Smoothness energy term.
* - de: Data energy term.
*
* - 'e': Perform labels expansion
* [gch labels] = GraphCutMex(gch, 'e')
* [gch labels] = GraphCutMex(gch, 'e', iter)
* [gch labels] = GraphCutMex(gch, 'e', [], label)
* [gch labels] = GraphCutMex(gch, 'e', [], label, indices)
*
* When no inputs are provided, GraphCut performs expansion steps
* until it converges.
*
* Inputs:
* - iter: a double scalar, the maximum number of expand
* iterations to perform.
* - label: int32 scalar denoting the label for which to perfom
* expand step.
* - indices: int32 array of linear indices of pixels for which
* expand step is computed. indices _MUST_ be zero offset and not
* one offset like matlab usuall indexing system, i.e., to include
* the first pixel in the expand indices array must contain 0 and
* NOT 1!
*
* Outputs:
* - labels: a width*height array of type int32, containing a
* label per pixel. note that labels values must be is range
* [0..num_labels-1].
*
* - 'a': Perform alpha - beta swappings
* [gch labels] = GraphCutMex(gch, 'a')
* [gch labels] = GraphCutMex(gch, 'a', iter)
* [gch labels] = GraphCutMex(gch, 'a', label1, label2)
*
* When no inputs are provided, GraphCut performs alpha - beta swaps steps
* until it converges.
*
* Inputs:
* - iter: a double scalar, the maximum number of swap
* iterations to perform.
* - label1, label2: int32 scalars denoting two labels for swap
* step.
*
* Outputs:
* - labels: a width*height array of type int32, containing a
* label per pixel. note that labels values must be is range
* [0..num_labels-1].
*
* - 'c': Close the graph and release allocated resources.
* [gch] = GraphCutMex(gch,'c');
*
*
* See Also:
* GraphCutConstr
*
* This wrapper for Matlab was written by Shai Bagon (shai.bagon@weizmann.ac.il).
* Department of Computer Science and Applied Mathmatics
* Wiezmann Institute of Science
* http://www.wisdom.weizmann.ac.il/
*
* The core cpp application was written by Veksler Olga:
*
* [1] Efficient Approximate Energy Minimization via Graph Cuts
* Yuri Boykov, Olga Veksler, Ramin Zabih,
* IEEE transactions on PAMI, vol. 20, no. 12, p. 1222-1239, November 2001.
*
* [2] What Energy Functions can be Minimized via Graph Cuts?
* Vladimir Kolmogorov and Ramin Zabih.
* To appear in IEEE Transactions on Pattern Analysis and Machine Intelligence (PAMI).
* Earlier version appeared in European Conference on Computer Vision (ECCV), May 2002.
*
* [3] An Experimental Comparison of Min-Cut/Max-Flow Algorithms
* for Energy Minimization in Vision.
* Yuri Boykov and Vladimir Kolmogorov.
* In IEEE Transactions on Pattern Analysis and Machine Intelligence (PAMI),
* September 2004
*
* [4] Matlab Wrapper for Graph Cut.
* Shai Bagon.
* in www.wisdom.weizmann.ac.il/~bagon, December 2006.
*
* This software can be used only for research purposes, you should cite
* the aforementioned papers in any resulting publication.
* If you wish to use this software (or the algorithms described in the aforementioned paper)
* for commercial purposes, you should be aware that there is a US patent:
*
* R. Zabih, Y. Boykov, O. Veksler,
* "System and method for fast approximate energy minimization via graph cuts ",
* United Stated Patent 6,744,923, June 1, 2004
*
*
* The Software is provided "as is", without warranty of any kind.
*
*
*/
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 */
)
{
/* building graph is only call without gch */
GCoptimization *MyGraph = NULL;
char mode ='\0';
if ( nrhs < 2 ) {
mexErrMsgIdAndTxt("GraphCut","Too few input arguments");
}
/* get graph handle */
MyGraph = GetGCHandle(prhs[0]);
/* get mode */
GetScalar(prhs[1], mode);
switch (mode) {
case 'c': /* close */
delete MyGraph; /* ->~GCoptimization(); /* explicitly call the destructor */
MyGraph = NULL;
break;
case 's': /* set labels */
/* setting the labels: we have an extra parameter - int32 array of size w*l
* containing the new labels */
SetLabels(MyGraph, nlhs, plhs, nrhs, prhs);
break;
case 'g': /* get labels */
GetLabels(MyGraph, nlhs, plhs, nrhs, prhs);
break;
case 'a': /* a-b swaps */
ABswaps(MyGraph, nlhs, plhs, nrhs, prhs);
break;
case 'e': /* expand steps */
Expand(MyGraph, nlhs, plhs, nrhs, prhs);
break;
case 'n': /* get the current labeling energy */
Energy(MyGraph, nlhs, plhs, nrhs, prhs);
break;
default:
mexErrMsgIdAndTxt("GraphCut:mode","unrecognized mode");
}
/* update the gch output handle */
int dims[2] = {1,1};
GraphHandle *pgh;
plhs[0] = mxCreateNumericArray(1, dims, MATLAB_POINTER_TYPE, mxREAL);
pgh = (GraphHandle*) mxGetData(plhs[0]);
*pgh = (GraphHandle)(MyGraph);
}
/**************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -