예제 #1
0
        private void ProcessFrame(object sender, EventArgs e)
        {
            if (_capture != null && _capture.Ptr != IntPtr.Zero)
            {
                _capture.Retrieve(frame, 0);
                gpuFrame.Upload(frame);
                cudaBgMOG2.Apply(gpuFrame, gpuSub);
                CudaInvoke.Threshold(gpuSub, gpuSub, 12, 255, Emgu.CV.CvEnum.ThresholdType.Binary);
                gpuSub.Download(outSub);

                CvInvoke.FindContours(outSub, contours, hiererachy, Emgu.CV.CvEnum.RetrType.List, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxNone);

                for (int i = 0; i < contours.Size; i++)
                {
                    if (CvInvoke.ContourArea(contours[i]) > 50)
                    {
                        contoursGood.Push(contours[i]);
                    }
                }

                grayImage = new Image <Gray, byte>(frame.Width, frame.Height, new Gray(0));
                grayImage.SetZero();
                CvInvoke.DrawContours(grayImage, contoursGood, -1, new MCvScalar(255, 255, 255), -1);
                CvInvoke.Dilate(grayImage, grayImage, element, new Point(-1, -1), 6, Emgu.CV.CvEnum.BorderType.Constant, new MCvScalar(255, 255, 255));
                contoursGood.Clear();

                CvInvoke.FindContours(grayImage, contours, hiererachy, Emgu.CV.CvEnum.RetrType.List, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxNone);

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

                for (int i = 0; i < contours.Size; i++)
                {
                    MCvMoments moments          = CvInvoke.Moments(contours[i], false);
                    Point      WeightedCentroid = new Point((int)(moments.M10 / moments.M00), (int)(moments.M01 / moments.M00));
                    points.Add(WeightedCentroid);
                }

                blobList.AssignToBlobs(points);
                blobList.Draw(frame);
                blobList.Draw(mask);
                blobList.Update();

                CvInvoke.DrawContours(frame, contours, -1, new MCvScalar(0, 0, 255));

                imageBox1.Image = frame;
                imageBox2.Image = mask;

                grayImage.Dispose();

                indexFrame++;
            }
        }