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

Make update speed independet from pixel color differences. #6

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
49 changes: 36 additions & 13 deletions Vision/SimpleBackgroundModelingDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,19 @@ public class SimpleBackgroundModelingDetector : IMotionDetector

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

// threshold values
private int _differenceThreshold = 15;
private int _differenceThresholdNeg = -15;

// morphing speed, do an update step each "_framesPerBackgroundUpdate" frames:
private int _framesPerBackgroundUpdate = 2;
private int _framesCounter;
// morphing speed, number of steps required for full update (higher value is slower morphing):
private byte _framesUpdateSteps = 50; // 0 .. 255 where 0 is old behaviour.

private int _millisecondsPerBackgroundUpdate;
private int _millisecondsPerBackgroundUpdate = 0;
private int _millisecondsLeftUnprocessed;
private DateTime _lastTimeMeasurment;

Expand Down Expand Up @@ -388,7 +391,7 @@ public unsafe void ProcessFrame( UnmanagedImage videoFrame )
// pointers to background and current frames
byte* backFrame;
byte* currFrame;
int diff;
int diff, steps = _framesUpdateSteps;

// update background frame
if ( _millisecondsPerBackgroundUpdate == 0 )
Expand All @@ -401,16 +404,34 @@ public unsafe void ProcessFrame( UnmanagedImage videoFrame )
backFrame = (byte*) _backgroundFrame.ImageData.ToPointer( );
currFrame = (byte*) _motionFrame.ImageData.ToPointer( );

for ( int i = 0; i < _frameSize; i++, backFrame++, currFrame++ )
if ( steps == 0 ) // old behaviour
{
diff = *currFrame - *backFrame;
if ( diff > 0 )
for ( int i = 0; i < _frameSize; i++, backFrame++, currFrame++ )
{
( *backFrame )++;
diff = *currFrame - *backFrame;
if ( diff > 0 )
{
( *backFrame )++;
}
else if ( diff < 0 )
{
( *backFrame )--;
}
}
else if ( diff < 0 )
}
else
{
for ( int i = 0; i < _frameSize; i++, backFrame++, currFrame++ )
{
( *backFrame )--;
diff = *currFrame - *backFrame;
if ( diff > 0 )
{
( *backFrame ) += (byte)((steps - 1 + diff) / steps);
}
else if ( diff < 0 )
{
( *backFrame ) -= (byte)((steps - 1 - diff) / steps);
}
}
}
}
Expand Down Expand Up @@ -465,13 +486,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
51 changes: 37 additions & 14 deletions Vision/SimpleColorBackgroundModelingDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,18 @@ public class SimpleColorBackgroundModelingDetector : IMotionDetector

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

// threshold values
private int _differenceThreshold = 15;

// morphing speed, do an update step each "_framesPerBackgroundUpdate" frames:
private int _framesPerBackgroundUpdate = 2;
private int _framesCounter;
// morphing speed, number of steps required for full update (higher value is slower morphing):
private byte _framesUpdateSteps = 50; // 0 .. 255 where 0 is old behaviour.

private int _millisecondsPerBackgroundUpdate;
private int _millisecondsPerBackgroundUpdate = 0;
private int _millisecondsLeftUnprocessed;
private DateTime _lastTimeMeasurment;

Expand Down Expand Up @@ -379,7 +382,7 @@ public unsafe void ProcessFrame( UnmanagedImage videoFrame )
// pointers to background and current frames
byte* backFrame;
byte* currFrame;
int diff;
int diff, steps = _framesUpdateSteps;

// update background frame
if ( _millisecondsPerBackgroundUpdate == 0 )
Expand All @@ -391,17 +394,35 @@ public unsafe void ProcessFrame( UnmanagedImage videoFrame )

backFrame = (byte*) _backgroundFrame.ImageData.ToPointer( );
currFrame = (byte*) videoFrame.ImageData.ToPointer( );
for ( int i = 0; i < _frameSize; i++, backFrame++, currFrame++ )

if ( steps == 0 ) // old behaviour
{
diff = *currFrame - *backFrame;
if ( diff > 0 )
for ( int i = 0; i < _frameSize; i++, backFrame++, currFrame++ )
{
( *backFrame )++;
diff = *currFrame - *backFrame;
if ( diff > 0 )
{
( *backFrame )++;
}
else if ( diff < 0 )
{
( *backFrame )--;
}
}
else if ( diff < 0 )
}
else
{
for ( int i = 0; i < _frameSize; i++, backFrame++, currFrame++ )
{
( *backFrame )--;
diff = *currFrame - *backFrame;
if ( diff > 0 )
{
( *backFrame ) += (byte)((steps - 1 + diff) / steps);
}
else if ( diff < 0 )
{
( *backFrame ) -= (byte)((steps - 1 - diff) / steps);
}
}
}
}
Expand Down Expand Up @@ -471,13 +492,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