Image Component Library (ICL)
|
LocalThreshold Filter class. More...
#include <LocalThresholdOp.h>
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::ImgBase * | m_roiBufSrc |
mask size | |
core::ImgBase * | m_roiBufDst |
output ROI buffer image for ROI support | |
IntegralImgOp * | m_iiOp |
IntegralImgOp for RegionMean algorithm. | |
BinaryCompareOp * | m_cmp |
BinaryCompareOp for tiledXXX algorithms. | |
core::ImgBase * | m_tiledBuf1 |
first buffer for tiledXXX algorithsm | |
core::ImgBase * | m_tiledBuf2 |
second buffer for tiledXXX algorithsm |
LocalThreshold Filter class.
The LocalThresholdOp implements a set of local threshold algorithms. Currently, three implementations are available:
2*maskSize
. The threshold of all pixels within this tile is set relatively to the mean-value of the tileThis 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
in the integral image. The pixel count in the region is
, which directly leads to region-mean .
A local image threshold at an image location must factor in the local image intensity, which can be approximated by the mean value of the square region centered at with a certain radius . To emphazise that depends also on we denote it by . To have more influence on the actually used threshold at location , we used an additive global threshold variable .
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
This time, no special operation for multi channels images are implemneted, so each channel is process independently in this case.
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 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.
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
maskSize | size of the mask to use for calculations, the image width and height must each be larger than 2*maskSize. |
globalThreshold | additive Threshold to the calculated reagions mean |
gammaSlope | gammaSlope (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.
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.
void icl::filter::LocalThresholdOp::apply_a | ( | const core::ImgBase * | src, |
core::ImgBase ** | dst | ||
) | [private] |
internal algorithm function
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
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
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