Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ Note that from a probabilistic viewpoint, GCO works in log-space.
Note that all input arrays are assumed to be in int32.
This means that float potentials must be rounded!

These algorithms can only deal with certain energies. Unfortunately
I have not figured out yet how to convert C++ errors to Python. If an unknown
These algorithms can only deal with certain energies. If an
error is raised, it probably means that you used an invalid energy function.
Look at the gco README for details.

Expand Down
29 changes: 29 additions & 0 deletions gco_exception_handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "Python.h"
#include "GCoptimization.h"
#include <exception>
#include <string>

using namespace std;

extern PyObject *gcerror; // a python exception object that represents the graph cut exception in python

/**
\brief this function translates graph cut exceptions into python exceptions.

It uses the mechanism described in http://stackoverflow.com/questions/10684983/handling-custom-c-exceptions-in-cython
*/
void handle_gco_exception()
{
try
{
throw;
}
catch (GCException& e)
{
PyErr_SetString(gcerror, e.message);
}
catch (const std::exception& e)
{
PyErr_SetString(PyExc_RuntimeError, e.what() );
}
}
1 change: 1 addition & 0 deletions gco_exception_handler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void handle_gco_exception();
45 changes: 30 additions & 15 deletions gco_python.pyx
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
import numpy as np
cimport numpy as np


np.import_array()

# we use the mechanism described in http://stackoverflow.com/questions/10684983/handling-custom-c-exceptions-in-cython to translate graph-cut c++ exceptions into python exceptions that will allow the user to see the message detailing the graph cut exception.
from python_object cimport PyObject
class GCError(RuntimeError):
"""
This class represents a graph cut error (exception)
"""
pass

# make the GCError class accessible to c++, so that the c++ function handle_gco_exception can use it
cdef public PyObject* gcerror = <PyObject*>GCError

cdef extern from "gco_exception_handler.hpp":
cdef void handle_gco_exception()

cdef extern from "GCoptimization.h":
cdef cppclass GCoptimizationGridGraph:
GCoptimizationGridGraph(int width, int height, int n_labels) except +
void setDataCost(int *) except +
void setSmoothCost(int *) except +
void expansion(int n_iterations) except +
void swap(int n_iterations) except +
void setSmoothCostVH(int* pairwise, int* V, int* H) except +
int whatLabel(int node) except +
GCoptimizationGridGraph(int width, int height, int n_labels) except +handle_gco_exception
void setDataCost(int *) except +handle_gco_exception
void setSmoothCost(int *) except +handle_gco_exception
void expansion(int n_iterations) except +handle_gco_exception
void swap(int n_iterations) except +handle_gco_exception
void setSmoothCostVH(int* pairwise, int* V, int* H) except +handle_gco_exception
int whatLabel(int node) except +handle_gco_exception

cdef cppclass GCoptimizationGeneralGraph:
GCoptimizationGeneralGraph(int n_vertices, int n_labels) except +
void setDataCost(int *) except +
void setSmoothCost(int *) except +
void setNeighbors(int, int) except +
void setNeighbors(int, int, int) except +
void expansion(int n_iterations) except +
void swap(int n_iterations) except +
int whatLabel(int node) except +
GCoptimizationGeneralGraph(int n_vertices, int n_labels) except +handle_gco_exception
void setDataCost(int *) except +handle_gco_exception
void setSmoothCost(int *) except +handle_gco_exception
void setNeighbors(int, int) except +handle_gco_exception
void setNeighbors(int, int, int) except +handle_gco_exception
void expansion(int n_iterations) except +handle_gco_exception
void swap(int n_iterations) except +handle_gco_exception
int whatLabel(int node) except +handle_gco_exception


def cut_simple(np.ndarray[np.int32_t, ndim=3, mode='c'] unary_cost,
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'maxflow.cpp']

files = [os.path.join(gco_directory, f) for f in files]
files.insert(0, "gco_exception_handler.cpp")
files.insert(0, "gco_python.pyx")

setup(cmdclass={'build_ext': build_ext},
Expand Down