Skip to content

Commit

Permalink
Implemented new bindings for gamma correction.
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Guenther committed Jul 3, 2014
1 parent 46deae3 commit 88c50be
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 89 deletions.
56 changes: 56 additions & 0 deletions bob/ip/base/auxiliary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,62 @@ PyObject* PyBobIpBase_histogramEqualization(PyObject*, PyObject* args, PyObject*
}


bob::extension::FunctionDoc s_gammaCorrection = bob::extension::FunctionDoc(
"gamma_correction",
"Performs a power-law gamma correction of a given 2D image",
".. todo:: Explain gamma correction in more detail"
)
.add_prototype("src, gamma, [dst]", "dst")
.add_parameter("src", "array_like (2D)", "The source image to compute the histogram for")
.add_parameter("gamma", "float", "The gamma value to apply")
.add_parameter("dst", "array_like (2D, float)", "The gamma-corrected image to write; if not specified, it will be created in the desired size")
.add_return("dst", "array_like (2D, float)", "The gamma-corrected image; the same as the ``dst`` parameter, if specified")
;

template <typename T> PyObject* inner_gammaCorrection(PyBlitzArrayObject* src, PyBlitzArrayObject* dst, double gamma) {
bob::ip::base::gammaCorrection(*PyBlitzArrayCxx_AsBlitz<T, 2>(src), *PyBlitzArrayCxx_AsBlitz<double, 2>(dst), gamma);
Py_INCREF(dst);
return PyBlitzArray_AsNumpyArray(dst, 0);
}

PyObject* PyBobIpBase_gammaCorrection(PyObject*, PyObject* args, PyObject* kwargs) {
TRY
static char* kwlist[] = {c("src"), c("gamma"), c("dst"), NULL};

PyBlitzArrayObject* src = 0,* dst = 0;
double gamma;

if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&d|O&", kwlist, &PyBlitzArray_Converter, &src, &gamma, &PyBlitzArray_OutputConverter, &dst)) return 0;
auto src_ = make_safe(src), dst_ = make_xsafe(dst);

// perform some checks
if (src->ndim != 2 || (dst && dst->ndim != 2)){
PyErr_Format(PyExc_ValueError, "'gamma_correction' can be performed on 2D arrays only");
return 0;
}

if (dst){
if (dst->ndim != 2 || dst->type_num != NPY_FLOAT64){
PyErr_Format(PyExc_TypeError, "'gamma_correction': ``dst`` must be a 2D array of type float");
return 0;
}
} else {
dst = (PyBlitzArrayObject*)PyBlitzArray_SimpleNew(NPY_FLOAT64, 2, src->shape);
dst_ = make_safe(dst);
}

switch (src->type_num){
case NPY_UINT8: return inner_gammaCorrection<uint8_t>(src, dst, gamma);
case NPY_UINT16: return inner_gammaCorrection<uint16_t>(src, dst, gamma);
case NPY_FLOAT64: return inner_gammaCorrection<double>(src, dst, gamma);
default:
PyErr_Format(PyExc_ValueError, "'gamma_correction' of %s arrays is currently not supported, only uint8, uint16 or float64 arrays are", PyBlitzArray_TypenumAsString(dst->type_num));
return 0;
}
CATCH_("in gamma_correction", 0)
}


bob::extension::FunctionDoc s_zigzag = bob::extension::FunctionDoc(
"zigzag",

Expand Down
32 changes: 29 additions & 3 deletions bob/ip/base/cpp/TanTriggs.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,37 @@
#include "bob/core/assert.h"
#include "bob/sp/conv.h"
#include "bob/sp/extrapolate.h"
// TODO: replace with local version
#include "bob/ip/gammaCorrection.h"

namespace bob { namespace ip { namespace base {

/**
* @brief Function which performs a gamma correction on a 2D
* blitz::array/image of a given type.
* The first dimension is the height (y-axis), whereas the second
* one is the width (x-axis).
* @param src The input blitz array
* @param dst The output blitz array (always double)
* @param gamma The gamma value for power-law gamma correction
*/
template<typename T>
void gammaCorrection(
const blitz::Array<T,2>& src,
blitz::Array<double,2>& dst,
const double gamma
){
// Check input/output
bob::core::array::assertZeroBase(src);
bob::core::array::assertZeroBase(dst);
bob::core::array::assertSameShape(dst, src);

// Check parameters and throw exception if required
if( gamma < 0.) throw std::runtime_error((boost::format("parameter `gamma' was set to %f, but should be greater or equal zero") % gamma).str());

// Perform gamma correction for the 2D array
dst = blitz::pow(src, gamma);
}


/**
* @brief This class can be used to perform Tan and Triggs preprocessing.
* This algorithm is described in the following articles:
Expand Down Expand Up @@ -147,7 +173,7 @@ namespace bob { namespace ip { namespace base {

// 1/ Perform gamma correction
if( m_gamma > 0.)
bob::ip::gammaCorrection( src, m_img_tmp, m_gamma);
bob::ip::base::gammaCorrection( src, m_img_tmp, m_gamma);
else
m_img_tmp = blitz::log( 1. + src );

Expand Down
6 changes: 6 additions & 0 deletions bob/ip/base/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ static PyMethodDef module_methods[] = {
METH_VARARGS|METH_KEYWORDS,
s_histogramEqualization.doc()
},
{
s_gammaCorrection.name(),
(PyCFunction)PyBobIpBase_gammaCorrection,
METH_VARARGS|METH_KEYWORDS,
s_gammaCorrection.doc()
},
{
s_zigzag.name(),
(PyCFunction)PyBobIpBase_zigzag,
Expand Down
4 changes: 4 additions & 0 deletions bob/ip/base/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ bool init_BobIpBaseDCTFeatures(PyObject* module);
int PyBobIpBaseDCTFeatures_Check(PyObject* o);



// gamma correction
PyObject* PyBobIpBase_gammaCorrection(PyObject*, PyObject*, PyObject*);
extern bob::extension::FunctionDoc s_gammaCorrection;
// Tan-Triggs
typedef struct {
PyObject_HEAD
Expand Down
81 changes: 0 additions & 81 deletions bob/ip/base/old/gamma_correction.cc

This file was deleted.

2 changes: 0 additions & 2 deletions bob/ip/base/old/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ void bind_core_bz_numpy();
void bind_sp_extrapolate();
void bind_sp_convolution();

void bind_ip_gamma_correction();
void bind_ip_shear();
void bind_ip_gaussian();
void bind_ip_gaussian_scale_space();
Expand Down Expand Up @@ -50,7 +49,6 @@ BOOST_PYTHON_MODULE(_old_library) {
bind_sp_extrapolate();
bind_sp_convolution();

bind_ip_gamma_correction();
bind_ip_shear();
bind_ip_gaussian();
bind_ip_gaussian_scale_space();
Expand Down
2 changes: 1 addition & 1 deletion bob/ip/base/tan_triggs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ int PyBobIpBaseTanTriggs_setAlpha(PyBobIpBaseTanTriggsObject* self, PyObject* va

static auto border = bob::extension::VariableDoc(
"border",
":py_class:`bob.sp.BorderType`",
":py:class:`bob.sp.BorderType`",
"The extrapolation method used by the convolution at the border, with read and write access"
);
PyObject* PyBobIpBaseTanTriggs_getBorder(PyBobIpBaseTanTriggsObject* self, void*){
Expand Down
32 changes: 31 additions & 1 deletion bob/ip/base/test/test_tantriggs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,37 @@

regenerate_reference = False

eps = 1e-4
def test_gamma_correction():
a2 = numpy.array([
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
[12, 13, 14, 15]],
dtype = numpy.uint16)

a2_g01 = numpy.array([
[0, 1, 1.0718, 1.1161],
[1.1487, 1.1746, 1.1962, 1.2148],
[1.2311, 1.2457, 1.2589, 1.2710],
[1.2821, 1.2924, 1.3020, 1.3110]],
dtype = numpy.float64)

a2_g11 = numpy.array([
[0, 1, 2.1435, 3.3484],
[4.5948, 5.8731, 7.1774, 8.5037],
[9.8492, 11.2116, 12.5893, 13.9808],
[15.3851, 16.8011, 18.2281, 19.6653]],
dtype = numpy.float64)

# gamma == 0.1
b2 = numpy.ndarray((4,4))
bob.ip.base.gamma_correction(a2, 0.1, b2);
assert numpy.allclose(b2, a2_g01, 1e-8, 1e-4);

# gamma == 0.1
b3 = bob.ip.base.gamma_correction(a2, 1.1);
assert numpy.allclose(b3, a2_g11, 1e-8, 1e-4);


def test_parametrization():
# Parametrization tests
Expand Down
7 changes: 7 additions & 0 deletions doc/py_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ Functions
bob.ip.base.block_output_shape
bob.ip.base.crop
bob.ip.base.shift

bob.ip.base.max_rect_in_mask
bob.ip.base.angle_to_horizontal

bob.ip.base.histogram
bob.ip.base.lbphs
bob.ip.base.lbphs_output_shape

bob.ip.base.histogram_equalization
bob.ip.base.gamma_correction

bob.ip.base.integral
bob.ip.base.zigzag

Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
),
Extension("bob.ip.base._old_library",
[
"bob/ip/base/old/gamma_correction.cc",
"bob/ip/base/old/gaussian.cc",
"bob/ip/base/old/GaussianScaleSpace.cc",
"bob/ip/base/old/GLCM.cc",
Expand Down

0 comments on commit 88c50be

Please sign in to comment.