Home » Source Code » PowerPoint Controler » MotionDetector3Optimized.cs

MotionDetector3Optimized.cs ( File view )

Description:A s...
  • By vaselly 2014-09-16
  • View(s):40
  • Download(s):0
  • Point(s): 1
			// Motion Detector
//
// Copyright  Andrew Kirillov, 2005
// andrew.kirillov@gmail.com
//
namespace motion
{

	using System;
	using System.Drawing;
	using System.Drawing.Imaging;

	using AForge.Imaging;
	using AForge.Imaging.Filters;

	/// 
	/// MotionDetector3Optimized
	/// 
	public class MotionDetector3Optimized : IMotionDetector
	{

		private byte[]	backgroundFrame = null;
		private byte[]	currentFrame = null;
		private byte[]	currentFrameDilatated = null;

		private int		counter = 0;

		private bool	calculateMotionLevel = false;
		private int		width;	// image width
		private int		height;	// image height
		private int		pixelsChanged;

		// Motion level calculation - calculate or not motion level
		public bool MotionLevelCalculation
		{

			get {
 return calculateMotionLevel; 
}
			set {
 calculateMotionLevel = value; 
}
		
}
        public void SlideName(string slidename)
        {

            
        
}

		// Motion level - amount of changes in percents
		public double MotionLevel
		{

			get {
 return (double) pixelsChanged / ( width * height ); 
}
		
}

		// Constructor
		public MotionDetector3Optimized( )
		{

		
}

		// Reset detector to initial state
		public void Reset( )
		{

			backgroundFrame = null;
			currentFrame = null;
			currentFrameDilatated = null;
			counter = 0;
		
}

		// Process new frame
		public void ProcessFrame( ref Bitmap image )
		{

			// get image dimension
			width	= image.Width;
			height	= image.Height;

			int fW = ( ( ( width - 1 ) / 8 ) + 1 );
			int fH = ( ( ( height - 1 ) / 8 ) + 1 );
			int len = fW * fH;

			if ( backgroundFrame == null )
			{

				// alloc memory for a backgound image and for current image
				backgroundFrame = new byte[len];
				currentFrame = new byte[len];
				currentFrameDilatated = new byte[len];

				// lock image
				BitmapData imgData = image.LockBits(
					new Rectangle( 0, 0, width, height ),
					ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb );

				// create initial backgroung image
				PreprocessInputImage( imgData, width, height, backgroundFrame );

				// unlock the image
				image.UnlockBits( imgData );

				// just return for the first time
				return;
			
}

			// lock image
			BitmapData data = image.LockBits(
				new Rectangle( 0, 0, width, height ),
				ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb );

			// preprocess input image
			PreprocessInputImage( data, width, height, currentFrame );

			if ( ++counter == 2 )
			{

				counter = 0;

				// move background towards current frame
				for ( int i = 0; i < len; i++ )
				{

					int t = currentFrame[i] - backgroundFrame[i];
					if ( t > 0 )
						backgroundFrame[i]++;
					else if ( t < 0 )
						backgroundFrame[i]--;
				
}
			
}

			// difference and thresholding
			pixelsChanged = 0;
			for ( int i = 0; i < len; i++ )
			{

				int t = currentFrame[i] - backgroundFrame[i];
				if ( t < 0 )
					t = -t;

				if ( t >= 15 )
				{

					pixelsChanged++;
					currentFrame[i] = (byte) 255;
				
}
				else
				{

					currentFrame[i] = (byte) 0;
				
}
			
}
			if ( calculateMotionLevel )
				pixelsChanged *= 64;
			else
				pixelsChanged = 0;

			// dilatation analogue for borders extending
			// it can be skipped
			for ( int i = 0; i < fH; i++ )
			{

				for ( int j = 0; j < fW; j++ )
				{

					int k = i * fW + j;
					int v = currentFrame[k];

					// left pixels
					if ( j > 0 )
					{

						v += currentFrame[k - 1];

						if ( i > 0 )
						{

							v += currentFrame[k - fW - 1];
						
}
						if ( i < fH - 1 )
						{

							v += currentFrame[k + fW - 1];
						
}
					
}
					// right pixels
					if ( j < fW - 1 )
					{

						v += currentFrame[k + 1];

						if ( i > 0 )
						{

							v += currentFrame[k - fW + 1];
						
}
						if ( i < fH - 1 )
						{

							v += currentFrame[k + fW + 1];
						
}
					
}
					// top pixel
					if ( i > 0 )
					{

						v += currentFrame[k - fW];
					
}
					// right pixel
					if ( i < fH - 1 )
					{

						v += currentFrame[k + fW];
					
}

					currentFrameDilatated[k] = (v != 0) ? (byte) 255 : (byte) 0;
				
}
			
}

			// postprocess the input image
			PostprocessInputImage( data, width, height, currentFrameDilatated );

			// unlock the image
			image.UnlockBits( data );
		
}

		// Preprocess input image
		private void PreprocessInputImage( BitmapData data, int width, int height, byte[] buf )
		{

			int stride = data.Stride;
			int offset = stride - width * 3;
			int len = (int)( ( width - 1 ) / 8 ) + 1;
			int rem = ( ( width - 1 ) % 8 ) + 1;
			int[] tmp = new int[len];
			int i, j, t1, t2, k = 0;

			unsafe
			{

				byte * src = (byte *) data.Scan0.ToPointer( );

				for ( int y = 0; y < height; )
				{

					// collect pixels
					Array.Clear( tmp, 0, len );

					// calculate
					for ( i = 0; ( i < 8 ) && ( y < height ); i++, y++ )
					{

						// for each pixel
						for ( int x = 0; x < width; x++, src += 3 )
						{

							// grayscale value using BT709
							tmp[(int) ( x / 8 )] += (int)( 0.2125f * src[RGB.R] + 0.7154f * src[RGB.G] + 0.0721f * src[RGB.B] );
						
}
						src += offset;
					
}

					// get average values
					t1 = i * 8;
					t2 = i * rem;

					for ( j = 0; j < len - 1; j++, k++ )
						buf[k] = (byte)( tmp[j] / t1 );
					buf[k++] = (byte)( tmp[j] / t2 );
				
}
			
}
		
}

		// Postprocess input image
		private void PostprocessInputImage( BitmapData data, int width, int height, byte[] buf )
		{

			int stride = data.Stride;
			int offset = stride - width * 3;
			int len = (int)( ( width - 1 ) / 8 ) + 1;
			int lenWM1 = len - 1;
			int lenHM1 = (int)( ( height - 1 ) / 8);
			int rem = ( ( width - 1 ) % 8 ) + 1;

			int i, j, k;
	
			unsafe
			{

				byte * src = (byte *) data.Scan0.ToPointer( );

				// for each line
				for ( int y = 0; y < height; y++ )
				{

					i = (y / 8);

					// for each pixel
					for ( int x = 0; x < width; x++, src += 3 )
					{

						j = x / 8;	
						k = i * len + j;

						// check if we need to highlight moving object
						if (buf[k] == 255)
						{

							// check for border
							if (
								( ( x % 8 == 0 ) && ( ( j == 0 ) || ( buf[k - 1] == 0 ) ) ) ||
								( ( x % 8 == 7 ) && ( ( j == lenWM1 ) || ( buf[k + 1] == 0 ) ) ) ||
								( ( y % 8 == 0 ) && ( ( i == 0 ) || ( buf[k - len] == 0 ) ) ) ||
								( ( y % 8 == 7 ) && ( ( i == lenHM1 ) || ( buf[k + len] == 0 ) ) )
								)
							{

								src[RGB.R] = 255;
							
}
						
}
					
}
					src += offset;
				
}
			
}
		
}
	
}

}
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.96 kB
01.96 kB
AssemblyInfo.cs2.37 kB2006-04-11 11:57
01.96 kB
01.96 kB
dshow.dll32.00 kB2007-06-13 21:51
dshow.pdb25.50 kB2007-06-13 21:51
01.96 kB
dshow.dll32.00 kB2007-06-07 20:26
01.96 kB
1.txt45.00 B2006-04-11 11:57
IBaseFilter.cs2.14 kB2006-04-11 11:57
IBasicVideo.cs4.70 kB2006-04-11 11:57
ICreateDevEnum.cs592.00 B2006-04-11 11:57
IEnumPins.cs831.00 B2006-04-11 11:57
IFileSourceFilter.cs861.00 B2006-04-11 11:57
IFilterGraph.cs1.58 kB2006-04-11 11:57
IGraphBuilder.cs2.84 kB2006-04-11 11:57
IMediaControl.cs1.49 kB2006-04-11 11:57
IMediaEvent.cs3.06 kB2006-04-11 11:57
IMediaPosition.cs1.88 kB2006-04-11 11:57
IMediaSeeking.cs2.79 kB2006-04-11 11:57
IPin.cs2.36 kB2006-04-11 11:57
IPropertyBag.cs853.00 B2006-04-11 11:57
ISampleGrabber.cs2.21 kB2006-04-11 11:57
IVideoFrameStep.cs747.00 B2006-04-11 11:57
IVideoWindow.cs5.90 kB2006-04-11 11:57
Structures.cs4.37 kB2006-04-11 11:57
Uuids.cs4.87 kB2006-04-11 11:57
Win32.cs919.00 B2006-04-11 11:57
dshow.csproj5.38 kB2007-03-16 20:38
Filter.cs2.57 kB2006-04-11 11:57
FilterCollection.cs2.14 kB2006-04-11 11:57
01.96 kB
01.96 kB
dshow.dll32.00 kB2007-06-13 21:51
dshow.pdb25.50 kB2007-06-13 21:51
01.96 kB
dshow.csproj.FileList.txt216.00 B2007-06-15 15:17
01.96 kB
dshow.dll32.00 kB2007-06-07 20:26
01.96 kB
dshow.dll28.00 kB2007-06-07 18:09
01.96 kB
Tools.cs1.11 kB2006-04-11 11:57
motion.sln1.79 kB2007-03-16 20:38
motion.suo35.00 kB2007-06-15 15:08
01.96 kB
AboutForm.cs11.91 kB2007-06-15 15:17
AboutForm.resx9.77 kB2007-06-15 15:17
App.ico26.07 kB2006-04-11 11:57
AssemblyInfo.cs2.47 kB2007-03-20 08:40
01.96 kB
01.96 kB
AForge.dll20.00 kB2007-03-16 19:54
AForge.Imaging.dll108.00 kB2007-03-19 18:42
AForge.Math.dll28.00 kB2007-03-16 19:54
App.ico26.07 kB2006-04-11 11:57
dshow.dll32.00 kB2007-06-13 21:51
dshow.pdb25.50 kB2007-06-13 21:51
motion.exe132.00 kB2007-06-15 15:17
motion.exe.config147.00 B2007-03-19 09:20
motion.pdb181.50 kB2007-06-15 15:17
motion.vshost.exe5.50 kB2005-09-23 07:56
motion.vshost.exe.config147.00 B2007-03-19 09:20
Tiger.Video.VFW.dll24.00 kB2007-06-13 21:51
Tiger.Video.VFW.pdb27.50 kB2007-06-13 21:51
01.96 kB
AForge.dll20.00 kB2007-03-16 19:54
AForge.Imaging.dll108.00 kB2007-03-19 18:42
AForge.Math.dll28.00 kB2007-03-16 19:54
dshow.dll32.00 kB2007-06-07 20:26
motion.exe124.00 kB2007-06-07 20:40
motion.exe.config147.00 B2007-03-19 09:20
motion.vshost.exe5.50 kB2005-09-23 07:56
motion.vshost.exe.config147.00 B2007-03-19 09:20
Tiger.Video.VFW.dll24.00 kB2007-06-07 20:26
Camera.cs3.47 kB2006-04-11 11:57
CameraWindow.cs5.17 kB2006-04-11 11:57
CameraWindow.resx5.55 kB2006-04-11 11:57
CaptureDeviceForm.cs4.49 kB2007-06-15 15:17
CaptureDeviceForm.resx5.37 kB2006-04-11 11:57
ClassDiagram1.cd1.00 B2007-06-07 18:39
Copyright.txt913.00 B2007-06-15 15:17
IMotionDetector.cs785.00 B2007-06-07 20:36
MainForm.cs21.34 kB2007-06-13 21:54
MainForm.resx45.81 kB2007-06-13 21:51
motion.csproj7.33 kB2007-06-07 18:45
MotionDetector1.cs9.95 kB2007-06-07 20:37
MotionDetector2.cs4.44 kB2007-06-07 20:38
MotionDetector3.cs5.06 kB2007-06-07 20:38
MotionDetector3Optimized.cs6.57 kB2007-06-07 20:38
MotionDetector4.cs4.89 kB2007-06-07 20:38
01.96 kB
01.96 kB
motion.AboutForm.resources3.00 kB2007-06-15 15:17
motion.CameraWindow.resources581.00 B2007-06-13 21:51
motion.CaptureDeviceForm.resources279.00 B2007-06-13 21:51
motion.csproj.GenerateResource.Cache1.00 kB2007-06-15 15:17
motion.exe132.00 kB2007-06-15 15:17
motion.MainForm.resources26.62 kB2007-06-13 21:51
motion.pdb181.50 kB2007-06-15 15:17
motion.Resources.1.gif828.00 B2006-04-11 11:57
motion.Resources.2.gif822.00 B2006-04-11 11:57
motion.Resources.3.gif830.00 B2006-04-11 11:57
motion.Resources.4.gif822.00 B2006-04-11 11:57
motion.Resources.5.gif822.00 B2006-04-11 11:57
motion.Resources.6.gif830.00 B2006-04-11 11:57
motion.Resources.7.gif821.00 B2006-04-11 11:57
motion.Resources.8.gif822.00 B2006-04-11 11:57
motion.Resources.9.gif822.00 B2006-04-11 11:57
motion.URLForm.resources269.00 B2007-06-13 21:51
ResolveAssemblyReference.cache13.42 kB2007-06-15 14:58
01.96 kB
motion.csproj.FileList.txt1.64 kB2007-06-15 15:17
01.96 kB
motion.AboutForm.resources3.00 kB2007-06-07 20:26
motion.CameraWindow.resources581.00 B2007-06-07 20:26
motion.CaptureDeviceForm.resources279.00 B2007-06-07 20:26
motion.csproj.GenerateResource.Cache1.00 kB2007-06-07 20:27
motion.exe124.00 kB2007-06-07 20:40
motion.MainForm.resources26.62 kB2007-06-07 20:27
motion.Resources.1.gif828.00 B2006-04-11 11:57
motion.Resources.2.gif822.00 B2006-04-11 11:57
motion.Resources.3.gif830.00 B2006-04-11 11:57
motion.Resources.4.gif822.00 B2006-04-11 11:57
motion.Resources.5.gif822.00 B2006-04-11 11:57
motion.Resources.6.gif830.00 B2006-04-11 11:57
motion.Resources.7.gif821.00 B2006-04-11 11:57
motion.Resources.8.gif822.00 B2006-04-11 11:57
motion.Resources.9.gif822.00 B2006-04-11 11:57
motion.URLForm.resources269.00 B2007-06-07 20:26
01.96 kB
ResolveAssemblyReference.cache10.44 kB2007-06-07 20:26
01.96 kB
01.96 kB
1.gif828.00 B2006-04-11 11:57
2.gif822.00 B2006-04-11 11:57
3.gif830.00 B2006-04-11 11:57
4.gif822.00 B2006-04-11 11:57
5.gif822.00 B2006-04-11 11:57
6.gif830.00 B2006-04-11 11:57
7.gif821.00 B2006-04-11 11:57
8.gif822.00 B2006-04-11 11:57
9.gif822.00 B2006-04-11 11:57
UnsafeBitmap.cs2.76 kB2007-06-15 14:58
URLForm.cs3.98 kB2006-04-11 11:57
URLForm.resx5.36 kB2006-04-11 11:57
01.96 kB
ByteArrayUtils.cs1.25 kB2006-04-11 11:57
CameraEvents.cs626.00 B2006-04-11 11:57
CaptureDevice.cs8.15 kB2006-04-11 11:57
IVideoSource.cs1.57 kB2006-04-11 11:57
JPEGStream.cs6.89 kB2006-04-11 11:57
MJPEGStream.cs9.12 kB2006-04-11 11:57
VideoFileSource.cs3.45 kB2006-04-11 11:57
VideoStream.cs8.47 kB2006-04-11 11:57
01.96 kB
AForge.dll20.00 kB2007-03-16 19:54
AForge.Imaging.dll108.00 kB2007-03-19 18:42
AForge.Math.dll28.00 kB2007-03-16 19:54
01.96 kB
AssemblyInfo.cs2.37 kB2006-04-11 11:57
AVIReader.cs5.29 kB2006-04-11 11:57
AVIWriter.cs5.59 kB2006-04-11 11:57
01.96 kB
01.96 kB
Tiger.Video.VFW.dll24.00 kB2007-06-13 21:51
Tiger.Video.VFW.pdb27.50 kB2007-06-13 21:51
01.96 kB
Tiger.Video.VFW.dll24.00 kB2007-06-07 20:26
01.96 kB
01.96 kB
01.96 kB
Tiger.Video.VFW.dll24.00 kB2007-06-13 21:51
Tiger.Video.VFW.pdb27.50 kB2007-06-13 21:51
01.96 kB
01.96 kB
Tiger.Video.VFW.dll20.00 kB2007-06-07 18:09
01.96 kB
Tiger.Video.VFW.dll24.00 kB2007-06-07 20:26
vfw.csproj.FileList.txt276.00 B2007-06-15 15:17
vfw.csproj3.55 kB2007-03-16 20:38
Win32.cs9.81 kB2006-04-11 11:57
...
Sponsored links

MotionDetector3Optimized.cs (805.82 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D