Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Types | Public Member Functions | Private Member Functions | Private Attributes
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

List of all members.

Public Types

enum  algorithm { regionMean, tiledNN, tiledLIN }
 Internally used algorithm. 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
 LocalThresholdOp (algorithm a, int maskSize=10, float globalThreshold=0, float gammaSlope=0)
 creates a LocalThresholdOp instance with given Algorithm-Type
 ~LocalThresholdOp ()
 Destructor.
virtual void apply (const core::ImgBase *src, core::ImgBase **dst)
 virtual apply function
void setMaskSize (unsigned int maskSize)
 set a new mask size (a new mask size image must be calculate in the next apply call)
void setGlobalThreshold (float globalThreshold)
 sets a enw global threshold value to used
void setGammaSlope (float gammaSlope)
 sets a new gamma slope to used (if gammaSlope is 0), the binary threshold is used
void setup (unsigned int maskSize, float globalThreshold, algorithm a=regionMean, float gammaSlope=0)
 sets all parameters at once
unsigned int getMaskSize () const
 returns the current mask size
float getGlobalThreshold () const
 returns the current global threshold value
float getGammaSlope () const
 returns the current gamma slope
algorithm getAlgorithm () const
 returns currently used algorithm type
void setAlgorithm (algorithm a)
 sets internally used algorithm

Private Member Functions

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

Private Attributes

core::ImgBasem_roiBufSrc
 mask size
core::ImgBasem_roiBufDst
 output ROI buffer image for ROI support
IntegralImgOpm_iiOp
 IntegralImgOp for RegionMean algorithm.
BinaryCompareOpm_cmp
 BinaryCompareOp for tiledXXX algorithms.
core::ImgBasem_tiledBuf1
 first buffer for tiledXXX algorithsm
core::ImgBasem_tiledBuf2
 second buffer for tiledXXX algorithsm

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

        .C....A...
        ..+++++...
        ..+++++...
        ..+++++...
        ..+++++...    
        .B++++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$.

Threshold

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$.

Algorithm

        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) )
            endfor
        

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])
        

Result

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

threshold-region-mean.png
RegionMean
threshold-tiled-lin.png
tiled-LIN
threshold-tiled-nn.png
tiled-NN

Benchmarks

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.

Enumerator:
regionMean 

regionMean threshold calculation

tiledNN 

tiled threshold with nearest neighbour interpolation

tiledLIN 

tiled threshold with linear interpolation


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

Parameters:
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

Destructor.


Member Function Documentation

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

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 
) [private]

internal algorithm function

returns currently used algorithm type

returns the current gamma slope

returns the current global threshold value

returns the current mask size

sets internally used algorithm

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 for tiledXXX algorithms.

currently used algorithm property algorithm m_algorithm;

IntegralImgOp for RegionMean algorithm.

output ROI buffer image for ROI support

mask size

global threshold gamma slope input ROI buffer image for ROI support

first buffer for tiledXXX algorithsm

second buffer for tiledXXX algorithsm


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines