Beispiel #1
0
        public static void ExtractRegionImages1(Bitmap frame1, Bitmap frame2)
        {
            frame1 = greyscaleFilter1.Apply(frame1);
            frame2 = greyscaleFilter1.Apply(frame2);
            lock (_lock1)
            {
                cropFilter1.Rectangle = Region1;
                ROI1 = circleFilter2.Apply(resizeFilter1.Apply(cropFilter1.Apply(frame1)));
                diffFilter1.OverlayImage = circleFilter2.Apply(resizeFilter1.Apply(cropFilter1.Apply(frame2)));
                ROI1Diff = diffFilter1.Apply(ROI1);
                subFilter1.OverlayImage = invertFilter1.Apply(threshFilter1.Apply(ROI1Diff));
                ROI1DropDiff            = subFilter1.Apply(ROI1Diff);

                cropFilter1.Rectangle = Region2;
                ROI2 = circleFilter2.Apply(resizeFilter1.Apply(cropFilter1.Apply(frame1)));
                diffFilter1.OverlayImage = circleFilter2.Apply(resizeFilter1.Apply(cropFilter1.Apply(frame2)));
                ROI2Diff = diffFilter1.Apply(ROI2);
                subFilter1.OverlayImage = invertFilter1.Apply(threshFilter1.Apply(ROI2Diff));
                ROI2DropDiff            = subFilter1.Apply(ROI2Diff);

                cropFilter1.Rectangle = Region5;
                ROI5 = circleFilter2.Apply(resizeFilter1.Apply(cropFilter1.Apply(frame1)));
                diffFilter1.OverlayImage = circleFilter2.Apply(resizeFilter1.Apply(cropFilter1.Apply(frame2)));
                ROI5Diff = diffFilter1.Apply(ROI5);
                subFilter1.OverlayImage = invertFilter1.Apply(threshFilter1.Apply(ROI5Diff));
                ROI5DropDiff            = subFilter1.Apply(ROI5Diff);

                cropFilter1.Rectangle = Region6;
                ROI6 = circleFilter2.Apply(resizeFilter1.Apply(cropFilter1.Apply(frame1)));
                diffFilter1.OverlayImage = circleFilter2.Apply(resizeFilter1.Apply(cropFilter1.Apply(frame2)));
                ROI6Diff = diffFilter1.Apply(ROI6);
                subFilter1.OverlayImage = invertFilter1.Apply(threshFilter1.Apply(ROI6Diff));
                ROI6DropDiff            = subFilter1.Apply(ROI6Diff);
            }
        }
Beispiel #2
0
        public static void ExtractRegionsDevice2(Bitmap frame1, Bitmap frame2)
        {
            frame1 = greyscaleFilter2.Apply(frame1);
            frame2 = greyscaleFilter2.Apply(frame2);
            lock (_lock2)
            {
                cropFilter2.Rectangle = Region3;
                ROI3 = circleFilter2.Apply(resizeFilter2.Apply(cropFilter2.Apply(frame1)));
                diffFilter2.OverlayImage = circleFilter2.Apply(resizeFilter2.Apply(cropFilter2.Apply(frame2)));
                ROI3Diff = diffFilter2.Apply(ROI3);
                subFilter2.OverlayImage = invertFilter2.Apply(threshFilter2.Apply(ROI3Diff));
                ROI3DropDiff            = subFilter2.Apply(ROI3Diff);

                cropFilter2.Rectangle = Region4;
                ROI4 = circleFilter2.Apply(resizeFilter2.Apply(cropFilter2.Apply(frame1)));
                diffFilter2.OverlayImage = circleFilter2.Apply(resizeFilter2.Apply(cropFilter2.Apply(frame2)));
                ROI4Diff = diffFilter2.Apply(ROI4);
                subFilter2.OverlayImage = invertFilter2.Apply(threshFilter2.Apply(ROI4Diff));
                ROI4DropDiff            = subFilter2.Apply(ROI4Diff);

                cropFilter2.Rectangle = Region7;
                ROI7 = circleFilter2.Apply(resizeFilter2.Apply(cropFilter2.Apply(frame1)));
                diffFilter2.OverlayImage = circleFilter2.Apply(resizeFilter2.Apply(cropFilter2.Apply(frame2)));
                ROI7Diff = diffFilter2.Apply(ROI7);
                subFilter2.OverlayImage = invertFilter2.Apply(threshFilter2.Apply(ROI7Diff));
                ROI7DropDiff            = subFilter2.Apply(ROI7Diff);

                cropFilter2.Rectangle = Region8;
                ROI8 = circleFilter2.Apply(resizeFilter2.Apply(cropFilter2.Apply(frame1)));
                diffFilter2.OverlayImage = circleFilter2.Apply(resizeFilter2.Apply(cropFilter2.Apply(frame2)));
                ROI8Diff = diffFilter2.Apply(ROI8);
                subFilter2.OverlayImage = invertFilter2.Apply(threshFilter2.Apply(ROI8Diff));
                ROI8DropDiff            = subFilter2.Apply(ROI8Diff);
            }
        }
Beispiel #3
0
        private Bitmap XXX(Bitmap bmpBefore, Bitmap bmpAfter)
        {
            var filter = new Grayscale(0.2125, 0.7154, 0.0721);

            bmpBefore = filter.Apply(bmpBefore);
            bmpAfter  = filter.Apply(bmpAfter);

            // create filters
            var     differenceFilter = new Difference();
            IFilter thresholdFilter  = new Threshold(15);

            // set backgroud frame as an overlay for difference filter
            differenceFilter.OverlayImage = bmpBefore;
            // apply the filters
            Bitmap  tmp1          = differenceFilter.Apply(bmpAfter);
            Bitmap  tmp2          = thresholdFilter.Apply(tmp1);
            IFilter erosionFilter = new Erosion();
            // apply the filter
            Bitmap tmp3 = erosionFilter.Apply(tmp2);

            IFilter pixellateFilter = new Pixellate();
            // apply the filter
            Bitmap tmp4 = pixellateFilter.Apply(tmp3);

            return(tmp4);
        }
Beispiel #4
0
        private void ProcessFrame()
        {
            //Check for preprocess filter & filter to grey
            if (preFilter != null)
            {
                frame_pre = preFilter.Apply(frame_c);
                frame_gs  = gsFilter.Apply(frame_pre);
            }
            else
            {
                frame_gs = gsFilter.Apply(frame_c);
            }

            //Initialise Background
            if (back == null)
            {
                back = (Bitmap)frame_gs.Clone();
            }

            //Process frame for motion
            diffFilter.OverlayImage = back;
            frame_processed         = diffFilter.Apply(frame_gs);
            frame_processed         = motionFilter.Apply(frame_processed);

            CheckMotion();

            //Update Background
            morphFilter.OverlayImage = frame_gs;
            back = morphFilter.Apply(back);
        }
Beispiel #5
0
        /// <summary>
        /// 处理(当前)画面
        /// </summary>
        /// <param name="image"></param>
        public void ProcessFrame(ref Bitmap image)
        {
            if (backgroundFrame == null)
            {
                backgroundFrame = GrayscaleFilter.Apply(image); // create initial backgroung image

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

                // just return for the first time
                return;
            }

            Bitmap tmpImage = GrayscaleFilter.Apply(image); //灰度过滤

            // set backgroud frame as an overlay for difference filter
            DifferenceFilter.OverlayImage = backgroundFrame;
            Bitmap tmpImage2 = DifferenceFilter.Apply(tmpImage);    //差异过滤

            // lock the temporary image and apply some filters on the locked data
            bitmapData = tmpImage2.LockBits(new Rectangle(0, 0, width, height),
                                            ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            // threshold filter
            ThresholdFilter.ApplyInPlace(bitmapData);           //阈值过滤
            // erosion filter
            Bitmap tmpImage3 = ErosionFilter.Apply(bitmapData); //腐蚀过滤

            // unlock temporary image
            tmpImage2.UnlockBits(bitmapData);
            tmpImage2.Dispose();

            // calculate amount of changed pixels
            pixelsChanged = (calculateMotionLevel) ? CalculateWhitePixels(tmpImage3) : 0;

            // dispose old background
            backgroundFrame.Dispose();
            // set backgound to current
            backgroundFrame = tmpImage;

            // extract red channel from the original image
            Bitmap redChannel = extrachChannel.Apply(image);    //提取通道

            //  merge red channel with moving object
            MergeFilter.OverlayImage = tmpImage3;
            Bitmap tmpImage4 = MergeFilter.Apply(redChannel);   //合并过滤

            redChannel.Dispose();
            tmpImage3.Dispose();

            // replace red channel in the original image
            replaceChannel.ChannelImage = tmpImage4;
            Bitmap tmpImage5 = replaceChannel.Apply(image); //替换通道

            tmpImage4.Dispose();

            image.Dispose();
            image = tmpImage5;
        }
Beispiel #6
0
        public Bitmap Difference(Bitmap leftOperand, Bitmap rightOperand)
        {
            ResizeBilinear resize = new ResizeBilinear(leftOperand.Width, leftOperand.Height);

            rightOperand = resize.Apply(rightOperand);
            Difference filter = new Difference(rightOperand);

            return(filter.Apply(leftOperand));
        }
Beispiel #7
0
        private void imageSubstractToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // filtre oluşturduk
            Difference filter = new Difference((Bitmap)pictureBox6.Image);
            // uyguladık
            Bitmap cikarim = filter.Apply((Bitmap)pictureBox3.Image);

            pictureBox7.Image = cikarim;
        }
Beispiel #8
0
        public mCompositeDifference(Bitmap UnderlayBitmap, Bitmap OverlayBitmap)
        {
            BitmapUnder = new mSetFormat(UnderlayBitmap, mFilter.BitmapTypes.Rgb24bpp).ModifiedBitmap;
            BitmapOver  = new mSetFormat(OverlayBitmap, mFilter.BitmapTypes.Rgb24bpp).ModifiedBitmap;

            ModifiedBitmap = BitmapUnder;

            Effect = new Difference(BitmapOver);

            ModifiedBitmap = Effect.Apply(BitmapUnder);
        }
Beispiel #9
0
        private Bitmap ProcessCurrentFrame()
        {
            Bitmap bmp;

            //curr_gs = pixelFilter.Apply(curr_gs);

            if (first)
            {
                back  = (Bitmap)curr_gs.Clone();
                first = false;
            }

            /* Detection */

            diffFilter.OverlayImage = back;
            bmp = diffFilter.Apply(curr_gs);
            bmp = thresholdFilter.Apply(bmp);
            bmp = blobFilter.Apply(bmp);
            //bmp = erosionFilter.Apply(bmp);
            //bmp = openFilter.Apply(bmp);
            //bmp = edgeFilter.Apply(bmp);

            try
            {
                //blobCount.Text = blobGrabber.Apply(bmp).Size.ToString();
            }
            catch (Exception)
            {
            }


            /* Transcribe to original image */

            // extract red channel from the original image
            //IFilter extrachChannel = new ExtractChannel(RGB.R);
            //Bitmap redChannel = extrachChannel.Apply(curr_c);
            //  merge red channel with motion regions
            //Merge mergeFilter = new Merge();
            //mergeFilter.OverlayImage = bmp;
            //Bitmap tmp4 = mergeFilter.Apply(redChannel);
            // replace red channel in the original image
            //ReplaceChannel replaceChannel = new ReplaceChannel(RGB.R,tmp4);
            //curr_c = replaceChannel.Apply(curr_c);

            /* Background Update */
            //towardsFilter.OverlayImage = curr_gs;
            //back = towardsFilter.Apply(back);
            morphFilter.OverlayImage = curr_gs;
            back = morphFilter.Apply(back);


            return(bmp);
        }
Beispiel #10
0
        private Bitmap getTwoFrameDifference(Bitmap frame)
        {
            if (lastFrame == null)
            {
                return(null);
            }

            Difference filter           = new Difference(lastFrame);
            Bitmap     differenceBitmap = filter.Apply(frame);

            return(differenceBitmap);
        }
        private Bitmap ProcessImageDiference(Bitmap lastimg, Bitmap openingFilterImg)
        {
            // create filter
            Difference filter = new Difference(openingFilterImg);
            // apply the filter
            Bitmap resultImage = filter.Apply(lastimg);
            // create filter
            Invert filter2 = new Invert();

            // apply the filter
            filter2.ApplyInPlace(resultImage);
            return(resultImage);
        }
Beispiel #12
0
        int MotionDetection(Bitmap CurrentFrame)
        {
            if (PreFrame == null)
            {
                PreFrame = GrayFilter.Apply(CurrentFrame);
                PixFilter.ApplyInPlace(PreFrame);
                return(0);
            }

            Bitmap ProcFrame = GrayFilter.Apply(CurrentFrame);

            PixFilter.ApplyInPlace(ProcFrame);

            MoveTowardsFilter.StepSize     = 1;
            MoveTowardsFilter.OverlayImage = ProcFrame;
            MoveTowardsFilter.ApplyInPlace(PreFrame);

            DiffFilter.OverlayImage = PreFrame;
            Bitmap DiffImage = DiffFilter.Apply(ProcFrame);

            ThreFilter.ApplyInPlace(DiffImage);

            BlobsFilter.ProcessImage(DiffImage);
            Blob[] Blobs = BlobsFilter.GetObjectsInformation();

            int ret = 0;

            if (Blobs.Length != 0)
            {
                Graphics graph = Graphics.FromImage(CurrentFrame);
                Pen      pen   = new Pen(Color.Yellow, 2);
                graph.DrawRectangle(pen, new Rectangle(3, 3, 10, 10));
                foreach (Blob item in Blobs)
                {
                    if (item.Area > blobArea)
                    {
                        if (ShowMotionCheckBox.Checked)
                        {
                            graph.DrawRectangle(pen, item.Rectangle);
                        }
                        ++ret;
                    }
                }
            }

            videoShowPicBox.Image = CurrentFrame;

            PreFrame = ProcFrame;

            return(ret);
        }
Beispiel #13
0
        public static Bitmap PixelDiff(Bitmap a, Bitmap b)
        {
            var difference = new Difference(a);
            var dif        = difference.Apply(b);

            gaussianBlur.ApplyInPlace(dif);

            Bitmap clone = Grayscale.CommonAlgorithms.Y.Apply(dif);

            threshold.ApplyInPlace(clone);
            fillHoles.ApplyInPlace(clone);

            return(clone);
        }
Beispiel #14
0
        static void Main(string[] args)
        {
            try
            {
                string     path             = "D:\\HACK2015\\PICS";
                Bitmap     sourceImage      = AForge.Imaging.Image.FromFile(Path.Combine(path, "CAM1s.jpg"));
                Difference differenceFilter = new Difference(); //AForge.Imaging.Filters.Difference
                differenceFilter.OverlayImage = sourceImage;

                Bitmap sourceImg = AForge.Imaging.Image.FromFile(Path.Combine(path, "CAM1.jpg"));
                Bitmap tempImg   = sourceImg.Clone() as Bitmap;
                tempImg = differenceFilter.Apply(tempImg);
                FiltersSequence seq = new FiltersSequence();
                seq.Add(Grayscale.CommonAlgorithms.BT709);
                seq.Add(new OtsuThreshold());
                tempImg = seq.Apply(tempImg);
                tempImg.Save(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "CAM1.jpg"));

                int objectCount = 0;

                BlobCounter blobCounter = new BlobCounter();
                blobCounter.FilterBlobs = true;
                blobCounter.MinHeight   = 60;
                blobCounter.MinWidth    = 40;
                blobCounter.ProcessImage(tempImg);
                Blob[] blobs = blobCounter.GetObjectsInformation();
                for (int i = 0; i < blobs.Length; i++)
                {
                    List <IntPoint>    edgePoints   = blobCounter.GetBlobsEdgePoints(blobs[i]);
                    List <IntPoint>    corners      = null;
                    SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
                    if (shapeChecker.IsQuadrilateral(edgePoints, out corners))
                    {
                        ++objectCount;
                    }
                }

                Console.WriteLine("No. of BLOBS: " + blobCounter.GetObjectsInformation().Length);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.Read();
        }
Beispiel #15
0
        private Bitmap getThreeFrameDifference(Bitmap frame)
        {
            if (lastFrame == null || lastButOneFrame == null)
            {
                return(null);
            }

            Difference filterPrevious           = new Difference(lastButOneFrame);
            Bitmap     differenceBitmapPrevious = filterPrevious.Apply(lastFrame);

            Difference filterNext           = new Difference(lastFrame);
            Bitmap     differenceBitmapNext = filterNext.Apply(frame);

            Intersect intersectionFilter         = new Intersect(differenceBitmapPrevious);
            Bitmap    threeFrameDifferenceBitmap = intersectionFilter.Apply(differenceBitmapNext);

            return(threeFrameDifferenceBitmap);
        }
Beispiel #16
0
        private Bitmap getChangingBackgroundDifference(Bitmap frame)
        {
            if (background == null || lastFrame == null)
            {
                background = frame;
                return(null);
            }

            if (background != lastFrame)
            {
                Morph filter = new Morph(background);
                filter.SourcePercent = 0.5;
                background           = filter.Apply(lastFrame);
            }

            Difference differenceFilter = new Difference(background);
            Bitmap     differenceBitmap = differenceFilter.Apply(frame);

            return(differenceBitmap);
        }
Beispiel #17
0
        public static Bitmap operationImages(Bitmap img1, Bitmap img2, int op)
        {
            img1 = ImageUtil.convert(img1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            img2 = ImageUtil.convert(img2, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            Bitmap  filteredImage = img1;
            IFilter myFilter;

            if (op == Filters.mode["Add"])
            {
                myFilter      = new Add(img2);
                filteredImage = myFilter.Apply(img1);
            }
            else if (op == Filters.mode["Subtract"])
            {
                myFilter      = new Subtract(img2);
                filteredImage = myFilter.Apply(img1);
            }
            else if (op == Filters.mode["Intersect"])
            {
                myFilter      = new Intersect(img2);
                filteredImage = myFilter.Apply(img1);
            }
            else if (op == Filters.mode["Difference"])
            {
                myFilter      = new Difference(img2);
                filteredImage = myFilter.Apply(img1);
            }
            else if (op == Filters.mode["Merge"])
            {
                myFilter      = new Merge(img2);
                filteredImage = myFilter.Apply(img1);
            }
            else if (op == Filters.mode["Multiply"])
            {
                myFilter      = new Multiply(img2);
                filteredImage = myFilter.Apply(img1);
            }
            return(filteredImage);
        }
        private async Task CalibThread(FrameReadyEventArgs e)
        {
            try
            {
                if (_errors > MaxErrorCount)
                {
                    _cc.FrameReady -= BaseCalibration;
                    _vs.Close();
                    Console.WriteLine("Calibration impossible");
                }
                if (_calibrationStep == CalibrationFrames)
                {
                    _cc.FrameReady -= BaseCalibration;
                    _vs.Close();
                    Console.WriteLine("Calibration complete");
                    CalibrationCompleted(this, new EventArgs());
                }
                else
                {
                    switch (_calibrationStep)
                    {
                    // get the corners from the difference image
                    case 2:
                        var bm = UnmanagedImage.FromManagedImage(e.Frame.Bitmap);
                        bm = _diffFilter.Apply(bm);
                        var gf = new GaussianBlur(9.0, 3);
                        gf.ApplyInPlace(bm);
                        var cf = new ColorFiltering(new IntRange(10, 255), new IntRange(20, 255),
                                                    new IntRange(20, 255));
                        cf.ApplyInPlace(bm);
                        var blobCounter = new BlobCounter
                        {
                            ObjectsOrder        = ObjectsOrder.Size,
                            BackgroundThreshold = Color.FromArgb(255, 15, 20, 20),
                            FilterBlobs         = true
                        };
                        blobCounter.ProcessImage(bm);
                        var blobs = blobCounter.GetObjectsInformation();
                        if (blobs.Any())
                        {
                            int             i = 0;
                            List <IntPoint> corners;
                            do
                            {
                                corners =
                                    PointsCloud.FindQuadrilateralCorners(blobCounter.GetBlobsEdgePoints(blobs[i++]));
                            } while (corners.Count != 4);
                            InPlaceSort(corners);
                            Grid.TopLeft     = new Point(corners[0].X, corners[0].Y);
                            Grid.TopRight    = new Point(corners[1].X, corners[1].Y);
                            Grid.BottomLeft  = new Point(corners[2].X, corners[2].Y);
                            Grid.BottomRight = new Point(corners[3].X, corners[3].Y);
                            if (Grid.TopLeft.X > 10 && Grid.TopRight.X < _vs.Width - 5 && Grid.BottomLeft.X > 5 &&
                                Grid.BottomRight.X < _vs.Width - 5 && Grid.TopLeft.Y > 10 && Grid.TopRight.Y > 5 &&
                                Grid.BottomLeft.Y < _vs.Height - 5 && Grid.BottomRight.Y < _vs.Height - 5 &&
                                Grid.TopLeft.X < Grid.BottomRight.X &&     //blobs[i - 1].Area > 60000 &&
                                Grid.BottomLeft.X < Grid.TopRight.X && Grid.BottomLeft.X < Grid.BottomRight.X &&
                                Grid.TopLeft.Y < Grid.BottomLeft.Y && Grid.TopLeft.Y < Grid.BottomRight.Y &&
                                Grid.TopRight.Y < Grid.BottomLeft.Y && Grid.TopRight.Y < Grid.BottomRight.Y)
                            {
                                _calibrationStep++;
                                _vs.Clear();
                                Grid.AddPoint(new Point(), new Point(corners[0].X, corners[0].Y));
                                Grid.AddPoint(new Point(0, _vs.Height), new Point(corners[1].X, corners[1].Y));
                                Grid.AddPoint(new Point(_vs.Width, 0), new Point(corners[2].X, corners[2].Y));
                                Grid.AddPoint(new Point(_vs.Width, _vs.Height),
                                              new Point(corners[3].X, corners[3].Y));
                            }
                            else
                            {
                                _calibrationStep = 0;
                                _errors++;
                            }
                        }
                        else
                        {
                            _calibrationStep = 0;
                            _errors++;
                            _vs.Draw();
                        }
                        break;

                    case 1:     // draw second image, store the first
                        _diffFilter.OverlayImage = e.Frame.Bitmap;
                        _vs.Clear();
                        for (int y = 0; y < Columncount; y++)
                        {
                            for (int x = 0; x < Rowcount; x++)
                            {
                                if (!(y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1))
                                {
                                    _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight),
                                                (int)_sqrwidth, (int)_sqrheight,
                                                Color.FromArgb(255, 255, 255, 255));
                                }
                            }
                        }
                        _vs.Draw();
                        _calibrationStep++;
                        break;

                    //draw the first image
                    case 0:
                        Grid = new Grid(e.Frame.Bitmap.Width, e.Frame.Bitmap.Height)
                        {
                            ScreenSize = new Rectangle(0, 0, _vs.Width, _vs.Height)
                        };
                        var thread = new Thread(() =>
                        {
                            _vs.Show();
                            _vs.AddRect(0, 0, _vs.Width, _vs.Height, Color.FromArgb(255, 0, 0, 0));
                        });
                        thread.SetApartmentState(ApartmentState.STA);
                        thread.Start();
                        thread.Join();
                        for (int y = 0; y < Columncount; y++)
                        {
                            for (int x = 0; x < Rowcount; x++)
                            {
                                if (y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1)
                                {
                                    _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight),
                                                (int)_sqrwidth, (int)_sqrheight,
                                                Color.FromArgb(255, 255, 255, 255));
                                }
                            }
                        }
                        _vs.Draw();
                        _calibrationStep++;
                        break;
                    }
                }
                await Task.Delay(MillisecondsDelay);
            }
            finally
            {
                _sem.Release();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            disp1.SizeMode = PictureBoxSizeMode.StretchImage;
            disp2.SizeMode = PictureBoxSizeMode.StretchImage;
            disp3.SizeMode = PictureBoxSizeMode.StretchImage;
            System.Drawing.Image refimg  = System.Drawing.Image.FromFile("C:\\Users\\MohammadAlCheikhSale\\Desktop\\Final Hackathon\\RefImg.jpg");
            System.Drawing.Image caseimg = System.Drawing.Image.FromFile("C:\\Users\\MohammadAlCheikhSale\\Desktop\\Final Hackathon\\CaseImg.jpg");
            // create filter
            Difference filter = new Difference((Bitmap)refimg);
            // apply the filter
            Bitmap resultImage = filter.Apply((Bitmap)caseimg);

            disp1.Image = refimg;
            //ResultImage.Image = resultImage;
            disp2.Image = caseimg;
            show_result(resultImage);
            System.Threading.Thread.Sleep(5000);

            Grayscale gsfilter = new Grayscale(0.2125, 0.7154, 0.0721);
            Bitmap    gsImage  = gsfilter.Apply((Bitmap)caseimg);

            //ResultImage.Image = gsImage;

            System.Threading.Thread.Sleep(5000);

            Threshold bnFilter = new Threshold(100);
            Bitmap    bnImage  = bnFilter.Apply((Bitmap)gsImage);

            disp3.Image = bnImage;

            System.Threading.Thread.Sleep(5000);

            MaskedFilter mask     = new MaskedFilter(new Sepia(), bnImage);
            Bitmap       MasImage = mask.Apply((Bitmap)caseimg);

            disp3.Image = MasImage;

            /*HSLFiltering filter = new HSLFiltering(
             *  new IntRange(330, 30),    // hue range
             *  new Range(0, 1),    // saturation range
             *  new Range(0, 1));    // luminance range
             *
             * filter.UpdateLuminance = false;
             * filter.UpdateHue = false;
             * // apply the filter
             * filter.ApplyInPlace((Bitmap)proimg);*/

            /*Median filter = new Median();
             * // apply the filter
             * filter.ApplyInPlace((Bitmap)proimg);*/

            /*/ create grayscale filter (BT709)
             * Grayscale filter1 = new Grayscale(0.2125, 0.7154, 0.0721);
             * // apply the filter
             * Bitmap grayImage = filter1.Apply((Bitmap)proimg);
             *
             * // create filter
             * BlobsFiltering filter = new BlobsFiltering();
             * // configure filter
             * filter.CoupledSizeFiltering = true;
             * filter.MinWidth = 70;
             * filter.MinHeight = 70;
             * // apply the filter
             * filter.ApplyInPlace(grayImage);
             *
             * // create filter
             * Difference filter2 = new Difference(overlayImage);
             * // apply the filter
             * Bitmap resultImage = filter2.Apply(sourceImage);
             * procImage.Image = grayImage;*/



            /* BlobCounter bc = new BlobCounter();
             * bc.ProcessImage((Bitmap)resultImage);
             * Blob[] blobs = bc.GetObjectsInformation();
             */
        }
        private async Task CalibThread(FrameReadyEventArgs e)
        {
            Debug.WriteLine("Calibrating " + _calibrationStep);
            e.Frame.Bitmap.Save(@"C:\temp\aforge\src\img" + _calibrationStep + ".jpg");
            //var stats = new ImageStatistics(e.Frame.Bitmap);
            //        //var histogram = stats.Gray;
            ////Debug.WriteLine("Grey: Min: " + histogram.Min + " Max: " + histogram.Max + " Mean: " + histogram.Mean +
            ////    " Dev: " + histogram.StdDev + " Median: " + histogram.Median);
            //var histogram = stats.Green;
            //Debug.WriteLine("Green: Min: " + histogram.Min + " Max: " + histogram.Max + " Mean: " + histogram.Mean +
            //    " Dev: " + histogram.StdDev + " Median: " + histogram.Median);
            //histogram = stats.Blue;
            //Debug.WriteLine("Blue: Min: " + histogram.Min + " Max: " + histogram.Max + " Mean: " + histogram.Mean +
            //    " Dev: " + histogram.StdDev + " Median: " + histogram.Median);
            //histogram = stats.Red;
            //Debug.WriteLine("Red: Min: " + histogram.Min + " Max: " + histogram.Max + " Mean: " + histogram.Mean +
            //e.Frame.Bitmap.Save(@"C:\temp\aforge\src\img" + _calibrationStep + ".jpg");
            //    " Dev: " + histogram.StdDev + " Median: " + histogram.Median);
            //e.NewImage.Save(@"C:\temp\aforge\src\img" + _calibrationStep + ".jpg");
            if (_errors > 100)
            {
                //calibration not possible
                return;
            }
            if (_calibrationStep == 3)
            {
                _cc.FrameReady -= BaseCalibration; // TODO
                //Grid.Calculate();
                _vs.Close();
                CalibrationCompleted(this, new EventArgs());
            }
            else
            {
                //var diffBitmap = new Bitmap(1,1);
                //if(diffFilter.OverlayImage != null)
                //    diffBitmap = diffFilter.Apply(e.NewImage);
                //diffBitmap.Save(@"C:\temp\aforge\diff\img" + _calibrationStep + ".jpg");
                if (_calibrationStep > 2)
                {
                    _vs.Clear();
                    FillRects();
                    //var gf = new ColorFiltering(new IntRange(0, 255), //(int) stats.Red.Mean),
                    //                            new IntRange((int) (stats.Green.Mean + ColorDiff), 255),
                    //                            new IntRange(0, 255));//(int) stats.Blue.Mean));
                    //var bf = new ColorFiltering(new IntRange(0, 255),//(int)stats.Red.Mean),
                    //    new IntRange(0, 255),//(int) stats.Green.Mean),
                    //    new IntRange((int) stats.Blue.Mean + ColorDiff, 255));
                    // create color Channel images
                    var gbm = PartiallyApplyAvgFilter(e.Frame.Bitmap, Channels.Green, 8, 8, 8);
                    gbm.Save(@"C:\temp\aforge\gimg\img" + _calibrationStep + ".jpg");
                    var bbm = PartiallyApplyAvgFilter(e.Frame.Bitmap, Channels.Blue, 8, 8, 8);
                    bbm.Save(@"C:\temp\aforge\bimg\img" + _calibrationStep + ".jpg");
                    var gblobCounter = new BlobCounter {
                        ObjectsOrder = ObjectsOrder.YX,
                        MaxHeight    = 30, MinHeight = 15, MaxWidth = 30, MinWidth = 15, FilterBlobs = true, CoupledSizeFiltering = false
                    };
                    gblobCounter.ProcessImage(gbm);
                    var bblobCounter = new BlobCounter {
                        ObjectsOrder = ObjectsOrder.YX,
                        MaxHeight    = 30, MinHeight = 15, MaxWidth = 30, MinWidth = 15, FilterBlobs = true, CoupledSizeFiltering = false
                    };
                    bblobCounter.ProcessImage(bbm);
                    ProcessBlobs(gblobCounter, 0);
                    ProcessBlobs(bblobCounter, 1);
                    _calibrationStep++;
                }
                else
                {
                    switch (_calibrationStep)
                    {
                    case 2:     // identify screen bounds
                        //var thresholdFilter = new Threshold(50);
                        //thresholdFilter.ApplyInPlace(diffBitmap);
                        //var cf = new ColorFiltering(new IntRange(0, 255), //red is bad
                        //                            new IntRange((int) stats.Green.Mean + MeanDiff, 255),
                        //                            new IntRange((int) stats.Blue.Mean + MeanDiff, 255));
                        //var bm = cf.Apply(e.NewImage);
                        //var bm = PartiallyApplyAvgFilter(e.Frame.Bitmap, Channels.GreenAndBlue, 2, 2, MeanDiff);
                        var bm = UnmanagedImage.FromManagedImage(e.Frame.Bitmap);
                        bm = diffFilter.Apply(bm);
                        var gf = new GaussianBlur(9.0, 3);
                        gf.ApplyInPlace(bm);
                        var cf = new ColorFiltering(new IntRange(10, 255), new IntRange(20, 255),
                                                    new IntRange(20, 255));
                        cf.ApplyInPlace(bm);
                        var blobCounter = new BlobCounter {
                            ObjectsOrder = ObjectsOrder.Size, BackgroundThreshold = Color.FromArgb(255, 15, 20, 20), FilterBlobs = true
                        };
                        blobCounter.ProcessImage(bm);
                        bm.ToManagedImage().Save(@"C:\temp\aforge\diff.jpg");
                        var             blobs = blobCounter.GetObjectsInformation();
                        int             i     = 0;
                        List <IntPoint> corners;
                        do
                        {
                            corners =
                                PointsCloud.FindQuadrilateralCorners(blobCounter.GetBlobsEdgePoints(blobs[i++]));
                        } while (corners.Count != 4);
                        InPlaceSort(corners);
                        Grid.TopLeft     = new Point(corners[0].X, corners[0].Y);
                        Grid.TopRight    = new Point(corners[1].X, corners[1].Y);
                        Grid.BottomLeft  = new Point(corners[2].X, corners[2].Y);
                        Grid.BottomRight = new Point(corners[3].X, corners[3].Y);
                        if (Grid.TopLeft.X > 10 && Grid.TopRight.X < _vs.Width - 10 && Grid.BottomLeft.X > 10 &&
                            Grid.BottomRight.X < _vs.Width - 10 && Grid.TopLeft.Y > 10 && Grid.TopRight.Y > 10 &&
                            Grid.BottomLeft.Y < _vs.Height - 10 && Grid.BottomRight.Y < _vs.Height - 10 &&
                            Grid.TopLeft.X < Grid.BottomRight.X && blobs[i - 1].Area > 60000 &&
                            Grid.BottomLeft.X < Grid.TopRight.X && Grid.BottomLeft.X < Grid.BottomRight.X &&
                            Grid.TopLeft.Y < Grid.BottomLeft.Y && Grid.TopLeft.Y < Grid.BottomRight.Y &&
                            Grid.TopRight.Y < Grid.BottomLeft.Y && Grid.TopRight.Y < Grid.BottomRight.Y)
                        {
                            _calibrationStep++;
                            _vs.Clear();
                            FillRects();
                            Grid.AddPoint(new Point(), new Point(corners[0].X, corners[0].Y));
                            Grid.AddPoint(new Point(0, _vs.Height), new Point(corners[1].X, corners[1].Y));
                            Grid.AddPoint(new Point(_vs.Width, 0), new Point(corners[2].X, corners[2].Y));
                            Grid.AddPoint(new Point(_vs.Width, _vs.Height), new Point(corners[3].X, corners[3].Y));
                        }
                        else
                        {
                            _calibrationStep = 0;
                            _errors++;
                        }
                        break;

                    case 1:
                        diffFilter.OverlayImage = e.Frame.Bitmap;
                        //_vs.AddRect(0, 0, (int) _vs.Width, (int) _vs.Height, Color.FromArgb(255, 255, 255, 255));
                        _vs.Clear();
                        for (int y = 0; y < Columncount; y++)
                        {
                            for (int x = 0; x < Rowcount; x++)
                            {
                                if (!(y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1))
                                {
                                    _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight),
                                                (int)_sqrwidth, (int)_sqrheight,
                                                Color.FromArgb(255, 255, 255, 255));
                                }
                            }
                        }
                        _vs.Draw();
                        _calibrationStep++;
                        break;

                    case 0:
                        Grid = new Grid(e.Frame.Bitmap.Width, e.Frame.Bitmap.Height);
                        var thread = new Thread(() =>
                        {
                            _vs.Show();
                            _vs.AddRect(0, 0, (int)_vs.Width, (int)_vs.Height, Color.FromArgb(255, 0, 0, 0));
                        });
                        thread.SetApartmentState(ApartmentState.STA);
                        thread.Start();
                        thread.Join();
                        for (int y = 0; y < Columncount; y++)
                        {
                            for (int x = 0; x < Rowcount; x++)
                            {
                                if (y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1)
                                {
                                    _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight),
                                                (int)_sqrwidth, (int)_sqrheight,
                                                Color.FromArgb(255, 255, 255, 255));
                                }
                            }
                        }
                        _vs.Draw();
                        _calibrationStep++;
                        break;
                    }
                }
            }
            Debug.WriteLine("Releasing");
            await Task.Delay(100);

            _sem.Release();
        }
Beispiel #21
0
        /// <summary>
        /// Execute the segmentation process.After the segmentation is over you will
        /// have a collection of moving objects that were idetified in this frame.
        /// </summary>
        /// <param name="frame">The frame.Can not be null.</param>
        /// <returns>
        /// A collection of moving objects, i.e. blobs.
        /// </returns>
        /// <exception cref="ArgumentNullException">Frame is null.</exception>
        /// <exception cref="ProcessException">Error while executing the algorithm.</exception>
        public ICollection <ExtendedBlob> execute(Bitmap frame)
        {
            /*
             * 1. Convert image to grayscale.
             * 2. Run Difference algorithm.
             * 3. Use threshold of 40 to turn it into binary frame, why ? see paper
             * "Moving target classification and tracking from real time video" by Lipton.
             * 4. Apply connected components algorithm.
             */

            // First time running means no prerior data to compare.
            if (previousFrame == null)
            {
                // create initial backgroung image
                previousFrame = grayscaleFilter.Apply(frame);
                // get image dimension
                width  = frame.Width;
                height = frame.Height;

                // Return empty list.
                return(new  List <ExtendedBlob>());
            }
            else
            {
                // apply the grayscale file
                Bitmap grayscaledImage = grayscaleFilter.Apply(frame);

                // set backgroud frame as an overlay for difference filter
                differenceFilter.OverlayImage = previousFrame;

                // apply difference filter
                Bitmap differenceImage = differenceFilter.Apply(grayscaledImage);

                // lock the temporary image and apply some filters on the locked data
                bitmapData = differenceImage.LockBits(new Rectangle(0, 0, width, height),
                                                      ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

                // threshold filter
                thresholdFilter.ApplyInPlace(bitmapData);
                Bitmap medianImage = medianFilter.Apply(bitmapData);
                pixelateFilter.ApplyInPlace(medianImage);

                // unlock temporary image
                differenceImage.UnlockBits(bitmapData);
                differenceImage.Dispose();


                // Run connected components.
                List <ExtendedBlob> returnValue = runConectedComponentsAlgorithm(medianImage);
                // Update users.
                OnImageChanged(new ImageChangedEventArgs((Image)medianImage.Clone()));

                // Clean
                medianImage.Dispose();
                // dispose old background
                previousFrame.Dispose();
                // set backgound to current
                previousFrame = grayscaledImage;

                return(returnValue);
            }
        }
Beispiel #22
0
        private void ParseImage(int imageId)
        {
            DateTime startTime = DateTime.Now;

            Console.WriteLine(string.Format("Start parsing {0}", startTime));

            string difImagePath = "";

            if (imageId >= 0)
            {
                difImagePath = DifBoardImages[imageId];
            }

            Bitmap orig;
            Bitmap dif;

            try
            {
                Console.WriteLine("Load files");

                orig = (Bitmap)Bitmap.FromFile(OriginalBoardImage);

                if (imageId >= 0)
                {
                    dif = (Bitmap)Bitmap.FromFile(difImagePath);
                }
                else
                {
                    dif = (Bitmap)internalImageContainer;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERR: " + ex.Message);
                Console.WriteLine(ex.StackTrace);

                // kill any exception due to missing files
                return;
            }

            Difference filter = new Difference(orig);

            dif = filter.Apply(dif);
            Console.WriteLine("Apply difference filter");

            BlobCounter blobCounter = new BlobCounter();

            blobCounter.ProcessImage(dif);
            Console.WriteLine("Blob counting");
            Blob[] blobs = blobCounter.GetObjectsInformation();

            // create Graphics object to draw on the image and a pen
            Graphics           g            = Graphics.FromImage(dif);
            Pen                redPen       = new Pen(Color.Red, 3);
            Pen                bluePen      = new Pen(Color.Blue, 1);
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

            // check each object and draw circle around objects, which
            // are recognized as circles
            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);

                Console.Write(string.Format("Detected {0} points", edgePoints.Count));

                if (edgePoints.Count <= 1)
                {
                    continue;
                }

                List <IntPoint> points = new List <IntPoint>();

                if (shapeChecker.IsQuadrilateral(edgePoints))
                {
                    Console.Write(", quadrilateral");
                }
                else if (shapeChecker.IsConvexPolygon(edgePoints, out points))
                {
                    Console.Write(", convex poligon");
                }
                else if (shapeChecker.IsTriangle(edgePoints, out points))
                {
                    Console.Write(", triangle");
                }

                Pen usePen = shapeChecker.IsQuadrilateral(edgePoints) ? redPen : bluePen;

                int centerX = edgePoints[0].X;
                int centerY = edgePoints[0].Y;

                for (int j = 0; j < edgePoints.Count - 1; j++)
                {
                    centerX += edgePoints[j + 1].X;
                    centerY += edgePoints[j + 1].Y;

                    g.DrawLine(usePen, edgePoints[j].X, edgePoints[j].Y, edgePoints[j + 1].X, edgePoints[j + 1].Y);
                }

                // add last point
                centerX += edgePoints[edgePoints.Count - 1].X;
                centerY += edgePoints[edgePoints.Count - 1].Y;

                g.DrawLine(usePen
                           , edgePoints[0].X, edgePoints[0].Y
                           , edgePoints[edgePoints.Count - 1].X, edgePoints[edgePoints.Count - 1].Y);

                Console.WriteLine(string.Format("\nOriginal (x, y): {0}, {1}"
                                                , new object[] {
                    (centerX / edgePoints.Count), (centerY / edgePoints.Count)
                }));
                Console.WriteLine(string.Format("Detected (x, y): {0}, {1}"
                                                , (centerX / edgePoints.Count)
                                                , (centerY / edgePoints.Count)
                                                ));
            }

            redPen.Dispose();
            bluePen.Dispose();
            g.Dispose();

            //DifI = dif;
            dif.Save(dirPath + "processed-dif.jpg");

            Console.WriteLine(string.Format("Duration: {0} sec", (DateTime.Now - startTime)));
        }
        private async Task CalibThread(FrameReadyEventArgs e)
        {
            //Debug.WriteLine("Calibrating " + _calibrationStep + " " + _drawing);
            //e.Frame.Bitmap.Save(@"C:\temp\daforge\src\img" + (_calibrationStep<10?"0":"") + _calibrationStep + "-" + (_drawing?"1":"0") + "-" + _errors + ".jpg", ImageFormat.Jpeg);
            if (_errors > 100)
            {
                //calibration not possible
                return;
            }
            if (_calibrationStep == CalibrationFrames)
            {
                _cc.FrameReady -= BaseCalibration; // TODO
                //Grid.Calculate();
                //Grid.PredictFromCorners();
                _vs.Close();
                Console.WriteLine("Calibration complete");
                CalibrationCompleted(this, new EventArgs());
            }
            else
            {
                switch (_calibrationStep)
                {
                // get the corners from the difference image
                case 2:
                    var bm = UnmanagedImage.FromManagedImage(e.Frame.Bitmap);
                    //diffFilter.OverlayImage.Save(@"C:\temp\daforge\diff\src" + _errors + ".jpg", ImageFormat.Jpeg);
                    bm = diffFilter.Apply(bm);
                    var gf = new GaussianBlur(9.0, 3);
                    gf.ApplyInPlace(bm);
                    var cf = new ColorFiltering(new IntRange(10, 255), new IntRange(20, 255),
                                                new IntRange(20, 255));
                    cf.ApplyInPlace(bm);
                    var blobCounter = new BlobCounter
                    {
                        ObjectsOrder        = ObjectsOrder.Size,
                        BackgroundThreshold = Color.FromArgb(255, 15, 20, 20),
                        FilterBlobs         = true
                    };
                    blobCounter.ProcessImage(bm);
                    //bm.ToManagedImage().Save(@"C:\temp\daforge\diff\img" + _calibrationStep + "-" + _errors + ".jpg", ImageFormat.Jpeg);
                    var blobs = blobCounter.GetObjectsInformation();
                    if (blobs.Any())
                    {
                        int             i = 0;
                        List <IntPoint> corners;
                        do
                        {
                            corners =
                                PointsCloud.FindQuadrilateralCorners(blobCounter.GetBlobsEdgePoints(blobs[i++]));
                        } while (corners.Count != 4);
                        RecursiveAForgeCalibrator.GridBlobs.InPlaceSort(corners);
                        Grid.TopLeft     = new Point(corners[0].X, corners[0].Y);
                        Grid.TopRight    = new Point(corners[1].X, corners[1].Y);
                        Grid.BottomLeft  = new Point(corners[2].X, corners[2].Y);
                        Grid.BottomRight = new Point(corners[3].X, corners[3].Y);
                        if (Grid.TopLeft.X > 10 && Grid.TopRight.X < _vs.Width - 5 && Grid.BottomLeft.X > 5 &&
                            Grid.BottomRight.X < _vs.Width - 5 && Grid.TopLeft.Y > 10 && Grid.TopRight.Y > 5 &&
                            Grid.BottomLeft.Y < _vs.Height - 5 && Grid.BottomRight.Y < _vs.Height - 5 &&
                            Grid.TopLeft.X < Grid.BottomRight.X &&     //blobs[i - 1].Area > 60000 &&
                            Grid.BottomLeft.X < Grid.TopRight.X && Grid.BottomLeft.X < Grid.BottomRight.X &&
                            Grid.TopLeft.Y < Grid.BottomLeft.Y && Grid.TopLeft.Y < Grid.BottomRight.Y &&
                            Grid.TopRight.Y < Grid.BottomLeft.Y && Grid.TopRight.Y < Grid.BottomRight.Y)
                        {
                            _calibrationStep++;
                            _vs.Clear();
                            Grid.AddPoint(new Point(), new Point(corners[0].X, corners[0].Y));
                            Grid.AddPoint(new Point(0, _vs.Height), new Point(corners[1].X, corners[1].Y));
                            Grid.AddPoint(new Point(_vs.Width, 0), new Point(corners[2].X, corners[2].Y));
                            Grid.AddPoint(new Point(_vs.Width, _vs.Height),
                                          new Point(corners[3].X, corners[3].Y));
                        }
                        else
                        {
                            _calibrationStep = 0;
                            _errors++;
                        }
                    }
                    else
                    {
                        _calibrationStep = 0;
                        _errors++;
                        _vs.Draw();
                    }
                    break;

                case 1:     // draw second image, store the first
                    diffFilter.OverlayImage = e.Frame.Bitmap;
                    //diffFilter.OverlayImage.Save(@"C:\temp\daforge\diff\srcf" + _errors + ".jpg", ImageFormat.Jpeg);
                    //_vs.AddRect(0, 0, (int) _vs.Width, (int) _vs.Height, Color.FromArgb(255, 255, 255, 255));
                    _vs.Clear();
                    for (int y = 0; y < Columncount; y++)
                    {
                        for (int x = 0; x < Rowcount; x++)
                        {
                            if (!(y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1))
                            {
                                _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight),
                                            (int)_sqrwidth, (int)_sqrheight,
                                            Color.FromArgb(255, 255, 255, 255));
                            }
                        }
                    }
                    _vs.Draw();
                    _calibrationStep++;
                    break;

                //draw the first image
                case 0:
                    Grid            = new Grid(e.Frame.Bitmap.Width, e.Frame.Bitmap.Height);
                    Grid.ScreenSize = new Rectangle(0, 0, _vs.Width, _vs.Height);
                    var thread = new Thread(() =>
                    {
                        _vs.Show();
                        _vs.AddRect(0, 0, _vs.Width, _vs.Height, Color.FromArgb(255, 0, 0, 0));
                    });
                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start();
                    thread.Join();
                    for (int y = 0; y < Columncount; y++)
                    {
                        for (int x = 0; x < Rowcount; x++)
                        {
                            if (y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1)
                            {
                                _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight),
                                            (int)_sqrwidth, (int)_sqrheight,
                                            Color.FromArgb(255, 255, 255, 255));
                            }
                        }
                    }
                    _vs.Draw();
                    _calibrationStep++;
                    break;
                }
            }
            Debug.WriteLine("Releasing");
            await Task.Delay(500);

            _sem.Release();
        }
Beispiel #24
0
        private void ParseImage(int imageId)
        {
            DateTime startTime = DateTime.Now;

            labelDetection.Text = "";

            string difImagePath = "";

            if (imageId >= 0)
            {
                difImagePath = DifBoardImages[imageId];
            }

            Bitmap orig;
            Bitmap dif;

            try
            {
                orig = (Bitmap)Bitmap.FromFile(OriginalBoardImage);

                if (imageId >= 0)
                {
                    dif = (Bitmap)Bitmap.FromFile(difImagePath);
                }
                else
                {
                    dif = (Bitmap)pictureBoxGenerated.Image;
                }
            }
            catch {
                // kill any exception due to missing files
                return;
            }

            pictureBoxOrig.Image = dif;

            Difference filter = new Difference(orig);

            dif = filter.Apply(dif);

            BlobCounter blobCounter = new BlobCounter();

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

            // create Graphics object to draw on the image and a pen
            Graphics           g            = Graphics.FromImage(dif);
            Pen                redPen       = new Pen(Color.Red, 3);
            Pen                bluePen      = new Pen(Color.Blue, 1);
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

            // check each object and draw circle around objects, which
            // are recognized as circles
            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);

                labelDetection.Text = string.Format("{0} points", edgePoints.Count);

                if (edgePoints.Count <= 1)
                {
                    continue;
                }

                List <IntPoint> points = new List <IntPoint>();

                if (shapeChecker.IsQuadrilateral(edgePoints))
                {
                    labelDetection.Text += ", quadrilateral";
                }
                else if (shapeChecker.IsConvexPolygon(edgePoints, out points))
                {
                    labelDetection.Text += ", convex poligon";
                }
                else if (shapeChecker.IsTriangle(edgePoints, out points))
                {
                    labelDetection.Text += ", triangle";
                }

                Pen usePen = shapeChecker.IsQuadrilateral(edgePoints) ? redPen : bluePen;

                int centerX = edgePoints[0].X;
                int centerY = edgePoints[0].Y;
                int minX    = centerX;
                int minY    = centerY;
                int maxX    = centerX;
                int maxY    = centerY;
                for (int j = 0; j < edgePoints.Count - 1; j++)
                {
                    centerX += edgePoints[j + 1].X;
                    centerY += edgePoints[j + 1].Y;

                    if (edgePoints[j + 1].X < minX)
                    {
                        minX = edgePoints[j + 1].X;
                    }
                    if (edgePoints[j + 1].Y < minY)
                    {
                        minY = edgePoints[j + 1].Y;
                    }
                    if (edgePoints[j + 1].X > maxX)
                    {
                        maxX = edgePoints[j + 1].X;
                    }
                    if (edgePoints[j + 1].Y > maxX)
                    {
                        maxX = edgePoints[j + 1].Y;
                    }

                    g.DrawLine(usePen, edgePoints[j].X, edgePoints[j].Y, edgePoints[j + 1].X, edgePoints[j + 1].Y);
                }

                g.DrawLine(usePen
                           , edgePoints[0].X, edgePoints[0].Y
                           , edgePoints[edgePoints.Count - 1].X, edgePoints[edgePoints.Count - 1].Y);

                labelDetectedPosition.Text = string.Format("{0}, {1}"
                                                           , new object[] {
                    (centerX / edgePoints.Count), (centerY / edgePoints.Count)
                    //, maxX - minX, maxY - minY
                });
                labelDetectionOffset.Text = string.Format("{0}, {1}"
                                                          , Grigore.Position.X - (centerX / edgePoints.Count)
                                                          , Grigore.Position.Y - (centerY / edgePoints.Count)
                                                          );
            }

            redPen.Dispose();
            bluePen.Dispose();
            g.Dispose();

            pictureBoxRoboBoard.Image = dif;

            labelDetection.Text = string.Format("{1}, {0} sec", (DateTime.Now - startTime), labelDetection.Text);
        }
        private async Task CalibThread(FrameReadyEventArgs e)
        {
            Debug.WriteLine("Calibrating " + _calibrationStep + " ");
            e.Frame.Bitmap.Save(@"C:\temp\daforge\src\img" + (_calibrationStep < 10?"0":"") + _calibrationStep + "-" + _errors + ".jpg", ImageFormat.Jpeg);
            if (_errors > 100)
            {
                //calibration not possible
                return;
            }
            if (_calibrationStep > CalibrationFrames)
            {
                // A concurrency problem, do nothing
            }
            if (_calibrationStep == CalibrationFrames)
            {
                _cc.FrameReady -= BaseCalibration;
                _calibrationStep++;
                //Grid.Calculate();
                _vs.Close();
                CalibrationCompleted(this, new EventArgs());
            }
            else
            {
                if (_calibrationStep > 2)
                {
                    // analyse diffimage
                    _vs.Clear();
                    var img = diffFilter.Apply(e.Frame.Bitmap);
                    var mf  = new GaussianBlur(0.6, 5);
                    var gf  = Grayscale.CommonAlgorithms.BT709;
#if DEBUG
                    actImg = (Bitmap)img.Clone();
#endif
                    img = gf.Apply(img);
                    var stats = new ImageStatistics(img);
                    mf.ApplyInPlace(img);
                    mf = new GaussianBlur(0.7, 7);
                    mf.ApplyInPlace(img);
                    var tf = new Threshold((int)(stats.GrayWithoutBlack.Mean + stats.GrayWithoutBlack.StdDev * 2.0));
                    tf.ApplyInPlace(img);
                    img.Save(@"C:\temp\daforge\diff\img" + _calibrationStep + "-" + _errors + ".jpg", ImageFormat.MemoryBmp);
                    var blobCounter = new BlobCounter
                    {
                        ObjectsOrder         = ObjectsOrder.YX,
                        MaxHeight            = 30,
                        MinHeight            = 10,
                        MaxWidth             = 30,
                        MinWidth             = 10,
                        FilterBlobs          = true,
                        CoupledSizeFiltering = false
                    };
                    blobCounter.ProcessImage(img);
                    if (ProcessBlobs(blobCounter))
                    {
                        _calibrationStep++;
                    }
                    else
                    {
                        _errors++;
                    }
#if DEBUG
                    actImg.Save(@"C:\temp\daforge\squares\img" + _calibrationStep + ".jpg", ImageFormat.Jpeg);
#endif
                    DrawMarkers();
                }
                else
                {
                    switch (_calibrationStep)
                    {
                    case 2:
                        var bm = UnmanagedImage.FromManagedImage(e.Frame.Bitmap);
                        //diffFilter.OverlayImage.Save(@"C:\temp\daforge\diff\src" + _errors + ".jpg", ImageFormat.Jpeg);
                        bm = diffFilter.Apply(bm);
                        var gf = new GaussianBlur(0.8, 5);
                        gf.ApplyInPlace(bm);
                        var cf = new ColorFiltering(new IntRange(10, 255), new IntRange(20, 255),
                                                    new IntRange(20, 255));
                        cf.ApplyInPlace(bm);
                        var blobCounter = new BlobCounter
                        {
                            ObjectsOrder = ObjectsOrder.Size,
                            MinHeight    = 200,
                            MinWidth     = 300,
                            //BackgroundThreshold = Color.FromArgb(255, 20, 20, 50),
                            //CoupledSizeFiltering = false,
                            FilterBlobs = true
                        };
                        blobCounter.ProcessImage(bm);
                        bm.ToManagedImage()
                        .Save(@"C:\temp\daforge\diff\img" + _calibrationStep + "-" + _errors + ".jpg",
                              ImageFormat.Jpeg);
                        var blobs = blobCounter.GetObjectsInformation();
                        if (blobs.Any())
                        {
                            int             i = 0;
                            List <IntPoint> corners;
                            do
                            {
                                corners =
                                    PointsCloud.FindQuadrilateralCorners(blobCounter.GetBlobsEdgePoints(blobs[i++]));
                            } while (corners.Count != 4);
                            RecursiveAForgeCalibrator.GridBlobs.InPlaceSort(corners);
                            Grid.TopLeft     = new Point(corners[0].X, corners[0].Y);
                            Grid.TopRight    = new Point(corners[1].X, corners[1].Y);
                            Grid.BottomLeft  = new Point(corners[2].X, corners[2].Y);
                            Grid.BottomRight = new Point(corners[3].X, corners[3].Y);
                            if (Grid.TopLeft.X > 10 && Grid.TopRight.X < _vs.Width - 5 && Grid.BottomLeft.X > 5 &&
                                Grid.BottomRight.X < _vs.Width - 5 && Grid.TopLeft.Y > 10 && Grid.TopRight.Y > 5 &&
                                Grid.BottomLeft.Y < _vs.Height - 5 && Grid.BottomRight.Y < _vs.Height - 5 &&
                                Grid.TopLeft.X < Grid.BottomRight.X &&
                                Grid.BottomLeft.X < Grid.TopRight.X && Grid.BottomLeft.X < Grid.BottomRight.X &&
                                Grid.TopLeft.Y < Grid.BottomLeft.Y && Grid.TopLeft.Y < Grid.BottomRight.Y &&
                                Grid.TopRight.Y < Grid.BottomLeft.Y && Grid.TopRight.Y < Grid.BottomRight.Y)
                            {
                                _calibrationStep++;
                                _vs.Clear();
                                Grid.AddPoint(new Point(), new Point(corners[0].X, corners[0].Y));
                                Grid.AddPoint(new Point(0, _vs.Height), new Point(corners[1].X, corners[1].Y));
                                Grid.AddPoint(new Point(_vs.Width, 0), new Point(corners[2].X, corners[2].Y));
                                Grid.AddPoint(new Point(_vs.Width, _vs.Height),
                                              new Point(corners[3].X, corners[3].Y));
                                DrawMarkers();
                            }
                            else
                            {
                                _calibrationStep = 0;
                                _errors++;
                            }
                        }
                        else
                        {
                            _calibrationStep = 0;
                            _errors++;
                        }
                        break;

                    case 1:
                        diffFilter.OverlayImage = e.Frame.Bitmap;
                        //diffFilter.OverlayImage.Save(@"C:\temp\daforge\diff\srcf" + _errors + ".jpg", ImageFormat.Jpeg);
                        //_vs.AddRect(0, 0, (int) _vs.Width, (int) _vs.Height, Color.FromArgb(255, 255, 255, 255));
                        _vs.Clear();
                        DrawInverted(0, 0, _vs.Width, _vs.Height);
                        _vs.Draw();
                        _calibrationStep++;
                        break;

                    case 0:
                        Grid            = new Grid(e.Frame.Bitmap.Width, e.Frame.Bitmap.Height);
                        Grid.ScreenSize = new Rectangle(0, 0, _vs.Width, _vs.Height);
                        var thread = new Thread(() =>
                        {
                            _vs.Show();
                        });
                        thread.SetApartmentState(ApartmentState.STA);
                        thread.Start();
                        thread.Join();
                        DrawBackground();
                        _vs.Draw();
                        _calibrationStep++;
                        break;
                    }
                }
            }
            Debug.WriteLine("Releasing");
            await Task.Delay(500);

            _sem.Release();
        }
        // Slow version (eventually pictures could be resized for more performance)
        private void FindPersonPosition(UnmanagedImage frameImage)
        {
            var stopwatch = Stopwatch.StartNew();

            Difference     diffFilter = new Difference(_baseImage);
            UnmanagedImage diffImg    = diffFilter.Apply(frameImage);
            //diffImg.ToManagedImage().Save(@"c:\temp\recognition\diff.png");

            // TODO ev. ColorFilter
            Grayscale grayFilter = new GrayscaleBT709();

            diffImg = grayFilter.Apply(diffImg);
            //diffImg.ToManagedImage().Save(@"c:\temp\recognition\diff-gray.png");

            Threshold thresholdFilter = new Threshold(30);

            thresholdFilter.ApplyInPlace(diffImg);
            //diffImg.ToManagedImage().Save(@"c:\temp\recognition\diff-gray-thres.png");

            Median medianFilter = new Median(3);

            medianFilter.ApplyInPlace(diffImg);
            //diffImg.ToManagedImage().Save(@"c:\temp\recognition\diff-gray-thres-med.png");

            // Find Person Borders (TODO ev. Performance Boost, wenn CollectActivePixels vermieden wird)
            // per Iteration durch Pixeldaten koennte ev. die groesste und die kleinste x-Koordinate schneller
            // gefunden werden..
            List <AForge.IntPoint> whites = diffImg.CollectActivePixels(BOTTOM_BASE_STRIPE);
            int minX = whites.Min(obj => obj.X);
            int maxX = whites.Max(obj => obj.X);

            // Schneller als MS Algorithmus fuer Min und Max
            // (auch wenn es assymptotisch gleich schnell sein muesste)
            //int minX = int.MaxValue;
            //int maxX = int.MinValue;
            //foreach(AForge.IntPoint whitePixel in whites)
            //{
            //    //if (whitePixel.X < minX)
            //    //{
            //    //    minX = whitePixel.X;
            //    //}
            //    if (whitePixel.X > maxX)
            //    {
            //        maxX = whitePixel.X;
            //    }
            //}

            var elapsedStage1 = stopwatch.ElapsedMilliseconds;

            Debug.WriteLine("Person is this fat: {0}, {1}", minX, maxX);

            // Find Arm
            whites = diffImg.CollectActivePixels(new Rectangle(maxX, 0, 640 - maxX, 480 - BOTTOM_BASE_STRIPE.Height));
            maxX   = whites.Max(obj => obj.X);
            var armPoint = whites.First(obj => obj.X == maxX);

            Debug.WriteLine("Persons arm is here: {0}, {1}", armPoint.X, armPoint.Y);

            Bitmap b = frameImage.ToManagedImage();

            using (Graphics g = Graphics.FromImage(b))
            {
                g.DrawEllipse(Pens.Red, armPoint.X, armPoint.Y, 5, 5);
                b.Save(@"c:\temp\recognition\gach.png");
            }

            Debug.WriteLine("Time: {0}", elapsedStage1);
            stopwatch.Stop();
        }
        private async Task CalibThread(FrameReadyEventArgs e)
        {
            Debug.WriteLine("Calibrating " + _calibrationStep + " " + _drawing);
            e.Frame.Bitmap.Save(@"C:\temp\daforge\src\img" + (_calibrationStep < 10?"0":"") + _calibrationStep + "-" + (_drawing?"1":"0") + "-" + _errors + ".jpg", ImageFormat.Jpeg);
            if (_errors > 100)
            {
                //calibration not possible
                return;
            }
            if (_calibrationStep == CalibrationFrames && !_drawing)
            {
                _cc.FrameReady -= BaseCalibration; // TODO
                //Grid.Calculate();
                _vs.Close();
                CalibrationCompleted(this, new EventArgs());
            }
            else
            {
                if (_calibrationStep > 2)
                {
                    if (_drawing)
                    {
                        // draw diffimage
                        _drawing = false;
                        _vs.Clear();
                        FillRects();
                        diffFilter.OverlayImage = e.Frame.Bitmap;
                    }
                    else
                    {
                        // analyse diffimage
                        _calibrationStep++;
                        _drawing = true;
                        _vs.Clear();
                        FillRects();
                        _calibrationStep--;
                        var gbm = diffFilter.Apply(e.Frame.Bitmap);
                        gbm.Save(@"C:\temp\daforge\diff\img" + _calibrationStep + ".jpg", ImageFormat.Jpeg);
#if DEBUG
                        actImg = (Bitmap)gbm.Clone();
#endif
                        var d     = UnmanagedImage.FromManagedImage(gbm);
                        var stats = new ImageStatistics(d);
                        var gcf   = new ColorFiltering(new IntRange(0, 255),
                                                       new IntRange((int)(stats.GreenWithoutBlack.Mean + stats.GreenWithoutBlack.StdDev + 5), 255),
                                                       new IntRange(0, 255));
                        var bcf = new ColorFiltering(new IntRange(0, 255),
                                                     new IntRange(0, 255),
                                                     new IntRange((int)(stats.BlueWithoutBlack.Mean + stats.BlueWithoutBlack.StdDev), 255));
                        //Debug.WriteLine("Green: " + stats.GreenWithoutBlack.Median + " Blue: " + stats.BlueWithoutBlack.Median);
                        //Debug.WriteLine("Green: " + stats.GreenWithoutBlack.Mean + " Blue: " + stats.BlueWithoutBlack.Mean);
                        var bf = new Difference(gcf.Apply(d));
                        bcf.ApplyInPlace(d);
                        bf.ApplyInPlace(d);
                        d.ToManagedImage().Save(@"C:\temp\daforge\diff\img" + _calibrationStep + ".jpg", ImageFormat.Jpeg);
                        stats = new ImageStatistics(d);
                        gcf   = new ColorFiltering(new IntRange(0, 255),
                                                   new IntRange((int)stats.GreenWithoutBlack.Mean, 255),
                                                   new IntRange(0, 255));
                        bcf = new ColorFiltering(new IntRange(0, 255),
                                                 new IntRange(0, 255),
                                                 new IntRange((int)stats.BlueWithoutBlack.Mean, 255));
                        // split channels
                        var bbm = bcf.Apply(d);
                        bbm.ToManagedImage().Save(@"C:\temp\daforge\bimg\img" + _calibrationStep + ".jpg", ImageFormat.Bmp);
                        gcf.ApplyInPlace(d);
                        d.ToManagedImage().Save(@"C:\temp\daforge\gimg\img" + _calibrationStep + ".jpg", ImageFormat.Bmp);
                        var gblobCounter = new BlobCounter
                        {
                            ObjectsOrder         = ObjectsOrder.YX,
                            MaxHeight            = 60,
                            MinHeight            = 15,
                            MaxWidth             = 60,
                            MinWidth             = 15,
                            FilterBlobs          = true,
                            CoupledSizeFiltering = false
                        };
                        gblobCounter.ProcessImage(d);
                        var bblobCounter = new BlobCounter
                        {
                            ObjectsOrder         = ObjectsOrder.YX,
                            MaxHeight            = 60,
                            MinHeight            = 15,
                            MaxWidth             = 60,
                            MinWidth             = 15,
                            FilterBlobs          = true,
                            CoupledSizeFiltering = false
                        };
                        bblobCounter.ProcessImage(bbm);
                        ProcessBlobs(gblobCounter, 0);
                        ProcessBlobs(bblobCounter, 1);
#if DEBUG
                        actImg.Save(@"C:\temp\daforge\squares\img" + _calibrationStep + ".jpg", ImageFormat.Jpeg);
#endif
                        _calibrationStep++;
                    }
                }
                else
                {
                    switch (_calibrationStep)
                    {
                    case 2:
                        var bm = UnmanagedImage.FromManagedImage(e.Frame.Bitmap);
                        //diffFilter.OverlayImage.Save(@"C:\temp\daforge\diff\src" + _errors + ".jpg", ImageFormat.Jpeg);
                        bm = diffFilter.Apply(bm);
                        var gf = new GaussianBlur(9.0, 3);
                        gf.ApplyInPlace(bm);
                        var cf = new ColorFiltering(new IntRange(10, 255), new IntRange(20, 255),
                                                    new IntRange(20, 255));
                        cf.ApplyInPlace(bm);
                        var blobCounter = new BlobCounter {
                            ObjectsOrder = ObjectsOrder.Size, BackgroundThreshold = Color.FromArgb(255, 15, 20, 20), FilterBlobs = true
                        };
                        blobCounter.ProcessImage(bm);
                        bm.ToManagedImage().Save(@"C:\temp\daforge\diff\img" + _calibrationStep + "-" + _errors + ".jpg", ImageFormat.Jpeg);
                        var blobs = blobCounter.GetObjectsInformation();
                        if (blobs.Any())
                        {
                            int             i = 0;
                            List <IntPoint> corners;
                            do
                            {
                                corners =
                                    PointsCloud.FindQuadrilateralCorners(blobCounter.GetBlobsEdgePoints(blobs[i++]));
                            } while (corners.Count != 4);
                            RecursiveAForgeCalibrator.GridBlobs.InPlaceSort(corners);
                            Grid.TopLeft     = new Point(corners[0].X, corners[0].Y);
                            Grid.TopRight    = new Point(corners[1].X, corners[1].Y);
                            Grid.BottomLeft  = new Point(corners[2].X, corners[2].Y);
                            Grid.BottomRight = new Point(corners[3].X, corners[3].Y);
                            if (Grid.TopLeft.X > 10 && Grid.TopRight.X < _vs.Width - 5 && Grid.BottomLeft.X > 5 &&
                                Grid.BottomRight.X < _vs.Width - 5 && Grid.TopLeft.Y > 10 && Grid.TopRight.Y > 5 &&
                                Grid.BottomLeft.Y < _vs.Height - 5 && Grid.BottomRight.Y < _vs.Height - 5 &&
                                Grid.TopLeft.X < Grid.BottomRight.X && blobs[i - 1].Area > 60000 &&
                                Grid.BottomLeft.X < Grid.TopRight.X && Grid.BottomLeft.X < Grid.BottomRight.X &&
                                Grid.TopLeft.Y < Grid.BottomLeft.Y && Grid.TopLeft.Y < Grid.BottomRight.Y &&
                                Grid.TopRight.Y < Grid.BottomLeft.Y && Grid.TopRight.Y < Grid.BottomRight.Y)
                            {
                                _calibrationStep++;
                                _drawing = true;
                                _vs.Clear();
                                FillRects();
                                Grid.AddPoint(new Point(), new Point(corners[0].X, corners[0].Y));
                                Grid.AddPoint(new Point(0, _vs.Height), new Point(corners[1].X, corners[1].Y));
                                Grid.AddPoint(new Point(_vs.Width, 0), new Point(corners[2].X, corners[2].Y));
                                Grid.AddPoint(new Point(_vs.Width, _vs.Height),
                                              new Point(corners[3].X, corners[3].Y));
                                //_mapper.PredictFromCorners();
                            }
                            else
                            {
                                _calibrationStep = 0;
                                _errors++;
                            }
                        }
                        else
                        {
                            _calibrationStep = 0;
                            _errors++;
                            _vs.Draw();
                        }
                        break;

                    case 1:
                        diffFilter.OverlayImage = e.Frame.Bitmap;
                        //diffFilter.OverlayImage.Save(@"C:\temp\daforge\diff\srcf" + _errors + ".jpg", ImageFormat.Jpeg);
                        //_vs.AddRect(0, 0, (int) _vs.Width, (int) _vs.Height, Color.FromArgb(255, 255, 255, 255));
                        _vs.Clear();
                        for (int y = 0; y < Columncount; y++)
                        {
                            for (int x = 0; x < Rowcount; x++)
                            {
                                if (!(y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1))
                                {
                                    _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight),
                                                (int)_sqrwidth, (int)_sqrheight,
                                                Color.FromArgb(255, 255, 255, 255));
                                }
                            }
                        }
                        _vs.Draw();
                        _calibrationStep++;
                        break;

                    case 0:
                        Grid            = new Grid(e.Frame.Bitmap.Width, e.Frame.Bitmap.Height);
                        Grid.ScreenSize = new Rectangle(0, 0, _vs.Width, _vs.Height);
                        _mapper         = new CornerBarycentricMapper(Grid);
                        var thread = new Thread(() =>
                        {
                            _vs.Show();
                            _vs.AddRect(0, 0, _vs.Width, _vs.Height, Color.FromArgb(255, 0, 0, 0));
                        });
                        thread.SetApartmentState(ApartmentState.STA);
                        thread.Start();
                        thread.Join();
                        for (int y = 0; y < Columncount; y++)
                        {
                            for (int x = 0; x < Rowcount; x++)
                            {
                                if (y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1)
                                {
                                    _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight),
                                                (int)_sqrwidth, (int)_sqrheight,
                                                Color.FromArgb(255, 255, 255, 255));
                                }
                            }
                        }
                        _vs.Draw();
                        _calibrationStep++;
                        break;
                    }
                }
            }
            Debug.WriteLine("Releasing");
            await Task.Delay(500);

            _sem.Release();
        }
Beispiel #28
0
        // Process new frame
        public void ProcessFrame(ref Bitmap image)
        {
            if (backgroundFrame == null)
            {
                // create initial backgroung image
                backgroundFrame = grayscaleFilter.Apply(image);

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

                // just return for the first time
                return;
            }

            Bitmap tmpImage;

            // apply the grayscale file
            tmpImage = grayscaleFilter.Apply(image);

            // set backgroud frame as an overlay for difference filter
            differenceFilter.OverlayImage = backgroundFrame;

            // apply difference filter
            Bitmap tmpImage2 = differenceFilter.Apply(tmpImage);

            // lock the temporary image and apply some filters on the locked data
            bitmapData = tmpImage2.LockBits(new Rectangle(0, 0, width, height),
                                            ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            // threshold filter
            thresholdFilter.ApplyInPlace(bitmapData);
            // erosion filter
            Bitmap tmpImage3 = erosionFilter.Apply(bitmapData);

            // unlock temporary image
            tmpImage2.UnlockBits(bitmapData);
            tmpImage2.Dispose( );

            // calculate amount of changed pixels
            pixelsChanged = (calculateMotionLevel) ?
                            CalculateWhitePixels(tmpImage3) : 0;

            // dispose old background
            backgroundFrame.Dispose( );
            // set backgound to current
            backgroundFrame = tmpImage;

            // extract red channel from the original image
            Bitmap redChannel = extrachChannel.Apply(image);

            //  merge red channel with moving object
            mergeFilter.OverlayImage = tmpImage3;
            Bitmap tmpImage4 = mergeFilter.Apply(redChannel);

            redChannel.Dispose( );
            tmpImage3.Dispose( );

            // Updated from original example to support new AForge Libraries (Sumit)
            if (replaceChannel == null)
            {
                replaceChannel = new ReplaceChannel(RGB.R, tmpImage4);
            }
            else
            {
                replaceChannel.ChannelImage = tmpImage4;
            }

            Bitmap tmpImage5 = replaceChannel.Apply(image);

            tmpImage4.Dispose( );

            image.Dispose( );
            image = tmpImage5;
        }