Commit 26d44587 authored by David GEISSBUHLER's avatar David GEISSBUHLER Committed by David GEISSBUHLER

Added original C++ code by Robby Tan for specular highlights removal with...

Added original C++ code by Robby Tan for specular highlights removal with corresponding python bindings.'
parent a55a13ca
// include directly and indirectly dependent libraries
#ifdef NO_IMPORT_ARRAY
#undef NO_IMPORT_ARRAY
#endif
#include <bob.blitz/cppapi.h>
#include <bob.blitz/cleanup.h>
#include <bob.extension/documentation.h>
// declare C++ functions
void remove_highlights_orig( blitz::Array<float ,3> &img,
blitz::Array<float ,3> &diff,
blitz::Array<float ,3> &sfi,
blitz::Array<float ,3> &residue,
float epsilon);
// declare the function
static PyObject* PyRemoveHighlights(PyObject*, PyObject* args, PyObject* kwargs) {
BOB_TRY
static const char* const_kwlist[] = {"array", "startEps", 0};
static char** kwlist = const_cast<char**>(const_kwlist);
PyBlitzArrayObject* array;
double epsilon = 0.5f;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|d", kwlist,
&PyBlitzArray_Converter, &array,
&epsilon)) return 0;
// check that the array has the expected properties
if (array->type_num != NPY_FLOAT32|| array->ndim != 3){
PyErr_Format(PyExc_TypeError,
"remove_highlights : Only 3D arrays of type float32 are allowed");
return 0;
}
// extract the actual blitz array from the Python type
blitz::Array<float ,3> img = *PyBlitzArrayCxx_AsBlitz<float , 3>(array);
// results
int dim_x = img.shape()[2];
int dim_y = img.shape()[1];
blitz::Array<float ,3> diffuse_img(3, dim_y, dim_x);
blitz::Array<float ,3> speckle_free_img(3, dim_y, dim_x);
blitz::Array<float ,3> speckle_img(3, dim_y, dim_x);
diffuse_img = 0;
speckle_free_img = 0;
speckle_img = 0;
// call the C++ function
remove_highlights_orig(img, diffuse_img, speckle_free_img, speckle_img, (float)epsilon);
// convert the blitz array back to numpy and return it
PyObject *ret_tuple = PyTuple_New(3);
PyTuple_SetItem(ret_tuple, 0, PyBlitzArrayCxx_AsNumpy(speckle_free_img));
PyTuple_SetItem(ret_tuple, 1, PyBlitzArrayCxx_AsNumpy(diffuse_img));
PyTuple_SetItem(ret_tuple, 2, PyBlitzArrayCxx_AsNumpy(speckle_img));
return ret_tuple;
// handle exceptions that occurred in this function
BOB_CATCH_FUNCTION("remove_highlights", 0)
}
//////////////////////////////////////////////////////////////////////////
/////// Python module declaration ////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// module-wide methods
static PyMethodDef module_methods[] = {
{
"remove_highlights",
(PyCFunction)PyRemoveHighlights,
METH_VARARGS|METH_KEYWORDS,
"remove_highlights [doc]"
},
{NULL} // Sentinel
};
// module documentation
PyDoc_STRVAR(module_docstr, "Exemplary Python Bindings");
// module definition
#if PY_VERSION_HEX >= 0x03000000
static PyModuleDef module_definition = {
PyModuleDef_HEAD_INIT,
BOB_EXT_MODULE_NAME,
module_docstr,
-1,
module_methods,
0, 0, 0, 0
};
#endif
// create the module
static PyObject* create_module (void) {
# if PY_VERSION_HEX >= 0x03000000
PyObject* module = PyModule_Create(&module_definition);
auto module_ = make_xsafe(module);
const char* ret = "O";
# else
PyObject* module = Py_InitModule3(BOB_EXT_MODULE_NAME, module_methods, module_docstr);
const char* ret = "N";
# endif
if (!module) return 0;
if (PyModule_AddStringConstant(module, "__version__", BOB_EXT_MODULE_VERSION) < 0) return 0;
/* imports bob.blitz C-API + dependencies */
if (import_bob_blitz() < 0) return 0;
return Py_BuildValue(ret, module);
}
PyMODINIT_FUNC BOB_EXT_ENTRY_NAME (void) {
# if PY_VERSION_HEX >= 0x03000000
return
# endif
create_module();
}
This diff is collapsed.
/**
* @author Andre Anjos <andre.anjos@idiap.ch>
* @date Thu 7 Nov 13:50:16 2013
*
* @brief Binds configuration information available from bob
*/
#define BOB_IMPORT_VERSION
#include <bob.blitz/config.h>
#include <bob.blitz/cleanup.h>
// builds the dictionary of versions
static PyObject* build_version_dictionary() {
PyObject* retval = PyDict_New();
if (!retval) return 0;
auto retval_ = make_safe(retval);
if (!dict_steal(retval, "Blitz++", blitz_version())) return 0;
if (!dict_steal(retval, "Boost", boost_version())) return 0;
if (!dict_steal(retval, "Compiler", compiler_version())) return 0;
if (!dict_steal(retval, "Python", python_version())) return 0;
if (!dict_steal(retval, "NumPy", numpy_version())) return 0;
if (!dict_steal(retval, "bob.blitz", bob_blitz_version())) return 0;
return Py_BuildValue("O", retval);
}
static PyMethodDef module_methods[] = {
{0} /* Sentinel */
};
PyDoc_STRVAR(module_docstr,
"Information about software used to compile the C++ Bob API"
);
#if PY_VERSION_HEX >= 0x03000000
static PyModuleDef module_definition = {
PyModuleDef_HEAD_INIT,
BOB_EXT_MODULE_NAME,
module_docstr,
-1,
module_methods,
0, 0, 0, 0
};
#endif
static PyObject* create_module (void) {
# if PY_VERSION_HEX >= 0x03000000
PyObject* module = PyModule_Create(&module_definition);
auto module_ = make_xsafe(module);
const char* ret = "O";
# else
PyObject* module = Py_InitModule3(BOB_EXT_MODULE_NAME, module_methods, module_docstr);
const char* ret = "N";
# endif
if (!module) return 0;
/* register version numbers and constants */
if (PyModule_AddStringConstant(module, "module", BOB_EXT_MODULE_VERSION) < 0) return 0;
if (PyModule_AddObject(module, "externals", build_version_dictionary()) < 0) return 0;
return Py_BuildValue(ret, module);
}
PyMODINIT_FUNC BOB_EXT_ENTRY_NAME (void) {
# if PY_VERSION_HEX >= 0x03000000
return
# endif
create_module();
}
......@@ -3,11 +3,18 @@
# Sushil Bhattacharjee <sushil.bhattacharjee@idiap.ch>
# Tue 7 Mar 2017 11:26:26 CET
setup_packages = ['bob.extension', 'bob.blitz']
bob_packages = []
from setuptools import setup, find_packages, dist
dist.Distribution(dict(setup_requires=['bob.extension']))
dist.Distribution(dict(setup_requires = setup_packages + bob_packages))
# import the Extension class and the build_ext function from bob.blitz
from bob.blitz.extension import Extension, build_ext
# load the requirements.txt for additional requirements
from bob.extension.utils import load_requirements
requirements = load_requirements()
requirements = setup_packages + bob_packages + load_requirements()
version = open("version.txt").read().rstrip()
......@@ -31,6 +38,42 @@ setup(
install_requires = requirements,
# We are defining two extensions here. Each of them will be compiled
# independently into a separate .so file.
ext_modules = [
# The first extension defines the version of this package and all C++-dependencies.
Extension("bob.ip.qualitymeasure.version",
# list of files compiled into this extension
[
"bob/ip/qualitymeasure/version.cpp",
],
# additional parameters, see Extension documentation
version = version,
bob_packages = bob_packages,
),
# The second extension contains the actual C++ code and the Python bindings
Extension("bob.ip.qualitymeasure._library",
# list of files compiled into this extension
[
# the pure C++ code
"bob/ip/qualitymeasure/tan_specular_highlights_orig.cpp",
# the Python bindings
"bob/ip/qualitymeasure/main.cpp",
],
# additional parameters, see Extension documentation
version = version,
bob_packages = bob_packages,
),
],
# Important! We need to tell setuptools that we want the extension to be
# compiled with our build_ext function!
cmdclass = {
'build_ext': build_ext,
},
entry_points={
# scripts should be declared using this entry:
'console_scripts': [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment