Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement 'keepEdges' option for all frame difference detectors #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Controls/CameraWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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)":
Expand Down
10 changes: 6 additions & 4 deletions Vision/CustomFrameColorDifferenceDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
12 changes: 7 additions & 5 deletions Vision/CustomFrameDifferenceDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
}

Expand Down
55 changes: 53 additions & 2 deletions Vision/TwoFramesColorDifferenceDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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( );
Expand Down Expand Up @@ -179,6 +182,31 @@ public bool SuppressNoise
}
}

/// <summary>
/// Restore objects edges after noise suppression or not.
/// </summary>
///
/// <remarks><para>The value specifies if additional filtering should be done
/// to restore objects' edges after noise suppression by applying 3x3 dilatation
/// image processing filter.</para>
///
/// <para>Default value is set to <see langword="false"/>.</para>
///
/// <para><note>Turning the value on leads to more processing time of video frame.</note></para>
/// </remarks>
///
public bool KeepObjectsEdges
{
get { return _keepObjectEdges; }
set
{
lock (_sync)
{
_keepObjectEdges = value;
}
}
}

/// <summary>
/// Initializes a new instance of the <see cref="TwoFramesColorDifferenceDetector"/> class.
/// </summary>
Expand All @@ -194,6 +222,21 @@ public TwoFramesColorDifferenceDetector( ) { }
public TwoFramesColorDifferenceDetector( bool suppressNoise )
{
_suppressNoise = suppressNoise;
_keepObjectEdges = false;

}

/// <summary>
/// Initializes a new instance of the <see cref="TwoFramesColorDifferenceDetector"/> class.
/// </summary>
///
/// <param name="suppressNoise">Suppress noise in video frames or not (see <see cref="SuppressNoise"/> property).</param>
/// <param name="keepObjectEdges">Restore objects edges after noise suppression or not (see <see cref="KeepObjectsEdges"/> property).</param>
///
public TwoFramesColorDifferenceDetector( bool suppressNoise, bool keepObjectEdges )
{
_suppressNoise = suppressNoise;
_keepObjectEdges = keepObjectEdges;
}

/// <summary>
Expand Down Expand Up @@ -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
Expand Down
54 changes: 52 additions & 2 deletions Vision/TwoFramesDifferenceDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,16 @@ public class TwoFramesDifferenceDetector : IMotionDetector

// suppress noise
private bool _suppressNoise = true;
private bool _keepObjectEdges = false;

// threshold values
private int _differenceThreshold = 15;
private int _differenceThresholdNeg = -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( );
Expand Down Expand Up @@ -181,6 +184,31 @@ public bool SuppressNoise
}
}

/// <summary>
/// Restore objects edges after noise suppression or not.
/// </summary>
///
/// <remarks><para>The value specifies if additional filtering should be done
/// to restore objects' edges after noise suppression by applying 3x3 dilatation
/// image processing filter.</para>
///
/// <para>Default value is set to <see langword="false"/>.</para>
///
/// <para><note>Turning the value on leads to more processing time of video frame.</note></para>
/// </remarks>
///
public bool KeepObjectsEdges
{
get { return _keepObjectEdges; }
set
{
lock (_sync)
{
_keepObjectEdges = value;
}
}
}

/// <summary>
/// Initializes a new instance of the <see cref="TwoFramesDifferenceDetector"/> class.
/// </summary>
Expand All @@ -196,6 +224,20 @@ public TwoFramesDifferenceDetector( ) { }
public TwoFramesDifferenceDetector( bool suppressNoise )
{
_suppressNoise = suppressNoise;
_keepObjectEdges = false;
}

/// <summary>
/// Initializes a new instance of the <see cref="TwoFramesDifferenceDetector"/> class.
/// </summary>
///
/// <param name="suppressNoise">Suppress noise in video frames or not (see <see cref="SuppressNoise"/> property).</param>
/// <param name="keepObjectEdges">Restore objects edges after noise suppression or not (see <see cref="KeepObjectsEdges"/> property).</param>
///
public TwoFramesDifferenceDetector( bool suppressNoise, bool keepObjectEdges )
{
_suppressNoise = suppressNoise;
_keepObjectEdges = keepObjectEdges;
}

/// <summary>
Expand Down Expand Up @@ -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
Expand Down