Skip to content

median_imageMedianImageMedianImagemedian_imagemedian_imageπŸ”—

Short descriptionπŸ”—

median_imageMedianImageMedianImagemedian_imagemedian_image β€” Compute a median filter with various masks.

SignatureπŸ”—

median_image( image Image, out image ImageMedian, string MaskType, integer Radius, string Margin )void MedianImage( const HObject& Image, HObject* ImageMedian, const HTuple& MaskType, const HTuple& Radius, const HTuple& Margin )static void HOperatorSet.MedianImage( HObject image, out HObject imageMedian, HTuple maskType, HTuple radius, HTuple margin )def median_image( image: HObject, mask_type: str, radius: int, margin: Union[int, float, str] ) -> HObject

Herror median_image( const Hobject Image, Hobject* ImageMedian, const char* MaskType, const Hlong Radius, const char* Margin )

Herror T_median_image( const Hobject Image, Hobject* ImageMedian, const Htuple MaskType, const Htuple Radius, const Htuple Margin )

HImage HImage::MedianImage( const HString& MaskType, Hlong Radius, const HTuple& Margin ) const

HImage HImage::MedianImage( const HString& MaskType, Hlong Radius, const HString& Margin ) const

HImage HImage::MedianImage( const char* MaskType, Hlong Radius, const char* Margin ) const

HImage HImage::MedianImage( const wchar_t* MaskType, Hlong Radius, const wchar_t* Margin ) const (Windows only)

HImage HImage.MedianImage( string maskType, int radius, HTuple margin )

HImage HImage.MedianImage( string maskType, int radius, string margin )

DescriptionπŸ”—

median_imageMedianImage performs a median filter on the input image Imageimageimage with a square or circular mask and returns the filtered image in ImageMedianimageMedianimage_median. The shape of the mask can be selected with MaskTypemaskTypemask_type. The radius of the mask can be selected with Radiusradiusradius.

Conceptually, the median filter sorts all gray values within the mask in ascending order and then selects the median of the gray values. The median is the β€œmiddle” one of the sorted gray values, i.e., the gray value with rank (position) \((N - 1) / 2 + 1\) of the sorted gray values, where \(N\) denotes the number of pixels covered by the filter mask. Here, the rank 1 corresponds to the smallest gray value and the rank \(N\) corresponds to the largest gray value within the mask (see also rank_imageRankImage).

The filter mask is determined by Radiusradiusradius, defining the size, and MaskTypemaskTypemask_type, defining the shape of the mask. For latter one, the following options are available:

  • 'circle'"circle" The mask consists of the pixel within a circle with Radiusradiusradius around the pixel of the mask center.

  • 'square'"square" The mask consists of the pixel within a square with an edge length of \(2 \cdot\)Radiusradiusradius \(+ 1\) pixel.

median_imageMedianImage can be used, for example, to smooth images, to suppress unwanted objects (e.g., point-like or line-like structures) that are smaller than the mask, and can therefore be used to estimate the background illumination for a shading correction or as a preprocessing step for the dynamic threshold operation (see dyn_thresholdDynThreshold).

Several border treatments can be chosen for filtering via the parameter Marginmarginmargin:

  • gray value Pixels outside of the image border are assumed to be constant (with the indicated gray value).

  • 'continued'"continued" Continuation of the gray values at the image border.

  • 'cyclic'"cyclic" Cyclic continuation at the image borders.

  • 'mirrored'"mirrored" Reflection of pixels at the image borders.

When using the MaskTypemaskTypemask_type 'square'"square" with Radiusradiusradius 1 or 2 (resulting in a 3x3 or 5x5 pixel filter mask) and the border treatment β€˜mirrored’, median_imageMedianImage can be executed on OpenCL devices.

For an explanation of the concept of smoothing filters see the introduction of chapter Filters / Smoothing.

AttentionπŸ”—

median_imageMedianImage uses an algorithm with a runtime per pixel that depends on the mask height 2 * Radius + 1. Therefore, median_imageMedianImage is slower than median_rectMedianRect for square masks with a large mask height. The precise mask height for which median_rectMedianRect will become faster than median_imageMedianImage depends on the computer architecture (processor type, availability of SIMD instructions like SSE2 or MMX, cache size and throughput, memory throughput). Typically, this is the case for mask heights \(>\) 15, but can also be the case only for larger mask sizes, e.g., if SIMD instructions are unavailable and memory throughput is low.

Furthermore, it should be noted that median_rectMedianRect uses a recursive implementation, which internally computes the filter response on the smallest enclosing rectangle of the domain of the input image. Therefore, if the domain of the input image only covers a small fraction of the smallest enclosing rectangle, it can happen that median_imageMedianImage is faster than median_rectMedianRect even for larger mask heights.

Due to performance reasons, the input Imageimageimage is not checked whether it contains NaNs. Using an input image with NaNs crashes HALCON.

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
  • Supports OpenCL compute devices.

  • 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 channel level.

  • Automatically parallelized on domain level.

ParametersπŸ”—

Imageimageimage (input_object) (multichannel-)image(-array) β†’ object (byte* / int2* / uint2* / int4* / real*)HObject (byte* / int2* / uint2* / int4* / real*)HImage (byte* / int2* / uint2* / int4* / real*)HObject (byte* / int2* / uint2* / int4* / real*)Hobject (byte* / int2* / uint2* / int4* / real*) *allowed for compute devices

Image to be filtered.

ImageMedianimageMedianimage_median (output_object) (multichannel-)image(-array) β†’ object (byte / int2 / uint2 / int4 / real)HObject (byte / int2 / uint2 / int4 / real)HImage (byte / int2 / uint2 / int4 / real)HObject (byte / int2 / uint2 / int4 / real)Hobject * (byte / int2 / uint2 / int4 / real)

Filtered image.

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

Filter mask type.

Default: 'circle'"circle"
List of values: 'circle', 'square'"circle", "square"
List of values (for compute devices): 'square'"square"

Radiusradiusradius (input_control) integer β†’ (integer)HTuple (Hlong)HTuple (int / long)intHtuple (Hlong)

Radius of the filter mask.

Default: 11
List of values (for compute devices): 1, 21, 2
Suggested values: 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 15, 19, 25, 31, 39, 47, 591, 2, 3, 4, 5, 6, 7, 8, 9, 11, 15, 19, 25, 31, 39, 47, 59
Value range: 1 ≀ Radius ≀ 4095
Minimum increment: 1
Recommended increment: 2

Marginmarginmargin (input_control) string β†’ (string / integer / real)HTuple (HString / Hlong / double)HTuple (string / int / long / double)Union[int, float, str]Htuple (char* / Hlong / double)

Border treatment.

Default: 'mirrored'"mirrored"
List of values (for compute devices): 'mirrored'"mirrored"
Suggested values: 'mirrored', 'cyclic', 'continued', 0, 30, 60, 90, 120, 150, 180, 210, 240, 255"mirrored", "cyclic", "continued", 0, 30, 60, 90, 120, 150, 180, 210, 240, 255

ExampleπŸ”—

(HDevelop)


read_image (Image, 'fabrik')
median_image (Image, Median, 'circle', 3, 'continued')
dev_display(Median)

ComplexityπŸ”—

For each pixel: O(2 * Radius + 1).

ResultπŸ”—

If the parameter values are correct the operator median_imageMedianImage returns the value 2 (H_MSG_TRUE). The behavior in case of empty input (no input images available) is set via the operator set_system('no_object_result',<Result>). If necessary, an exception is raised.

Combinations with other operatorsπŸ”—

Combinations

Possible predecessors

read_imageReadImage

Possible successors

thresholdThreshold, dyn_thresholdDynThreshold, regiongrowingRegiongrowing

Alternatives

median_rectMedianRect, rank_imageRankImage, rank_rectRankRect

See also

gray_erosion_rectGrayErosionRect, gray_dilation_rectGrayDilationRect, gray_erosion_shapeGrayErosionShape, gray_dilation_shapeGrayDilationShape, gray_erosionGrayErosion, gray_dilationGrayDilation

ReferencesπŸ”—

T.S. Huang, G.J. Yang, G.Y. Tang; ``A Fast Two-Dimensional Median Filtering Algorithmβ€™β€˜; IEEE Transactions on Acoustics, Speech, and Signal Processing, vol.Β 27, no.Β 1, pp.Β 13-18, 1979.

R. Haralick, L. Shapiro; β€œComputer and Robot Vision”; Addison-Wesley, 1992, pp. 318-320.

ModuleπŸ”—

Foundation