IPSDK 0.2
IPSDK : Image Processing Software Development Kit
Adaptive Threshold 2d binarizationSee full documentation
imageadaptiveThreshold2dImg (inImg,inKnlXY)
imageadaptiveThresholdGaussian2dImg (inImg,halfKnlSize)

Detailed Description

Binarize an input image according an adaptive threshold based on the pixel's neighbourhood.

For each pixel, the algorithm computes a threshold $T$ according to a neighbourhood described by the input kernel size as follows :

\[ T(x, y) = \sum_{(i, j) \in \aleph}{InImg(x+i, y+j) \times InKnlXY(i, j)} \]

Where $\aleph$ is the pixel's neighbourhood.

Note
The kernels coefficients must be normalized :

\[ \sum_{(i, j) \in \aleph}{InKnlXY(i, j)} = 1\]

For example, the threshold can be the mean intensity in the neighbourhood with each kernel coefficients set to $1/N$, with $N$ being the number of pixels in the neighbourhood.

It is also possible to call the adaptiveThresholdGaussian2dImg wrapper, which needs the half kernel size instead of the kernel as input data. In this case, a predifined Gaussian kernel is used, whose coefficients are computed with the folowing standard deviation $\sigma$ (see http://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html#Mat%20getGaussianKernel%28int%20ksize,%20double%20sigma,%20int%20ktype%29) :

\[ \sigma = 0.3*((KnlSize-1)*0.5 - 1) + 0.8 \]

Where $KnlSize$ is the kernel size : $ N = KnlSize \times KnlSize $.

Note
To avoid a noisy binarization, it is advised to filter the input image with a median filter before computing the binarization.

Here is an example of an adaptive image thresholding applied to a 8-bits grey level image, with a Gaussian kernel with a size of 5 :

adaptiveThreshold2dImg.png

Example of Python code :

Example imports

import PyIPSDK
import PyIPSDK.IPSDKIPLBinarization as bin

Code Example

# opening of input images
inImg = PyIPSDK.loadTiffImageFile(inputImgPath)
# Define the kernel according to a coefficients collection
inKnl = PyIPSDK.rectangularKernelXY(halfKnlSize, halfKnlSize, coeffColl)
# threshold computation with a given kernel
outImg = bin.adaptiveThreshold2dImg(inImg, inKnl)
# threshold computation with a predifined Gaussian kernel
outImgGauss = bin.adaptiveThresholdGaussian2dImg(inImg, halfKnlSize)

Example of C++ code :

Example informations

Header file

#include <IPSDKIPL/IPSDKIPLBinarization/Processor/AdaptiveThreshold2dImg/AdaptiveThreshold2dImg.h>

Code Example

// Sample with a generated output image
// ------------------------------------
// compute absolute value of input image
ImagePtr pAutoOutImg = adaptiveThreshold2dImg(pInImg, pInKnlXY);
// Sample with a provided output image
// -----------------------------------
// create output image
ImageGeometryPtr pOutputImageGeometry = geometry2d(eImageBufferType::eIBT_Binary, sizeX, sizeY);
boost::shared_ptr<MemoryImage> pOutImg(boost::make_shared<MemoryImage>());
pOutImg->init(*pOutputImageGeometry);
// compute absolute value of input image
adaptiveThreshold2dImg(pInImg, pInKnlXY, pOutImg);
// Sample with an automatically computed
// Gaussian kernel and a generated output image
// --------------------------------------------
// compute absolute value of input image
ImagePtr pAutoGaussOutImg = adaptiveThresholdGaussian2dImg(pInImg, halfKnlSize);