Skip to content

local_thresholdLocalThresholdLocalThresholdlocal_thresholdlocal_thresholdπŸ”—

Short descriptionπŸ”—

local_thresholdLocalThresholdLocalThresholdlocal_thresholdlocal_threshold β€” Segment an image using local thresholding.

SignatureπŸ”—

local_threshold( image Image, out region Region, string Method, string LightDark, attribute.name GenParamName, attribute.value GenParamValue )void LocalThreshold( const HObject& Image, HObject* Region, const HTuple& Method, const HTuple& LightDark, const HTuple& GenParamName, const HTuple& GenParamValue )static void HOperatorSet.LocalThreshold( HObject image, out HObject region, HTuple method, HTuple lightDark, HTuple genParamName, HTuple genParamValue )def local_threshold( image: HObject, method: str, light_dark: str, gen_param_name: MaybeSequence[str], gen_param_value: MaybeSequence[Union[int, float]] ) -> HObject

Herror local_threshold( const Hobject Image, Hobject* Region, const char* Method, const char* LightDark, const char* GenParamName, const Hlong GenParamValue )

Herror T_local_threshold( const Hobject Image, Hobject* Region, const Htuple Method, const Htuple LightDark, const Htuple GenParamName, const Htuple GenParamValue )

HRegion HImage::LocalThreshold( const HString& Method, const HString& LightDark, const HTuple& GenParamName, const HTuple& GenParamValue ) const

HRegion HImage::LocalThreshold( const HString& Method, const HString& LightDark, const HString& GenParamName, Hlong GenParamValue ) const

HRegion HImage::LocalThreshold( const char* Method, const char* LightDark, const char* GenParamName, Hlong GenParamValue ) const

HRegion HImage::LocalThreshold( const wchar_t* Method, const wchar_t* LightDark, const wchar_t* GenParamName, Hlong GenParamValue ) const (Windows only)

HRegion HImage.LocalThreshold( string method, string lightDark, HTuple genParamName, HTuple genParamValue )

HRegion HImage.LocalThreshold( string method, string lightDark, string genParamName, int genParamValue )

DescriptionπŸ”—

local_thresholdLocalThreshold segments a single-channel image Imageimageimage using the thresholding method given in Methodmethodmethod and returns the segmented region in Regionregionregion. Currently the operator offers only the Method 'adapted_std_deviation'"adapted_std_deviation". This algorithm is a text binarization technique and provides good results for document images.

Adaptive ThresholdingπŸ”—

By selecting Methodmethodmethod = 'adapted_std_deviation'"adapted_std_deviation", a locally adaptive thresholding based on local mean and standard deviation according to Sauvola (see the paper in References) is invoked. The algorithm is able to segment document images even if they are degraded, e.g., due to inhomogeneous illumination or noise. It enables text binarization on an inhomogeneous background by taking into account the local contrast.

For a segmentation of the dark foreground (see parameter LightDarklightDarklight_dark), for a pixel at position texttt{(r,c)}, a local threshold texttt{T(r,c)} is calculated within a window of size 'mask_size'"mask_size"x'mask_size'"mask_size" (see the generic parameter 'mask_size'"mask_size") as follows:

\[\begin{eqnarray*} T(r,c) = \mu(r,c)\left(1+k \left(\frac{\sigma(r,c)}{R}-1\right)\right) \end{eqnarray*}\]

where \(\mu(r,c)\) is the local mean value within the window and \(\sigma(r,c)\) denotes the corresponding standard deviation. The parameter texttt{R} (see 'range'"range") is the assumed maximum value of the standard deviation (texttt{R} = 128 for byte images) and texttt{k} (see 'scale'"scale") a parameter that controls how much the threshold value texttt{T(r,c)} differs from the mean value \(\mu(r,c)\). If there is high contrast in the neighborhood of a point texttt{(r,c)} the standard deviation \(\sigma(r,c)\) has a value close to texttt{R} which yields a threshold value texttt{T(r,c)} close to the local mean \(\mu(r,c)\). If the contrast is low, the local threshold is below the local mean value. For dark text on light background containing also darker regions, this lower threshold enables the segmentation of the text even in darker areas.

The parameter LightDarklightDarklight_dark controls, whether light or dark structures are segmented.

  • If LightDarklightDarklight_dark = 'dark'"dark", dark structures on a light background are segmented. Every pixel texttt{p(r,c)} whose gray value is smaller than the calculated local threshold texttt{T(r,c)} is selected.

  • If LightDarklightDarklight_dark = 'light'"light", light structures on a dark background are segmented. The result is essentially the same as if the image would have been inverted and then, LightDarklightDarklight_dark was set to 'dark'"dark".

By setting GenParamNamegenParamNamegen_param_name to one of the following values, additional parameters specific for the 'adapted_std_deviation'"adapted_std_deviation" method can be set with GenParamValuegenParamValuegen_param_value:

  • 'mask_size'"mask_size": specifies the mask size, i.e., the size of the neighborhood in which the local threshold is calculated. The smaller the window size the thinner the segmented strokes. 'mask_size'"mask_size" must be set to a value that is larger than the stroke width of the characters or structures to be segmented. If 'mask_size'"mask_size" is even, the next larger odd value is used.

    Suggested values: 1515, 2121, 3131.

    Default: 1515.

  • 'scale'"scale": sets the parameter texttt{k} (\(0\leq k\)), that controls how much the threshold value differs from the local mean value. Use smaller values for 'scale'"scale" to also segment structures with a lower contrast to their background. Use larger values to suppress clutter.

    Suggested values: 0.20.2, 0.30.3, 0.50.5.

    Default: 0.20.2.

  • 'range'"range": sets the maximum assumed value of standard deviation texttt{R}. This parameter should be adapted based on the expected gray value range. As a rule of thumb, the value for 'range'"range" can be set to \(range = 0.5\cdot(MaxGray-MinGray)\), where MinGray and MaxGray are the minimum and maximum gray values in the image, which can be determined with min_max_grayMinMaxGray.

    Suggested values: 128128, 32767.532767.5.

    Default: 128128 (for byte images), 32767.532767.5 (for uint2 images).

AttentionπŸ”—

Note that filter operators may return unexpected results if an image with a reduced domain is used as input. Please refer to the chapter Filters.

Execution informationπŸ”—

Execution information
  • Multithreading type: reentrant (runs in parallel with non-exclusive operators).

  • Multithreading scope: global (may be called from any thread).

  • Automatically parallelized on tuple level.

  • Automatically parallelized on internal data level.

ParametersπŸ”—

Imageimageimage (input_object) singlechannelimage(-array) β†’ object (byte / uint2)HObject (byte / uint2)HImage (byte / uint2)HObject (byte / uint2)Hobject (byte / uint2)

Input Image.

Regionregionregion (output_object) region(-array) β†’ objectHObjectHRegionHObjectHobject *

Segmented output region.

Methodmethodmethod (input_control) string β†’ (string)HTuple (HString)HTuple (string)strHtuple (char*)

Segmentation method.

Default: 'adapted_std_deviation'"adapted_std_deviation"
List of values: 'adapted_std_deviation'"adapted_std_deviation"

LightDarklightDarklight_dark (input_control) string β†’ (string)HTuple (HString)HTuple (string)strHtuple (char*)

Extract foreground or background?

Default: 'dark'"dark"
List of values: 'dark', 'light'"dark", "light"

GenParamNamegenParamNamegen_param_name (input_control) attribute.name(-array) β†’ (string)HTuple (HString)HTuple (string)MaybeSequence[str]Htuple (char*)

List of generic parameter names.

Default: [][]
List of values: 'mask_size', 'range', 'scale'"mask_size", "range", "scale"

GenParamValuegenParamValuegen_param_value (input_control) attribute.value(-array) β†’ (integer / real)HTuple (Hlong / double)HTuple (int / long / double)MaybeSequence[Union[int, float]]Htuple (Hlong / double)

List of generic parameter values.

Default: [][]
Suggested values: 0.2, 15, 30, 128.00.2, 15, 30, 128.0

Combinations with other operatorsπŸ”—

Combinations

Possible successors

connectionConnection, select_shapeSelectShape, select_graySelectGray

Alternatives

auto_thresholdAutoThreshold, binary_thresholdBinaryThreshold, char_thresholdCharThreshold

See also

gray_histoGrayHisto, thresholdThreshold

ReferencesπŸ”—

J. Sauvola, M. PietikΓ€inen, ``Adaptive document image binarization”, Pattern Recognition, 33, 225-236 (2000)

ModuleπŸ”—

Foundation