Image Component Library (ICL)
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
icl::filter::LocalThresholdOp Class Reference

LocalThreshold Filter class. More...

#include <LocalThresholdOp.h>

Inheritance diagram for icl::filter::LocalThresholdOp:
icl::filter::UnaryOp icl::utils::Uncopyable icl::utils::Configurable

Public Types

enum  algorithm { regionMean, tiledNN, tiledLIN, global }
 Internally used algorithm. More...
- Public Types inherited from icl::utils::Configurable
typedef Function< void, const Property & > Callback
 Function type for changed properties. More...

Public Member Functions

 LocalThresholdOp (unsigned int maskSize=10, float globalThreshold=0, float gammaSlope=0)
 create a new LocalThreshold object with given mask-size and global threshold and RegionMean algorithm More...
 LocalThresholdOp (algorithm a, int maskSize=10, float globalThreshold=0, float gammaSlope=0)
 creates a LocalThresholdOp instance with given Algorithm-Type More...
 ~LocalThresholdOp ()
 Destructor. More...
virtual void apply (const core::ImgBase *src, core::ImgBase **dst)
 virtual apply function More...
void setMaskSize (unsigned int maskSize)
 set a new mask size (a new mask size image must be calculate in the next apply call) More...
void setGlobalThreshold (float globalThreshold)
 sets a enw global threshold value to used More...
void setGammaSlope (float gammaSlope)
 sets a new gamma slope to used (if gammaSlope is 0), the binary threshold is used More...
void setup (unsigned int maskSize, float globalThreshold, algorithm a=regionMean, float gammaSlope=0)
 sets all parameters at once More...
unsigned int getMaskSize () const
 returns the current mask size More...
float getGlobalThreshold () const
 returns the current global threshold value More...
float getGammaSlope () const
 returns the current gamma slope More...
algorithm getAlgorithm () const
 returns currently used algorithm type More...
void setAlgorithm (algorithm a)
 sets internally used algorithm More...
- Public Member Functions inherited from icl::filter::UnaryOp
 UnaryOp ()
 Explicit empty constructor. More...
 UnaryOp (const UnaryOp &other)
 Explicit copy constructor. More...
UnaryOpoperator= (const UnaryOp &other)
 Explicit declaration of the assignment operator. More...
virtual ~UnaryOp ()
 Destructor. More...
virtual ICL_DEPRECATED void applyMT (const core::ImgBase *operand1, core::ImgBase **dst, unsigned int nThreads)
 apply function for multithreaded filtering (currently even slower than using one thread) More...
virtual const core::ImgBaseapply (const core::ImgBase *src)
 applys the filter usign an internal buffer as output image More...
void operator() (const core::ImgBase *src, core::ImgBase **dst)
 function operator (alternative for apply(src,dst) More...
const core::ImgBaseoperator() (const core::ImgBase *src)
 function operator for the implicit destination apply(src) call More...
const core::ImgBaseoperator() (const core::ImgBase &src)
 reference based function operator More...
void setClipToROI (bool bClipToROI)
 sets if the image should be clip to ROI or not More...
void setCheckOnly (bool bCheckOnly)
 sets if the destination image should be adapted to the source, or if it is only checked if it can be adapted. More...
bool getClipToROI () const
 returns the ClipToROI status More...
bool getCheckOnly () const
 returns the CheckOnly status More...
virtual void setPropertyValue (const std::string &propertyName, const utils::Any &value) throw (utils::ICLException)
 sets value of a property (always call call_callbacks(propertyName) or Configurable::setPropertyValue) More...
- Public Member Functions inherited from icl::utils::Configurable
virtual ~Configurable ()
 virtual destructor More...
 Configurable (const Configurable &other)
 Copy constructor. More...
Configurableoperator= (const Configurable &other)
 Assignment operator. More...
void setConfigurableID (const std::string &ID) throw (ICLException)
 sets the ID of this configurable More...
const std::string & getConfigurableID () const
 returns the configurables static ID More...
bool isOrderedFlagSet () const
 returns whether the ordered flag is set More...
void deactivateProperty (const std::string &pattern)
 adds an additional deativation pattern More...
void deleteDeactivationPattern (const std::string &pattern)
 removed a formerly added deactivation pattern More...
std::vector< std::string > getPropertyListWithoutDeactivated () const
 this returns a filtered list of properties (using all filters added by deactivateProperty) More...
virtual void adaptProperty (const std::string &name, const std::string &newType, const std::string &newInfo, const std::string &newToolTip) throw (ICLException)
 this function can be used to adapt a specific property afterwards More...
void registerCallback (const Callback &cb)
 add a callback for changed properties More...
void removedCallback (const Callback &cb)
 removes a callback that was registered before More...
void syncChangesTo (Configurable *others, int num=1)
 this can be used to let this instance also apply property changes to others More...
virtual std::vector< std::string > getPropertyList () const
 returns a list of All properties, that can be set using setProperty More...
virtual bool supportsProperty (const std::string &propertyName) const
 base implementation for property check (seaches in the property list) More...
virtual void saveProperties (const std::string &filename, const std::vector< std::string > &propertiesToSkip=EMPTY_VEC) const
 writes all available properties into a file More...
virtual void loadProperties (const std::string &filename, const std::vector< std::string > &propertiesToSkip=EMPTY_VEC)
 reads a camera config file from disc More...
virtual std::string getPropertyType (const std::string &propertyName) const
 get type of property More...
virtual std::string getPropertyInfo (const std::string &propertyName) const
 get information of a properties valid values More...
virtual Any getPropertyValue (const std::string &propertyName) const
 returns the current value of a property or a parameter More...
virtual std::string getPropertyToolTip (const std::string &propertyName) const
 returns the tooltip description for a given property More...
virtual int getPropertyVolatileness (const std::string &propertyName) const
 Returns whether this property may be changed internally. More...

Private Member Functions

template<algorithm a>
void apply_a (const core::ImgBase *src, core::ImgBase **dst)
 internal algorithm function More...

Private Attributes

 mask size More...
 output ROI buffer image for ROI support More...
 IntegralImgOp for RegionMean algorithm. More...
 BinaryCompareOp for tiledXXX algorithms. More...
 first buffer for tiledXXX algorithsm More...
 second buffer for tiledXXX algorithsm More...

Additional Inherited Members

- Static Public Member Functions inherited from icl::filter::UnaryOp
static UnaryOpfromString (const std::string &definition) throw (utils::ICLException)
 Creates a UnaryOp instance from given string definition. More...
static std::string getFromStringSyntax (const std::string &opSpecifier) throw (utils::ICLException)
 gives a string syntax description for given opSpecifier More...
static std::vector< std::string > listFromStringOps ()
 returns a list of all supported OP_SPEC values for the fromString function More...
static void applyFromString (const std::string &definition, const core::ImgBase *src, core::ImgBase **dst) throw (utils::ICLException)
 creates, applies and releases a UnaryOp defined by given definition string More...
- Static Public Member Functions inherited from icl::utils::Configurable
static std::string create_default_ID (const std::string &prefix)
 this function can be used in subclasses to create a default ID More...
static Configurableget (const std::string &id)
 returns configurable by given ID More...
static void register_configurable_type (const std::string &classname, Function< Configurable * > creator) throw (ICLException)
 registers a configurable type More...
static std::vector< std::string > get_registered_configurables ()
 returns a list of all registered configurable classnames More...
static Configurablecreate_configurable (const std::string &classname) throw (ICLException)
 creates a configurable by given name More...
- Static Public Attributes inherited from icl::utils::Configurable
static const std::vector< std::string > EMPTY_VEC
 used as shortcut – just an empty vector of std::strings More...
- Protected Member Functions inherited from icl::filter::UnaryOp
bool prepare (core::ImgBase **ppoDst, core::depth eDepth, const utils::Size &imgSize, core::format eFormat, int nChannels, const utils::Rect &roi, utils::Time timestamp=utils::Time::null)
virtual bool prepare (core::ImgBase **ppoDst, const core::ImgBase *poSrc)
 check+adapt destination image to properties of given source image More...
virtual bool prepare (core::ImgBase **ppoDst, const core::ImgBase *poSrc, core::depth eDepth)
- Protected Member Functions inherited from icl::utils::Configurable
void addProperty (const std::string &name, const std::string &type, const std::string &info, const Any &value=Any(), const int volatileness=0, const std::string &tooltip=std::string()) throw (ICLException)
 This can be used by derived classes to store supported properties in the internal list. More...
void addChildConfigurable (Configurable *configurable, const std::string &childPrefix="")
 This adds another configurable as child. More...
void removeChildConfigurable (Configurable *configurable)
 removes the given child configurable More...
Propertyprop (const std::string &propertyName) throw (ICLException)
 this CAN be used e.g. to store a property value in internal property-list More...
const Propertyprop (const std::string &propertyName) const throw (ICLException)
 this CAN be used e.g. to store a property value in internal property-list More...
 Configurable (const std::string &ID="", bool ordered=true) throw (ICLException)
 create this configurable with given ID More...
void call_callbacks (const std::string &propertyName, const Configurable *caller) const
 calls all registered callbacks More...
- Protected Member Functions inherited from icl::utils::Uncopyable
 Uncopyable ()
 Empty base constructor. More...
- Protected Attributes inherited from icl::filter::UnaryOp
- Protected Attributes inherited from icl::utils::Configurable
std::vector< Callbackcallbacks
 internally managed list of callbacks More...

Detailed Description

LocalThreshold Filter class.

The LocalThresholdOp implements a set of local threshold algorithms. Currently, three implementations are available:

Result modes

This is the most sophisticated algorithms. Its implementation bases on the calculation of an integral image of the given input image (see icl::IntegralImgOp)
Once having access to the integral image data, it is possible to calculate the mean of an arbitrary rectangular image region in constant time. Consider the following ASCII example. A rectangular image region is always described by its four corner points A,B,C and D


The "mass" of pixels inside the rect can be obtained by calculating

\[X = D - A - B + C\]

in the integral image. The pixel count in the region is

\[P = (A.x-C.x) * (B.y-C.y) \]

, which directly leads to region-mean $X/P$.


A local image threshold at an image location $p=(px,py)$ must factor in the local image intensity, which can be approximated by the mean value $\mu_p$ of the square region centered at $p$ with a certain radius $r$. To emphazise that $\mu_p$ depends also on $r$ we denote it by $\mu_r{p}$. To have more influence on the actually used threshold at location $p$, we used an additive global threshold variable $g$.


Input: Input-Image i, region radius r, global threshold g, destination image d
1. I := integral image of i
2. (static) calculate region size image S
   This is nessesary because border regions do not have the expected region size
   (2*r+1)² due to the overlap with the image border. The calculation of S is not
   very complex, so it is not discussed here further.
3. I := extend (I). To avoid accessing invalid pixel locations of the integral image,
   it is enlarged to each edge by r. To garantee, that this optimization does not lead
   to incorrect results, the upper and the left border of the integral image is filled with
   zeros, and the lower and the right edge is filled with the value of the nearest non-
   border pixel.
4. Pixel Loop:
   For each pixel (x,y) of i
      4.1 Estimate the corresponding edge points A,B,C and D of the squared neighbourhood.
          (do not forget that the integral image has got a r-pixle border to each edge)
          C := (x-r,y-r)
          A := (x+r,y-r)     
          B := (x-1,y+r)
          D := (x+r,y+r)
      4.2 Calculate the region mean with respect to the underlying region size
          M = (I(D) - I(A) - I(B) + I(C))/S(x,y)
      4.3 Calculate the theshold operation
          d(x,y) = 255 * (i(x,y) > (M+g) )

Mutli channel images

This time, no special operation for multi channels images are implemneted, so each channel is process independently in this case.

Experimental feature gamma slope

By applying a small adaption, the procedure presented avoid can be used to apply a local gamma correction on a source image. We just have to exchange the "stair"-function above $ d(x,y) = 255 * (i(x,y) > (M+g) ) $ by a linear function:

using a linear function function f(x) = m*x + b    (with clipping to range [0,255])
with m = gammaSlope (new variable)
     k = M+g
     that f(k) = 128
   f(x) = m(x-k)+128     (clipped to [0,255])


The RegionMean algorithm clearly provides the best results of the three algorithms. The nearest neighbour interpolation algorithm is not significantly faster than the linear version, however it's results are much worse. Here is a set of results images. All images use a tile size of 30 and a global threshold of 3



All threshold implementations are fast and highly optimized. In particular, we spend a lot of time for the optimization of the RegionMean algorithms, which provides the best results. Benchmarks were performed on a 2GHz Intel Core2Duo Machine with 2GB Ram. Compiler g++ 4.3, optimization flags -O4 -march=native.

MaskSize tiledNN tiledLIN RegionMean
200 5 5 13ms
100 5 5 11ms
40 5 5 10ms
20 5 5 9ms
10 5 5 9ms
5 6 6 9ms
2 9 9 9ms

The experimental gamma-slope-computation is much more expensive: Here, the RegionMean algorithms needs about 25ms.

Member Enumeration Documentation

Internally used algorithm.


regionMean threshold calculation


tiled threshold with nearest neighbour interpolation


tiled threshold with linear interpolation


simple global threshold

Constructor & Destructor Documentation

icl::filter::LocalThresholdOp::LocalThresholdOp ( unsigned int  maskSize = 10,
float  globalThreshold = 0,
float  gammaSlope = 0 

create a new LocalThreshold object with given mask-size and global threshold and RegionMean algorithm

maskSizesize of the mask to use for calculations, the image width and height must each be larger than 2*maskSize.
globalThresholdadditive Threshold to the calculated reagions mean
gammaSlopegammaSlope (Default=0) (Experimental feature) if set to 0 (default) the binary threshold is used
icl::filter::LocalThresholdOp::LocalThresholdOp ( algorithm  a,
int  maskSize = 10,
float  globalThreshold = 0,
float  gammaSlope = 0 

creates a LocalThresholdOp instance with given Algorithm-Type

icl::filter::LocalThresholdOp::~LocalThresholdOp ( )


Member Function Documentation

virtual void icl::filter::LocalThresholdOp::apply ( const core::ImgBase src,
core::ImgBase **  dst 

virtual apply function

roi support is realized by copying the current input image ROI into a dedicate image buffer with no roi set

Implements icl::filter::UnaryOp.

template<algorithm a>
void icl::filter::LocalThresholdOp::apply_a ( const core::ImgBase src,
core::ImgBase **  dst 

internal algorithm function

algorithm icl::filter::LocalThresholdOp::getAlgorithm ( ) const

returns currently used algorithm type

float icl::filter::LocalThresholdOp::getGammaSlope ( ) const

returns the current gamma slope

float icl::filter::LocalThresholdOp::getGlobalThreshold ( ) const

returns the current global threshold value

unsigned int icl::filter::LocalThresholdOp::getMaskSize ( ) const

returns the current mask size

void icl::filter::LocalThresholdOp::setAlgorithm ( algorithm  a)

sets internally used algorithm

void icl::filter::LocalThresholdOp::setGammaSlope ( float  gammaSlope)

sets a new gamma slope to used (if gammaSlope is 0), the binary threshold is used

void icl::filter::LocalThresholdOp::setGlobalThreshold ( float  globalThreshold)

sets a enw global threshold value to used

void icl::filter::LocalThresholdOp::setMaskSize ( unsigned int  maskSize)

set a new mask size (a new mask size image must be calculate in the next apply call)

void icl::filter::LocalThresholdOp::setup ( unsigned int  maskSize,
float  globalThreshold,
algorithm  a = regionMean,
float  gammaSlope = 0 

sets all parameters at once

Member Data Documentation

BinaryCompareOp* icl::filter::LocalThresholdOp::m_cmp

BinaryCompareOp for tiledXXX algorithms.

currently used algorithm property algorithm m_algorithm;

IntegralImgOp* icl::filter::LocalThresholdOp::m_iiOp

IntegralImgOp for RegionMean algorithm.

core::ImgBase* icl::filter::LocalThresholdOp::m_roiBufDst

output ROI buffer image for ROI support

core::ImgBase* icl::filter::LocalThresholdOp::m_roiBufSrc

mask size

global threshold gamma slope input ROI buffer image for ROI support

core::ImgBase* icl::filter::LocalThresholdOp::m_tiledBuf1

first buffer for tiledXXX algorithsm

core::ImgBase* icl::filter::LocalThresholdOp::m_tiledBuf2

second buffer for tiledXXX algorithsm

The documentation for this class was generated from the following file: