请帮帮我,我不明白这个来源。 [英] Please Help Me, I Don't Understand This Source.

查看:65
本文介绍了请帮帮我,我不明白这个来源。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我看到这段代码时,我无法理解这段代码。

i想知道你用二进制或灰度来表示这个项目。

你能解释一下吗?这来源??我真的想知道如何运行这个项目。

when i saw this code, i can't understand this code.
i want to know that you used binary or gray scale to this project.
can you explain this source?? i really want to know how run this project.

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

namespace uCamAlarm
{
    public class clsMotionDetector : 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; }
		}

		// Motion level - amount of changes in percents
		public double MotionLevel
		{
			get { return (double) pixelsChanged / ( width * height ); }
		}

		// Constructor
		public clsMotionDetector( )
		{
		}

		// 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[2] + 0.7154f * src[1] + 0.0721f * src[0] );
						}
						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[0] = 255;//****this is where it hilights
							}
						}
					}
					src += offset;
				}
			}
		}
    }
}

推荐答案

I我只是猜测你从一篇文章中得到了这个,并假设有一个人写了所有文章......甚至没有接近真相。



不发表这在快速答案下 - 如果您从文章中获得了代码,那么该文章底部会出现一个新消息按钮,这会导致将电子邮件发送给作者。然后他们会被告知你想和他们说话。

在这里发布这个依赖于他们匆匆而过并意识到这是为了他们。
I'm just guessing that you got this from an article, and assume that one person wrote them all...not even close to the truth.

Don't post this under Quick Answers - if you got the code from an article, then there is a "new message" button at the bottom of that article, which causes an email to be sent to the author. They are then alerted that you wish to speak to them.
Posting this here relies on them "dropping by" and realising it is for them.


这是不是一个真正的问题,它不能有任何快速答案。请联系此代码的作者。
This is not really a question and it can't have any "quick answers". Contact the author of this code.


这篇关于请帮帮我,我不明白这个来源。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆