MilleVisionNotes

This site focuses on practical examples of controlling Basler industrial cameras
using the pylon SDK and C#, based on real-world development experience.

Implementing Background Subtraction (Motion Detection) in WPF with Basler pylon SDK + OpenCV (C# / .NET 8)

eng_wpf_background_subtraction_basler_pylon_opencv_article_v2

Once your live view is working, a natural next step is highlighting only what moved. In this article, we implement a simple background subtraction pipeline to detect motion regions, draw bounding boxes, and display the results in a WPF Image.


✅ Environment

Because this implementation converts between BitmapSource and Mat, install the NuGet package:

  • OpenCvSharp4.WpfExtensions
Item Details
Camera Basler acA2500-14gm
SDK pylon Camera Software Suite
Language / GUI C# / .NET 8 / WPF
Libraries OpenCvSharp4 (OpenCvSharp4.Windows, OpenCvSharp4.WpfExtensions)

Recommended camera settings (for stable subtraction): Set ExposureAuto=Off, GainAuto=Off, and keep illumination stable (reduce flicker).

Prerequisites from earlier posts (reference):


Implementation Overview

We will follow this workflow:

  1. Capture a background frame (a frame with no motion)
  2. Preprocess with Gaussian blur
  3. Compute absolute difference (AbsDiff) → threshold (Threshold) → opening (MorphologyEx(Open))
  4. Extract contours (FindContours) → draw bounding rectangles
  5. Display the processed frame in WPF

Adding opening after threshold helps remove small speckle noise.


🧩 XAML (Minimal UI)

Add one column for a “Set BG” button and sliders for tuning.

BackgroundSubtraction_View


🔧 Core Code (Background Subtraction Pipeline)

This logic ideally belongs in the Model layer, but to keep diffs small from previous articles, it is implemented in the ViewModel.

The author tested on a Mono8 camera. If you use a color camera, convert to grayscale as needed.

The ViewModel implements IDisposable to ensure OpenCV Mat resources are released.


Code-Behind

Dispose the ViewModel when the window closes.


Example Run

  1. Click Set BG to capture the background frame.

BackgroundSubtraction_BG 2. Move an object in the scene → motion regions are detected and boxed. 3. Use sliders to tune sensitivity and minimum area.

BackgroundSubtraction_Detect


Tuning Tips

  • Retake background when illumination changes

  • Threshold: increase to suppress sensor noise; decrease to detect subtle motion

  • Morphology iterations: increase if edges are jagged; decrease if boxes “bloat”

  • Min area: simple filter to reduce false positives

  • Speed-ups:

    • Apply camera ROI to reduce the processed area
    • Cv2.Resize to process a smaller image (scale coordinates back)
    • Cache blurred background (as shown) to avoid blurring every frame

Common Pitfalls

Symptom Likely Cause Fix
Nothing detected Threshold too high / changes too small Lower threshold or increase blur kernel
Entire frame white Auto exposure/gain is fluctuating Set ExposureAuto/GainAuto=Off, stabilize lighting
Too many speckles Sensor noise / tiny vibration Keep opening (MorphTypes.Open) and tune kernel
UI freezes Processing too heavy Move processing to another Task, use ROI / resize
Memory grows Mat.Dispose() missing Use using blocks (as shown) and cache carefully

Summary

  • Background subtraction highlights only moving regions
  • Fixed exposure/gain + stable lighting improves reliability
  • Tune threshold, minimum area, and morphology to balance sensitivity vs noise
  • ROI and resizing are effective for performance

コメント

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA