ImageEn, unit ievision

TIEVisionBackgroundSubtractor.selectMethodKNN

TIEVisionBackgroundSubtractor.selectMethodKNN


Declaration

procedure selectMethodKNN(history: int32_t = 500; dist2Threshold: double = 400.0; detectShadows: bool32 = true); safecall;


Description

Select the K-nearest neigbours - based Background/Foreground Segmentation Algorithm.

Parameter Description
history Length of the history
dist2Threshold Threshold on the squared distance between the pixel and the sample to decide whether a pixel is close to that sample
detectShadows If true, the algorithm will detect shadows and mark them (shadows will show in the mask as gray, i.e. R,G,B = 127)




Demo

Demo  Demos\IEVision\BackgroundSubtract\BackgroundSubtract.dpr


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);
var
  w, h: integer;
  f: AnsiString;
begin
  VideoInputForm.Connect;

  // start capture
  ImageEnView1.IO.DShowParams.Run;

  // set up background subtractor
  m_backgroundSubtractor.selectMethodKNN();
end;

// We have a new frame
procedure TMainForm.ImageEnView1DShowNewFrame(Sender: TObject);
var
  fgmask: TIEVisionImage;
  learningRate: double;
begin
  // copy current sample to ImageEnView bitmap
  ImageEnView1.IO.DShowParams.GetSample(ImageEnView1.IEBitmap);

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

  // 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
  ImageEnView1.Update();
  ImageEnView2.Update();
  ImageEnView3.Update();
end;