0

Good morning,

So i was finally able to solve all the errors in my game, but now i can't seem to get a Motion Detector to work using Aforge, so far i've got:

private void device_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
        {
            tmpImage = eventArgs.Frame.Clone(Rec, PixelFormat.Format24bppRgb);
            Bitmap image = (Bitmap)tmpImage.Clone();
            //BitmapData bitmapData = image.LockBits( new System.Drawing.Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, image.PixelFormat);

            //BlobCounter blobCounter = new BlobCounter();

            //blobCounter.FilterBlobs = true;
            //blobCounter.MinHeight = 5;
            //blobCounter.MinWidth = 5;

            ////blobCounter.ProcessImage(bitmapData);
            ////Blob[] blobs = blobCounter.GetObjectsInformation();

            //image.UnlockBits(bitmapData);
            //Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
            Threshold filter2 = new Threshold(200);
            Invert invert = new Invert();

            Bitmap grayImage = Grayscale.CommonAlgorithms.BT709.Apply(image);
            invert.ApplyInPlace(grayImage);
            filter2.ApplyInPlace(grayImage);

            Graphics g = Graphics.FromImage(tmpImage);
            blobCounter.MinHeight = 40;
            blobCounter.MinWidth = 40;
            blobCounter.ObjectsOrder = ObjectsOrder.Area;
            blobCounter.ProcessImage(grayImage);
            System.Drawing.Rectangle[] rects = blobCounter.GetObjectsRectangles();
            Pen pen = new Pen(System.Drawing.Color.Red, 1);
            using (pen)
            {
                foreach (System.Drawing.Rectangle rc in rects)
                {
                    g.DrawRectangle(pen, rects[0].X, rects[0].Y, rects[0].Width, rects[0].Height);
                    g.DrawRectangle(pen, rects[1].X, rects[1].Y, rects[1].Width, rects[1].Height);
                }
            }
            g.Dispose();
        }     

I've tried following several tutorials, but all of them are for C# (I'm using XNA), i tried to apply them, but they didn't seem to work, can anyone help me out?

Thanks in advance,
Daniel.

EDIT: Forgot to mention, so far it's detecting the 2 largest black blobs.

Edited by Cronicle8: Forgetful

1
Contributor
1
Reply
3
Views
5 Years
Discussion Span
Last Post by Cronicle8
0

Motion Detector online:

private void device_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
        {
            tmpImage = eventArgs.Frame.Clone(Rec, PixelFormat.Format24bppRgb);
            Bitmap image = (Bitmap)tmpImage.Clone();

            SetMotionProcessingAlgorithm(new BlobCountingObjectsProcessing());
            BlobCountingObjectsProcessing countingDetector = (BlobCountingObjectsProcessing)detector.MotionProcessingAlgorithm;
            try
            {
                detector.ProcessFrame(tmpImage);
            }
            catch
            {
            }
            countingDetector.HighlightMotionRegions = false;
            countingDetector.MinObjectsWidth = 50;
            countingDetector.MinObjectsHeight = 80;
            countingDetector.HighlightColor = System.Drawing.Color.Blue;
            System.Drawing.Rectangle[] rects = countingDetector.ObjectRectangles;
            Pen pen = new Pen(System.Drawing.Color.Red, 1);
            foreach (System.Drawing.Rectangle recs in rects)
                if (rects.Length > 0)
                {
                    Graphics g = Graphics.FromImage(image);
                    if (recs.X > 250)
                    {
                        posx += 1;
                    }
                    if (recs.X < 100)
                    {
                        posx -= 1;
                    }
                    if (recs.Y < 50)
                    {
                        se = SpriteEffects.FlipVertically;
                    }
                    g.Dispose();
                }
        }
        private void SetMotionDetectionAlgorithm(IMotionDetector detectionAlgorithm)
        {
            lock (this)
            {
                detector.MotionDetectionAlgorithm = detectionAlgorithm;

                if (detectionAlgorithm is TwoFramesDifferenceDetector)
                {
                    if (
                        (detector.MotionProcessingAlgorithm is MotionBorderHighlighting) ||
                        (detector.MotionProcessingAlgorithm is BlobCountingObjectsProcessing))
                    {
                        SetMotionProcessingAlgorithm(new MotionAreaHighlighting());
                    }
                }
            }
        }
        private void SetMotionProcessingAlgorithm(IMotionProcessing processingAlgorithm)
        {
            lock (this)
            {
                detector.MotionProcessingAlgorithm = processingAlgorithm;
            }
        }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.