From 8bd9f7508d827ebbbb033fa5d9bfb52ee971e15b Mon Sep 17 00:00:00 2001 From: yablacky Date: Thu, 13 Oct 2016 22:50:59 +0200 Subject: [PATCH] Implement 'keepEdges' option for all frame difference detectors --- Controls/CameraWindow.cs | 4 +- Vision/CustomFrameColorDifferenceDetector.cs | 10 ++-- Vision/CustomFrameDifferenceDetector.cs | 12 +++-- Vision/TwoFramesColorDifferenceDetector.cs | 55 +++++++++++++++++++- Vision/TwoFramesDifferenceDetector.cs | 54 ++++++++++++++++++- 5 files changed, 120 insertions(+), 15 deletions(-) diff --git a/Controls/CameraWindow.cs b/Controls/CameraWindow.cs index 15adba72..e177a543 100644 --- a/Controls/CameraWindow.cs +++ b/Controls/CameraWindow.cs @@ -5377,7 +5377,7 @@ public void SetDetector() case "Two Frames": Camera.MotionDetector = new MotionDetector( - new TwoFramesDifferenceDetector(Camobject.settings.suppressnoise)); + new TwoFramesDifferenceDetector(Camobject.settings.suppressnoise,Camobject.detector.keepobjectedges)); SetProcessor(); break; case "Custom Frame": @@ -5395,7 +5395,7 @@ public void SetDetector() case "Two Frames (Color)": Camera.MotionDetector = new MotionDetector( - new TwoFramesColorDifferenceDetector(Camobject.settings.suppressnoise)); + new TwoFramesColorDifferenceDetector(Camobject.settings.suppressnoise,Camobject.detector.keepobjectedges)); SetProcessor(); break; case "Custom Frame (Color)": diff --git a/Vision/CustomFrameColorDifferenceDetector.cs b/Vision/CustomFrameColorDifferenceDetector.cs index 55d71bda..ff218200 100644 --- a/Vision/CustomFrameColorDifferenceDetector.cs +++ b/Vision/CustomFrameColorDifferenceDetector.cs @@ -336,13 +336,15 @@ public unsafe void ProcessFrame( UnmanagedImage videoFrame ) if ( _suppressNoise ) { // suppress noise and calculate motion amount - AForge.SystemTools.CopyUnmanagedMemory( _tempFrame.ImageData, _motionFrame.ImageData, _motionSize ); - _erosionFilter.Apply( _tempFrame, _motionFrame ); + _erosionFilter.Apply( _motionFrame, _tempFrame ); // src -> dst if ( _keepObjectEdges ) { - AForge.SystemTools.CopyUnmanagedMemory( _tempFrame.ImageData, _motionFrame.ImageData, _motionSize ); - _dilatationFilter.Apply( _tempFrame, _motionFrame ); + _dilatationFilter.Apply( _tempFrame, _motionFrame ); // src -> dst + } + else + { + AForge.SystemTools.CopyUnmanagedMemory( _motionFrame.ImageData, _tempFrame.ImageData, _motionSize ); // dst <- src } } diff --git a/Vision/CustomFrameDifferenceDetector.cs b/Vision/CustomFrameDifferenceDetector.cs index ffb4bb71..52542ccc 100644 --- a/Vision/CustomFrameDifferenceDetector.cs +++ b/Vision/CustomFrameDifferenceDetector.cs @@ -77,7 +77,7 @@ public class CustomFrameDifferenceDetector : IMotionDetector // suppress noise private bool _suppressNoise = true; - private bool _keepObjectEdges; + private bool _keepObjectEdges = false; // threshold values private int _differenceThreshold = 15; @@ -324,13 +324,15 @@ public unsafe void ProcessFrame( UnmanagedImage videoFrame ) if ( _suppressNoise ) { // suppress noise and calculate motion amount - AForge.SystemTools.CopyUnmanagedMemory( _tempFrame.ImageData, _motionFrame.ImageData, _frameSize ); - _erosionFilter.Apply( _tempFrame, _motionFrame ); + _erosionFilter.Apply( _motionFrame, _tempFrame ); // src -> dst if ( _keepObjectEdges ) { - AForge.SystemTools.CopyUnmanagedMemory( _tempFrame.ImageData, _motionFrame.ImageData, _frameSize ); - _dilatationFilter.Apply( _tempFrame, _motionFrame ); + _dilatationFilter.Apply( _tempFrame, _motionFrame ); // src -> dst + } + else + { + AForge.SystemTools.CopyUnmanagedMemory( _motionFrame.ImageData, _tempFrame.ImageData, _frameSize ); // dst <- src } } diff --git a/Vision/TwoFramesColorDifferenceDetector.cs b/Vision/TwoFramesColorDifferenceDetector.cs index 940a7764..712f0772 100644 --- a/Vision/TwoFramesColorDifferenceDetector.cs +++ b/Vision/TwoFramesColorDifferenceDetector.cs @@ -66,12 +66,15 @@ public class TwoFramesColorDifferenceDetector : IMotionDetector // suppress noise private bool _suppressNoise = true; + private bool _keepObjectEdges = false; // threshold values private int _differenceThreshold = 15; // binary erosion filter private readonly BinaryErosion3x3 _erosionFilter = new BinaryErosion3x3( ); + // binary dilatation filter + private readonly BinaryDilatation3x3 _dilatationFilter = new BinaryDilatation3x3(); // dummy object to lock for synchronization private readonly object _sync = new object( ); @@ -179,6 +182,31 @@ public bool SuppressNoise } } + /// + /// Restore objects edges after noise suppression or not. + /// + /// + /// The value specifies if additional filtering should be done + /// to restore objects' edges after noise suppression by applying 3x3 dilatation + /// image processing filter. + /// + /// Default value is set to . + /// + /// Turning the value on leads to more processing time of video frame. + /// + /// + public bool KeepObjectsEdges + { + get { return _keepObjectEdges; } + set + { + lock (_sync) + { + _keepObjectEdges = value; + } + } + } + /// /// Initializes a new instance of the class. /// @@ -194,6 +222,21 @@ public TwoFramesColorDifferenceDetector( ) { } public TwoFramesColorDifferenceDetector( bool suppressNoise ) { _suppressNoise = suppressNoise; + _keepObjectEdges = false; + + } + + /// + /// Initializes a new instance of the class. + /// + /// + /// Suppress noise in video frames or not (see property). + /// Restore objects edges after noise suppression or not (see property). + /// + public TwoFramesColorDifferenceDetector( bool suppressNoise, bool keepObjectEdges ) + { + _suppressNoise = suppressNoise; + _keepObjectEdges = keepObjectEdges; } /// @@ -279,8 +322,16 @@ public unsafe void ProcessFrame( UnmanagedImage videoFrame ) if ( _suppressNoise ) { // suppress noise and calculate motion amount - AForge.SystemTools.CopyUnmanagedMemory( _tempFrame.ImageData, _motionFrame.ImageData, _motionSize ); - _erosionFilter.Apply( _tempFrame, _motionFrame ); + _erosionFilter.Apply( _motionFrame, _tempFrame ); // src -> dst + + if ( _keepObjectEdges ) + { + _dilatationFilter.Apply( _tempFrame, _motionFrame ); // src -> dst + } + else + { + AForge.SystemTools.CopyUnmanagedMemory( _motionFrame.ImageData, _tempFrame.ImageData, _motionSize ); // dst <- src + } } // calculate amount of motion pixels diff --git a/Vision/TwoFramesDifferenceDetector.cs b/Vision/TwoFramesDifferenceDetector.cs index 0cb5a038..e5d171bc 100644 --- a/Vision/TwoFramesDifferenceDetector.cs +++ b/Vision/TwoFramesDifferenceDetector.cs @@ -66,6 +66,7 @@ public class TwoFramesDifferenceDetector : IMotionDetector // suppress noise private bool _suppressNoise = true; + private bool _keepObjectEdges = false; // threshold values private int _differenceThreshold = 15; @@ -73,6 +74,8 @@ public class TwoFramesDifferenceDetector : IMotionDetector // binary erosion filter private readonly BinaryErosion3x3 _erosionFilter = new BinaryErosion3x3( ); + // binary dilatation filter + private readonly BinaryDilatation3x3 _dilatationFilter = new BinaryDilatation3x3(); // dummy object to lock for synchronization private readonly object _sync = new object( ); @@ -181,6 +184,31 @@ public bool SuppressNoise } } + /// + /// Restore objects edges after noise suppression or not. + /// + /// + /// The value specifies if additional filtering should be done + /// to restore objects' edges after noise suppression by applying 3x3 dilatation + /// image processing filter. + /// + /// Default value is set to . + /// + /// Turning the value on leads to more processing time of video frame. + /// + /// + public bool KeepObjectsEdges + { + get { return _keepObjectEdges; } + set + { + lock (_sync) + { + _keepObjectEdges = value; + } + } + } + /// /// Initializes a new instance of the class. /// @@ -196,6 +224,20 @@ public TwoFramesDifferenceDetector( ) { } public TwoFramesDifferenceDetector( bool suppressNoise ) { _suppressNoise = suppressNoise; + _keepObjectEdges = false; + } + + /// + /// Initializes a new instance of the class. + /// + /// + /// Suppress noise in video frames or not (see property). + /// Restore objects edges after noise suppression or not (see property). + /// + public TwoFramesDifferenceDetector( bool suppressNoise, bool keepObjectEdges ) + { + _suppressNoise = suppressNoise; + _keepObjectEdges = keepObjectEdges; } /// @@ -267,8 +309,16 @@ public unsafe void ProcessFrame( UnmanagedImage videoFrame ) if ( _suppressNoise ) { // suppress noise and calculate motion amount - AForge.SystemTools.CopyUnmanagedMemory( _tempFrame.ImageData, _motionFrame.ImageData, _frameSize ); - _erosionFilter.Apply( _tempFrame, _motionFrame ); + _erosionFilter.Apply( _motionFrame, _tempFrame ); // src -> dst + + if ( _keepObjectEdges ) + { + _dilatationFilter.Apply( _tempFrame, _motionFrame ); // src -> dst + } + else + { + AForge.SystemTools.CopyUnmanagedMemory( _motionFrame.ImageData, _tempFrame.ImageData, _frameSize ); // dst <- src + } } // calculate amount of motion pixels