Skip to content

thresholdThresholdThresholdthresholdthreshold🔗

Short description🔗

thresholdThresholdThresholdthresholdthreshold — Segment an image using global threshold.

Signature🔗

threshold( image Image, out region Region, number MinGray, number MaxGray )void Threshold( const HObject& Image, HObject* Region, const HTuple& MinGray, const HTuple& MaxGray )static void HOperatorSet.Threshold( HObject image, out HObject region, HTuple minGray, HTuple maxGray )def threshold( image: HObject, min_gray: MaybeSequence[Union[int, float, str]], max_gray: MaybeSequence[Union[int, float, str]] ) -> HObject

Herror threshold( const Hobject Image, Hobject* Region, double MinGray, double MaxGray )

Herror T_threshold( const Hobject Image, Hobject* Region, const Htuple MinGray, const Htuple MaxGray )

HRegion HImage::Threshold( const HTuple& MinGray, const HTuple& MaxGray ) const

HRegion HImage::Threshold( double MinGray, double MaxGray ) const

HRegion HImage.Threshold( HTuple minGray, HTuple maxGray )

HRegion HImage.Threshold( double minGray, double maxGray )

Description🔗

thresholdThreshold selects the pixels from the input image whose gray values \(g\) fulfill the following condition:

\[\begin{eqnarray*} \textrm{MinGray} \le g \le \textrm{MaxGray} \enspace . \end{eqnarray*}\]

All points of an image fulfilling the condition are returned as one region. If more than one gray value interval is passed (tuples for MinGrayminGraymin_gray and MaxGraymaxGraymax_gray), one separate region is returned for each interval. For vector field images, the threshold is not applied to gray values but to the lengths of the vectors. The parameters MinGrayminGraymin_gray and MaxGraymaxGraymax_gray can be set to 'min'"min" or 'max'"max" in order to leave bottom and top limits, respectively, open.

Attention🔗

The usage of MinGrayminGraymin_gray and MaxGraymaxGraymax_gray can be affected by the input image type.

For images of type integer, floating point values in MinGrayminGraymin_gray and MaxGraymaxGraymax_gray are truncated. For images of type real or vector field, floating point precision will be used for MinGrayminGraymin_gray and MaxGraymaxGraymax_gray, if they are of type double.

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 / direction / cyclic / int1 / int2 / uint2 / int4 / int8 / real / vector_field)HObject (byte / direction / cyclic / int1 / int2 / uint2 / int4 / int8 / real / vector_field)HImage (byte / direction / cyclic / int1 / int2 / uint2 / int4 / int8 / real / vector_field)HObject (byte / direction / cyclic / int1 / int2 / uint2 / int4 / int8 / real / vector_field)Hobject (byte / direction / cyclic / int1 / int2 / uint2 / int4 / int8 / real / vector_field)

Input image.

Regionregionregion (output_object) region(-array) → objectHObjectHRegionHObjectHobject *

Segmented region.

MinGrayminGraymin_gray (input_control) number(-array) → (real / integer / string)HTuple (double / Hlong / HString)HTuple (double / int / long / string)MaybeSequence[Union[int, float, str]]Htuple (double / Hlong / char*)

Lower threshold for the gray values or 'min'"min".

Default: 128.0128.0
Suggested values: 0.0, 10.0, 30.0, 64.0, 128.0, 200.0, 220.0, 255.0, 'min'0.0, 10.0, 30.0, 64.0, 128.0, 200.0, 220.0, 255.0, "min"

MaxGraymaxGraymax_gray (input_control) number(-array) → (real / integer / string)HTuple (double / Hlong / HString)HTuple (double / int / long / string)MaybeSequence[Union[int, float, str]]Htuple (double / Hlong / char*)

Upper threshold for the gray values or 'max'"max".

Default: 255.0255.0
Suggested values: 0.0, 10.0, 30.0, 64.0, 128.0, 200.0, 220.0, 255.0, 'max'0.0, 10.0, 30.0, 64.0, 128.0, 200.0, 220.0, 255.0, "max"
Restriction: MaxGray >= MinGray

Example🔗

(HDevelop)

read_image(Image,'fabrik')
sobel_dir(Image,EdgeAmp,EdgeDir,'sum_abs',3)
threshold(EdgeAmp,Seg,50,255)
skeleton(Seg,Rand)
connection(Rand,Lines)
select_shape(Lines,Edges,'area','and',10,1000000)
(C)
read_image(&Image,"fabrik")\;
sobel_amp(Image,&EdgeAmp,"sum_abs",3)\;
threshold(EdgeAmp,&Seg,50.0,255.0)\;
skeleton(Seg,&Rand)\;
connection(Rand,&Lines)\;
select_shape(Lines,&Edges,"area","and",10.0,1000000.0)\;
(C++)
#include "HIOStream.h"
#if !defined(USE_IOSTREAM_H)
using namespace std\;
#endif
#include "HalconCpp.h"
using namespace Halcon\;

int main (int argc, char *argv[])
{
  if (argc != 4)
  {
    cout << "Usage : " << argv[0] << " 'image' MinGray MaxGray" << endl\;
    return (-1)\;
  }
 HImage   image (argv[1]),
           Sobel\;
  HWindow  win\;
 image.Display (win)\;
 int MinGray = atoi (argv[2])\;
  int MaxGray = atoi (argv[3])\;
 Sobel = image.SobelAmp ("sum_abs", 3)\;
 HRegionArray rand  = ((image >= MinGray) & (image <= MaxGray)).Skeleton()\;
  HRegionArray lines = rand.Connection()\;
  HRegionArray edges = lines.SelectShape("area", "and", 10.0, 10000000.0)\;
 edges.Display (win)\;
  win.Click ()\;
 return (0)\;
}

Complexity🔗

Let \(A\) be the area of the input region. Then the runtime complexity is \(O(A)\).

Result🔗

thresholdThreshold returns 2 (H_MSG_TRUE) if all parameters are correct. The behavior with respect to the input images and output regions can be determined by setting the values of the flags 'no_object_result'"no_object_result", 'empty_region_result'"empty_region_result", and 'store_empty_region'"store_empty_region" with set_systemSetSystem. If necessary, an exception is raised.

Combinations with other operators🔗

Combinations

Possible predecessors

histo_to_threshHistoToThresh, min_max_grayMinMaxGray, sobel_ampSobelAmp, binomial_filterBinomialFilter, gauss_filterGaussFilter, reduce_domainReduceDomain, fill_interlaceFillInterlace

Possible successors

connectionConnection, dilation1Dilation1, erosion1Erosion1, openingOpening, closingClosing, rank_regionRankRegion, shape_transShapeTrans, skeletonSkeleton

Alternatives

class_2dim_supClass2dimSup, hysteresis_thresholdHysteresisThreshold, dyn_thresholdDynThreshold, binary_thresholdBinaryThreshold, char_thresholdCharThreshold, auto_thresholdAutoThreshold, dual_thresholdDualThreshold

See also

zero_crossingZeroCrossing, background_segBackgroundSeg, regiongrowingRegiongrowing

Module🔗

Foundation