ImageEn, unit ievision



TIEVisionBackgroundSubtractor = interface(TIEVisionBase)


An interface to perform background/foreground segmentation.

Note: A shortcut method for this is available: SubtractImageBackground


Demo  Demos\IEVision\BackgroundSubtract\BackgroundSubtract.dpr
Demo  Demos\IEVision\BackgroundSubtractImage\BackgroundSubtractImage.dpr

Video Example

// Demo TImageEnViews:
// ImageEnView1: Captured video
// ImageEnView4: Image to use as background
// ImageEnView2: Foreground Mask
// ImageEnView3: Output with replaced background

// Capture button
procedure TMainForm.CaptureButtonClick(Sender: TObject);
  w, h: integer;
  f: AnsiString;

  // start capture

  // set up background subtractor

// We have a new frame
procedure TMainForm.ImageEnView1DShowNewFrame(Sender: TObject);
  fgmask: TIEVisionImage;
  learningRate: double;
  // copy current sample to ImageEnView bitmap

  // get foreground mask
  if CheckBoxLearning.Checked then
    learningRate := -1
    learningRate := 0;
  fgmask := m_backgroundSubtractor.apply(ImageEnView1.IEBitmap.GetIEVisionImage(), learningRate);

  // replace background using foreground mask
  ImageEnView4.Proc.ResampleTo(ImageEnView3.IEBitmap, ImageEnView1.IEBitmap.Width, ImageEnView1.IEBitmap.Height, rfNone);
  ImageEnView3.Proc.Merge(ImageEnView1.IEBitmap, ImageEnView2.IEBitmap);

  // refresh viewers

Image Example

Remove the background from an image based on a background only source image

SubtractorObject : A TIEVisionBackgroundSubtractor object, e.g. initialized in FormCreate using m_backgroundSubtractor := IEVisionLib.createBackgroundSubtractor();
BackgroundImage  : Image of background only
ForegroundImage  : Image with subject (in front of background)
DestMask         : If specified, is filled with the removal mask, an ie8g image, where 0 (black) is background, 127 (gray) is shadow and 255 (white) is foreground. Can be NIL
DestImage        : If specified, is filled with the subject with background removed (alpha channel). Can be NIL
Threshold        : The cut-off for the MOG2 algorithm. Smaller values return more background. Larger values return more transparency.
RemoveShadows    : Whether a detection algorithm is used to detect shadows and either mark them as gray in the DestMask or remove them
RemoveNoise      : Removes any isolated pixels smaller than the specified size (or 0 to skip)
Feathering       : Soften the edges of the subject (or 0 to skip)
  // RemoveShadows Constants
  Shadows_Ignore            = 0;
  Shadows_Detect            = 1;
  Shadows_Detect_and_Remove = 2;
procedure SubtractBackgroundFromImage(SubtractorObject: TIEVisionBackgroundSubtractor;
                                      BackgroundImage: TIEBitmap;
                                      ForegroundImage: TIEBitmap;
                                      DestMask: TIEBitmap;
                                      DestImage: TIEBitmap;
                                      Threshold: double = 16;
                                      RemoveShadows: Integer = Shadows_Detect_and_Remove;
                                      RemoveNoise: Integer = 3;
                                      Feathering: Integer = 0 );
  fgmask: TIEVisionImage;
  maskBmp: TIEBitmap;
  nullProgress: TProgressRec;
  abort: Boolean;
  maskBmp := nil;
    // setup background subtractor
    SubtractorObject.selectMethodMOG2( 500, Threshold, RemoveShadows in [ Shadows_Detect, Shadows_Detect_and_Remove ]);

    // "Learn" background
    SubtractorObject.Apply( BackgroundImage.GetIEVisionImage(), 1 );

    // Pass subject and get foreground mask
    fgmask := SubtractorObject.Apply( ForegroundImage.GetIEVisionImage(), 0 );

    if DestMask = nil then
      maskBmp := TIEBitmap.create
      maskBmp := DestMask;

    if RemoveShadows = Shadows_Detect_and_Remove then
      // Shadows will show in the mask as gray (R,G,B = 127). Convert them all to Black
      nullProgress := NullProgressRec( abort, False );
      _ConvertToBWThreshold( maskBmp, 228, nullProgress );

      if RemoveNoise > 0 then
        // Remove isolated B and W pixels smaller than "RemoveNoise"
        IERemoveIsolatedPixels1Bit( maskBmp, 0, 0 , MAXINT, MAXINT, True , RemoveNoise, True, nil, nil);
        IERemoveIsolatedPixels1Bit( maskBmp, 0, 0 , MAXINT, MAXINT, False, RemoveNoise, True, nil, nil);

      // Revert to ie8g so we can assign to Alpha channel
      maskBmp.PixelFormat := ie8g;

    if assigned( DestImage ) then
      DestImage.Assign( ForegroundImage );
      // Use the mask as the alpha channel
      DestImage.AlphaChannel.Assign( maskBmp );

      // Smooth edges
      DestImage.FeatherAlphaEdges( Feathering );

    if DestMask = nil then
      FreeAndNil( maskBmp );

procedure TMainForm.btnSubtractClick(Sender: TObject);
  if fBackgroundSubtractor = nil then
    fBackgroundSubtractor := IEVisionLib.createBackgroundSubtractor();
  SubtractBackgroundFromImage( fBackgroundSubtractor,
                               nil, // Don't want mask
                               5 );

Methods and Properties

Public Method  apply
Public Method  selectMethodGMG
Public Method  selectMethodMOG
Public Method  selectMethodMOG2
Public Method  selectMethodKNN

See Also

Public Method  createBackgroundSubtractor