Removes the background from the image.


#include "ltwrappr.h"

virtual L_INT LBitmap::SubtractBackground (uRollingBall, uShrinkSize, uBrightnessFactor, uFlags)


L_UINT uRollingBall

The radius (in pixels) of the ball that will roll over the entire image to determine the background. Recommended value is 50.

L_UINT uShrinkSize

Shrink size ratio used to minimize the image internally in order to increase the speed with little loss of accuracy. Possible values are:

Value Meaning
SBK_DEPEND [0] The Shrink Size depends on the ball size.
SBK_1_1 [1] No Resize (Highest accuracy).
SBK_1_2 [2] Resize to half width and height.
SBK_1_4 [3] Resize to quarter width and height.
SBK_1_8 [4] Resize to eighth width and height (very fast).

L_UINT uBrightnessFactor

Brightness factor for increasing or decreasing the brightness of the image.

Valid values range from 0 to 400. If you pass 100 the brightness remains unchanged. Lower values darken the image while higher values lighten the image.

L_UINT uFlags

Flags that indicate whether the background is darker than the foreground, and whether to show the objects without the background. You must select one from each group. Possible values are:

The following flags represent whether the background is darker than the foreground:

Value Meaning
SBK_BG_DARK [0x00000000] The background in the current image is darker than the foreground.
SBK_BG_BRIGHT [0x00000001] The background in the current image is brighter than the foreground.

The following flags represent whether to show the objects without a background:

Value Meaning
SBK_RES_SHOW [0x00000000] The output bitmap shows the result of the subtraction between the background and the original image.
SBK_BG_SHOW [0x00000010] The output bitmap shows only the background.


Value Meaning
SUCCESS The function was successful.
< 1 An error occurred. Refer to Return Codes.


This function is useful, especially with medical images and grayscale bitmaps in correcting non-uniform brightness.

The rolling ball algorithm works as follows:

  1. Consider the bitmap to be a 3-D surface and the z-axis to be the intensity of the image [The component V from the HSV color space].

  2. Roll a 3-D ball beneath the surface so all the points of the ball are under the surface with one or more points of the ball tangent to the surface.

  3. The tangent points to the rolling ball are considered to be the background.

Subtract the background from the original image.

The Rolling Ball Radius should be at least as large as the radius of the largest object in the image that is not part of the background to ensure the separation of the background from any objects.

A small radius allows the detection of small objects, whereas a larger radius will detect both small and large objects.

When subtracting the background, sometimes the result is dim. In such a case you can enhance the brightness after subtracting the background by using the uBrightness factor, which functions similar to LBitmap::Multiply. Passing 100 for uBrightness leaves the brightness unchanged.

Support for 12 and 16-bit grayscale and 48 and 64-bit color images is available only in the Document/Medical toolkits.

To update a status bar or detect a user interrupt during execution of this function, refer to LBase::EnableStatusCallback.

This function does not support signed data images. It returns the error code ERROR_SIGNED_DATA_NOT_SUPPORTED if a signed data image is passed to this function.

This function does not support 32-bit grayscale images. It returns the error code ERROR_GRAY32_UNSUPPORTED if a 32-bit grayscale image is passed to this function.

Subtract Background Function - Before

Subtract Background Function - Before

Subtract Background Function - After

Subtract Background Function - After

View additional platform support for this Subtract Background function.

Required DLLs and Libraries


Win32, x64.

See Also




L_INT LBitmap__SubtractBackgroundExample(LBitmap *pLeadBitmap)  
   /* Apply Subtract Background effect on the image*/ 
   return pLeadBitmap->SubtractBackground(50, 0, 0, SBK_BG_DARK | SBK_RES_SHOW);  
Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Raster Imaging C++ Class Library Help