Detect() 공개 메소드

Detect blobs from input image.
public Detect ( Byte>.Image img, CvBlobs blobs ) : uint
img Byte>.Image The input image
blobs CvBlobs The storage for the detected blobs
리턴 uint
예제 #1
1
파일: Class1.cs 프로젝트: Warren-GH/emgucv
      /*
      public void TestCodeBookBGModel()
      {
         using (Capture capture = new Capture())
         using (BGCodeBookModel<Bgr> model = new BGCodeBookModel<Bgr>())
         {
            ImageViewer viewer = new ImageViewer();
            Image<Gray, byte> fgMask = capture.QueryFrame().Convert<Gray, Byte>();

            Application.Idle += delegate(Object sender, EventArgs args)
            {
               Mat frame = capture.QueryFrame();
               model.Apply(frame);
               viewer.Image = model.ForegroundMask; 
            };
            viewer.ShowDialog();
         }
      }

      public void TestBlobTracking()
      {
         MCvFGDStatModelParams fgparam = new MCvFGDStatModelParams();
         fgparam.alpha1 = 0.1f;
         fgparam.alpha2 = 0.005f;
         fgparam.alpha3 = 0.1f;
         fgparam.delta = 2;
         fgparam.is_obj_without_holes = 1;
         fgparam.Lc = 32;
         fgparam.Lcc = 16;
         fgparam.minArea = 15;
         fgparam.N1c = 15;
         fgparam.N1cc = 25;
         fgparam.N2c = 25;
         fgparam.N2cc = 35;
         fgparam.perform_morphing = 0;
         fgparam.T = 0.9f;

         BlobTrackerAutoParam<Bgr> param = new BlobTrackerAutoParam<Bgr>();
         param.BlobDetector = new BlobDetector(Emgu.CV.CvEnum.BlobDetectorType.CC);
         param.FGDetector = new FGDetector<Bgr>(Emgu.CV.CvEnum.ForgroundDetectorType.Fgd, fgparam);
         param.BlobTracker = new BlobTracker(Emgu.CV.CvEnum.BLOBTRACKER_TYPE.MSFG);
         param.FGTrainFrames = 10;
         BlobTrackerAuto<Bgr> tracker = new BlobTrackerAuto<Bgr>(param);

         //MCvFont font = new MCvFont(Emgu.CV.CvEnum.FontFace.HersheySimplex, 1.0, 1.0);

         using(ImageViewer viewer = new ImageViewer())
         using (Capture capture = new Capture())
         {
            capture.ImageGrabbed += delegate(object sender, EventArgs e)
            {
               tracker.Process(capture.RetrieveBgrFrame());
               
               //Image<Bgr, Byte> img = capture.RetrieveBgrFrame();

               Image<Bgr, Byte> img = tracker.ForegroundMask.Convert<Bgr, Byte>();
               foreach (MCvBlob blob in tracker)
               {
                  img.Draw((Rectangle)blob, new Bgr(255.0, 255.0, 255.0), 2);
                  img.Draw(blob.ID.ToString(), Point.Round(blob.Center), CvEnum.FontFace.HersheySimplex, 1.0, new Bgr(255.0, 255.0, 255.0));
               }
               viewer.Image = img;
            };
            capture.Start();
            viewer.ShowDialog();
         }
      }*/
      
      public void TestCvBlob()
      {
         //MCvFont font = new MCvFont(Emgu.CV.CvEnum.FontFace.HersheySimplex, 0.5, 0.5);
         using (CvTracks tracks = new CvTracks())
         using (ImageViewer viewer = new ImageViewer())
         using (Capture capture = new Capture())
         using (Mat fgMask = new Mat())
         {
            //BGStatModel<Bgr> bgModel = new BGStatModel<Bgr>(capture.QueryFrame(), Emgu.CV.CvEnum.BG_STAT_TYPE.GAUSSIAN_BG_MODEL);
            BackgroundSubtractorMOG2 bgModel = new BackgroundSubtractorMOG2(0, 0, true);
            //BackgroundSubstractorMOG bgModel = new BackgroundSubstractorMOG(0, 0, 0, 0);

            capture.ImageGrabbed += delegate(object sender, EventArgs e)
            {
               Mat frame = new Mat();
               capture.Retrieve(frame);
               bgModel.Apply(frame, fgMask);

               using (CvBlobDetector detector = new CvBlobDetector())
               using (CvBlobs blobs = new CvBlobs())
               {
                  detector.Detect(fgMask.ToImage<Gray, Byte>(), blobs);
                  blobs.FilterByArea(100, int.MaxValue);

                  tracks.Update(blobs, 20.0, 10, 0);

                  Image<Bgr, Byte> result = new Image<Bgr, byte>(frame.Size);

                  using (Image<Gray, Byte> blobMask = detector.DrawBlobsMask(blobs))
                  {
                     frame.CopyTo(result, blobMask);
                  }
                  //CvInvoke.cvCopy(frame, result, blobMask);

                  foreach (KeyValuePair<uint, CvTrack> pair in tracks)
                  {
                     if (pair.Value.Inactive == 0) //only draw the active tracks.
                     {
                        CvBlob b = blobs[pair.Value.BlobLabel];
                        Bgr color = detector.MeanColor(b, frame.ToImage<Bgr, Byte>());
                        result.Draw(pair.Key.ToString(), pair.Value.BoundingBox.Location, CvEnum.FontFace.HersheySimplex, 0.5, color);
                        result.Draw(pair.Value.BoundingBox, color, 2);
                        Point[] contour = b.GetContour();
                        result.Draw(contour, new Bgr(0, 0, 255), 1);
                     }
                  }

                  viewer.Image = frame.ToImage<Bgr, Byte>().ConcateVertical(fgMask.ToImage<Bgr, Byte>().ConcateHorizontal(result));
               }
            };
            capture.Start();
            viewer.ShowDialog();
         }
      }
예제 #2
0
        void ProcessFrame(object sender, EventArgs e)
        {
            Mat frame         = _cameraCapture.QueryFrame();
            Mat smoothedFrame = new Mat();

            CvInvoke.GaussianBlur(frame, smoothedFrame, new Size(3, 3), 1); //filter out noises
            //frame._SmoothGaussian(3);

            #region use the BG/FG detector to find the forground mask
            Mat forgroundMask = new Mat();
            _fgDetector.Apply(smoothedFrame, forgroundMask);
            #endregion

            CvBlobs blobs = new CvBlobs();
            _blobDetector.Detect(forgroundMask.ToImage <Gray, byte>(), blobs);
            blobs.FilterByArea(100, int.MaxValue);

            float scale = (frame.Width + frame.Width) / 2.0f;
            _tracker.Update(blobs, 0.01 * scale, 5, 5);

            foreach (var pair in _tracker)
            {
                CvTrack b = pair.Value;
                CvInvoke.Rectangle(frame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
                CvInvoke.PutText(frame, b.Id.ToString(), new Point((int)Math.Round(b.Centroid.X), (int)Math.Round(b.Centroid.Y)), FontFace.HersheyPlain, 1.0, new MCvScalar(255.0, 255.0, 255.0));
            }

            imageBox1.Image = frame;
            imageBox2.Image = forgroundMask;
        }
예제 #3
0
        void ProcessFrame(object sender, EventArgs e)
        {
            Mat frame = _cameraCapture.QueryFrame();
            Image <Bgr, Byte> smoothedFrame = new Image <Bgr, byte>(frame.Size);

            CvInvoke.GaussianBlur(frame, smoothedFrame, new Size(3, 3), 1); //filter out noises
            //frame._SmoothGaussian(3);

            #region use the BG/FG detector to find the forground mask
            Mat forgroundMask = new Mat();
            _fgDetector.Apply(smoothedFrame, forgroundMask);

            #endregion
            CvBlobs blobs = new CvBlobs();
            _blobDetector.Detect(forgroundMask.ToImage <Gray, byte>(), blobs);
            blobs.FilterByArea(100, int.MaxValue);
            //_tracker.Process(smoothedFrame, forgroundMask);

            foreach (var pair in blobs)
            {
                CvBlob b = pair.Value;
                CvInvoke.Rectangle(frame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
                //CvInvoke.PutText(frame,  blob.ID.ToString(), Point.Round(blob.Center), FontFace.HersheyPlain, 1.0, new MCvScalar(255.0, 255.0, 255.0));
            }

            imageBox1.Image = frame;
            imageBox2.Image = forgroundMask;
        }
예제 #4
0
        private void Application_Idle(object sender, EventArgs e)
        {
            Mat a = cap.QueryFrame();

            if (a != null)
            {
                System.Threading.Thread.Sleep((int)(1000.0 / fps - 5));
                imageBox1.Image = a;
                GC.Collect();
            }
            // 讀取彩色影像
            Image <Bgr, byte> image01 = new Image <Bgr, byte>(imageBox1.Image.Bitmap);

            imageBox1.Image = image01;

            var image2     = image01.InRange(new Bgr(10, 1, 13), new Bgr(76, 144, 240));
            var image2_not = image2.Not();

            using (CvBlobs blobs = new CvBlobs())
            {
                blobDetector.Detect(image2, blobs);
                var image3 = image01.Copy();
                foreach (var pair in blobs)
                {
                    CvBlob b = pair.Value;
                    CvInvoke.Rectangle(image3, b.BoundingBox, new MCvScalar(255.255, 255, 0), 5);
                }
                imageBox1.Image = image01;
                imageBox2.Image = image2;
                imageBox3.Image = image3;
                imageBox4.Image = image2_not;
            }
        }
예제 #5
0
        static Image <Bgr, Byte> _doDetect(string fileName)
        {
            Debug.WriteLine($"Processing: {fileName}");

            var frameOrig = new Image <Bgr, Byte>(fileName);

            var frame = frameOrig.Convert <Gray, Byte>();

            Mat smoothedFrame = new Mat();

            CvInvoke.GaussianBlur(frame, smoothedFrame, new Size(19, 19), 7); //filter out noises

            var smoothedImage = smoothedFrame.ToImage <Gray, Byte>();

            if (_original == null)
            {
                _original = smoothedImage;
                return(null);
            }

            var frameDelta = smoothedImage.AbsDiff(_original);
            var thresh     = frameDelta.ThresholdBinary(new Gray(25), new Gray(255));

            thresh = thresh.Dilate(2);

            //File.WriteAllBytes(@"C:\Temp\imagery\aathreh.jpg", thresh.ToJpegData(95));

            _original = smoothedImage;

            //var cnts = new VectorOfVectorOfPoint();
            //CvInvoke.FindContours(thresh.Copy(), cnts, null, RetrType.External,
            //    ChainApproxMethod.ChainApproxSimple);

            //var goer = false;

            //for (var i = 0; i < cnts.Size; i++)
            //{
            //    var c = cnts[i];

            //    if (CvInvoke.ContourArea(c) < 500)
            //    {
            //        continue;
            //    }
            //    goer = true;


            //    //Debug.WriteLine(CvInvoke.ContourArea(c));
            //    //var rect = CvInvoke.BoundingRectangle(c);
            //    //CvInvoke.Rectangle(frame, rect, new MCvScalar(255.0, 255.0, 255.0), 2);
            //}

            ////// File.WriteAllBytes(@"C:\Temp\imagery\aaframes.jpg", frame.ToJpegData(95));

            // return goer;

            //Mat forgroundMask = new Mat();
            //_fgDetector.Apply(smoothedFrame, forgroundMask);

            CvBlobs blobs = new CvBlobs();

            _blobDetector.Detect(thresh, blobs);
            blobs.FilterByArea(1600, int.MaxValue);

            float scale = (frame.Width + frame.Width) / 2.0f;

            //File.WriteAllBytes(@"C:\Temp\imagery\aaout.jpg", smoothedImage.ToJpegData(95));


            _tracker.Update(blobs, scale, 5, 5);

            foreach (var pair in _tracker)
            {
                CvTrack b = pair.Value;
                CvInvoke.Rectangle(frameOrig, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
                CvInvoke.PutText(frameOrig, b.Id.ToString(), new Point((int)Math.Round(b.Centroid.X), (int)Math.Round(b.Centroid.Y)), FontFace.HersheyPlain, 1.0, new MCvScalar(255.0, 255.0, 255.0));
            }

            //  File.WriteAllBytes(@"C:\Temp\imagery\aaframes.jpg", frame.ToJpegData(95));
            // File.WriteAllBytes(@"C:\Temp\imagery\aablur.jpg", smoothedFrame.ToImage<Gray, byte>().ToJpegData(95));

            return(_tracker.Count > 0 ? frameOrig : null);
            //var cnts = new VectorOfVectorOfPoint();
            //CvInvoke.FindContours(thresh.Copy(), cnts, null, RetrType.External,
            //    ChainApproxMethod.ChainApproxSimple);



            //for (var i = 0; i < cnts.Size; i++)
            //{
            //    var c = cnts[i];
            //    Debug.WriteLine(CvInvoke.ContourArea(c));
            //    var rect = CvInvoke.BoundingRectangle(c);
            //    CvInvoke.Rectangle(frame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);


            //}



            //Mat smoothedFrame = new Mat();
            //CvInvoke.GaussianBlur(frame, smoothedFrame, new Size(23, 23), 5); //filter out noises
            ////frame._SmoothGaussian(3);


            //Mat forgroundMask = new Mat();
            //_fgDetector.Apply(smoothedFrame, forgroundMask);


            //CvBlobs blobs = new CvBlobs();
            //_blobDetector.Detect(forgroundMask.ToImage<Gray, byte>(), blobs);
            //blobs.FilterByArea(100, int.MaxValue);

            //float scale = (frame.Width + frame.Width) / 2.0f;

            //File.WriteAllBytes(@"C:\Temp\imagery\aaout.jpg", forgroundMask.ToImage<Gray, byte>().ToJpegData(95));


            //_tracker.Update(blobs, scale, 5, 5);

            //foreach (var pair in _tracker)
            //{
            //    CvTrack b = pair.Value;
            //    CvInvoke.Rectangle(frame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
            //    CvInvoke.PutText(frame, b.Id.ToString(), new Point((int)Math.Round(b.Centroid.X), (int)Math.Round(b.Centroid.Y)), FontFace.HersheyPlain, 1.0, new MCvScalar(255.0, 255.0, 255.0));
            //}

            //File.WriteAllBytes(@"C:\Temp\imagery\aaframes.jpg", frame.ToJpegData(95));
            //File.WriteAllBytes(@"C:\Temp\imagery\aablur.jpg", smoothedFrame.ToImage<Gray, byte>().ToJpegData(95));

            //Console.WriteLine($" >>>> Tracker: {_tracker.Count}, Blobs: {blobs.Count}");



            //foreach (var pair in _tracker)
            //{
            //    CvTrack b = pair.Value;
            //    CvInvoke.Rectangle(frame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
            //    CvInvoke.PutText(frame, b.Id.ToString(), new Point((int)Math.Round(b.Centroid.X), (int)Math.Round(b.Centroid.Y)), FontFace.HersheyPlain, 1.0, new MCvScalar(255.0, 255.0, 255.0));
            //}

            //imageBox1.Image = frame;
            //imageBox2.Image = forgroundMask;
        }
        /// <summary>
        /// 图漾粗略找合适的轮廓
        /// </summary>
        /// <param name="point_3d">三维点</param>
        public static void getContours(float[] point_3d)
        {
            GLB.TitleStr = "";
            int AREA = GLB.BUFW * GLB.BUFH;                //总面积
            var gray = GLB.MyFrame.Convert <Gray, Byte>(); //灰度化

            Emgu.CV.Cvb.CvBlobs        myBlobs = new Emgu.CV.Cvb.CvBlobs();
            Emgu.CV.Cvb.CvBlobDetector bd      = new Emgu.CV.Cvb.CvBlobDetector();
            uint n = bd.Detect(gray, myBlobs);//发现区块

            ////遍历各区块:
            for (uint i = 1; i <= myBlobs.Count; i++)
            {
                int         area   = myBlobs[i].Area;                                                  //获取面积
                RotatedRect rect   = CvInvoke.MinAreaRect(new VectorOfPoint(myBlobs[i].GetContour())); //最小矩形
                float       width  = rect.Size.Width;                                                  //像素宽
                float       height = rect.Size.Height;                                                 //像素长
                if (height < width)
                {
                    float temp = height;
                    height = width;
                    width  = temp;
                }
                float H2W = height / width;
                if (area > 0.02 * AREA && area < 0.75 * AREA && H2W > 1 && H2W < 2)//通过面积 长宽比 初略筛选
                {
                    if (getProduceInfo(myBlobs[i], point_3d) == true)
                    {
                        if (ProduceMacth() == true)//匹配成功
                        {
                            //////#########################################队列求均值--获取中心坐标#################################################
                            //GLB.avgCameraPoint3.Enqueue(new Point3(GLB.camera_device_point.X, GLB.camera_device_point.Y, GLB.camera_device_point.Z));
                            //if (GLB.avgCameraPoint3.Count > 5)
                            //{
                            //    GLB.avgCameraPoint3.Dequeue();
                            //}
                            //else
                            //{
                            //    return ;
                            //}
                            //GLB.camera_device_point.Z = (int)GLB.avgCameraPoint3.Average(o => o.Z);//中心点的深度//Z
                            //GLB.camera_device_point.Y = (int)GLB.avgCameraPoint3.Average(o => o.Y);//Y
                            //GLB.camera_device_point.X = (int)GLB.avgCameraPoint3.Average(o => o.X);//X
                            //RotatedRect boxCenter = new RotatedRect(new PointF((float )GLB.obj.jd.Average(o => o.X), (float)GLB.obj.jd.Average(o => o.Y)), new Size(8, 8), 0);
                            //CvInvoke.Ellipse(GLB.MyFrame, boxCenter, new MCvScalar(255, 0, 0), 4);//在中心画一个小圆
                            //CvInvoke.PutText(GLB.MyFrame, "x:" + (float)GLB.obj.jd.Average(o => o.X) + "y:" + (float)GLB.obj.jd.Average(o => o.Y) + "XC=" + GLB.obj.xCenter + "YC=" + GLB.obj.yCenter + "Depth=" + GLB.obj.Depth, new System.Drawing.Point((int)GLB.obj.jd.Average(o => o.X) - 176, (int)GLB.obj.jd.Average(o => o.Y) + 25), Emgu.CV.CvEnum.FontFace.HersheyDuplex, .75, new MCvScalar(255, 255, 255), 2);//深度显示

                            ////////队列求均值
                            //GLB.avgAngle.Enqueue((float)GLB.obj.Angle);
                            //if (GLB.avgAngle.Count > 5)
                            //{
                            //    GLB.avgAngle.Dequeue();
                            //}
                            //else
                            //{
                            //    return ;
                            //}
                            //GLB.obj.Angle = GLB.avgAngle.Average();//旋转角
                            //CvInvoke.PutText(GLB.MyFrame, "Angl=" + GLB.obj.Angle, new System.Drawing.Point((int)GLB.obj.jd[3].X, (int)GLB.obj.jd[3].Y), Emgu.CV.CvEnum.FontFace.HersheyDuplex, .75, new MCvScalar(0, 0, 255), 2);


                            ////#########################################坐标换算#################################################
                            GLB.robot_device_point.X = GLB.MatTuYangCam[0] * GLB.camera_device_point.X + GLB.MatTuYangCam[1] * GLB.camera_device_point.Y + GLB.MatTuYangCam[2];
                            GLB.robot_device_point.Y = GLB.MatTuYangCam[3] * GLB.camera_device_point.X + GLB.MatTuYangCam[4] * GLB.camera_device_point.Y + GLB.MatTuYangCam[5];
                            GLB.robot_device_point.Z = 2818 - GLB.camera_device_point.Z;
                            GLB.device_angl         += -2.6f;//相机与机器人夹角2.6度
                            GLB.device_angl          = (float)(GLB.device_angl * Math.PI / 180f);
                            //限定范围
                            if (GLB.robot_device_point.X < -600 || GLB.robot_device_point.X > 600 ||
                                GLB.robot_device_point.Y < -2200 || GLB.robot_device_point.Y > -800 ||
                                GLB.robot_device_point.Z < 280 || GLB.robot_device_point.Z > 1100)
                            {
                                GLB.Match_success = false;
                                GLB.TitleStr     += ",但是超出范围";
                            }
                            else
                            {
                                GLB.Match_success = true;
                            }
                        }
                        else
                        {
                            GLB.Match_success = false;
                        }
                    }
                }
            }
        }
예제 #7
0
파일: camera.cs 프로젝트: Bhitthal/Iota-2
        private void ProcessFrame(object sender, EventArgs arg)
        {
            Mat frame = new Mat();

            capture.Retrieve(frame, 0);
            Mat frame_crop = frame;
            Image <Hsv, Byte>  currenthsvFrame = (frame.ToImage <Bgr, Byte>()).Convert <Hsv, Byte>();
            Image <Gray, Byte> color_one       = new Image <Gray, Byte>(frame.Width, frame.Height);
            Image <Gray, Byte> color_two       = new Image <Gray, Byte>(frame.Width, frame.Height);
            Image <Gray, Byte> color_three     = new Image <Gray, Byte>(frame.Width, frame.Height);
            Image <Gray, Byte> color_four      = new Image <Gray, Byte>(frame.Width, frame.Height);


            /*
             * Color one is Red
             * Color two is Blue
             * Color three is Green
             * Color Four is Yellow
             * Green is in Right Index Finger
             * Blue is in Left Index Finger
             * Red in Right Thumb
             * Yelloe in Left Thumb
             */


            Hsv hsv_min_color_one = new Hsv(0, 135, 110);
            //Hsv hsv_max_color_one = new Hsv(6, 255, 255);
            Hsv hsv_max_color_one = new Hsv(8, 255, 255);
            Hsv hsv_min_color_two = new Hsv(112, 53, 10);
            Hsv hsv_max_color_two = new Hsv(119, 255, 255);

            /*
             * Hsv hsv_min_color_three = new Hsv(68, 59, 80);
             * Hsv hsv_max_color_three = new Hsv(85, 255, 255);
             * Hsv hsv_min_color_four = new Hsv(20, 165, 165);
             * Hsv hsv_max_color_four = new Hsv(36, 255, 255);
             */
            Hsv hsv_min_color_three = new Hsv(83, 109, 105);
            Hsv hsv_max_color_three = new Hsv(109, 255, 255);
            Hsv hsv_min_color_four  = new Hsv(18, 155, 155);
            Hsv hsv_max_color_four  = new Hsv(35, 255, 255);

            color_one   = currenthsvFrame.InRange(hsv_min_color_one, hsv_max_color_one);
            color_two   = currenthsvFrame.InRange(hsv_min_color_two, hsv_max_color_two);
            color_three = currenthsvFrame.InRange(hsv_min_color_three, hsv_max_color_three);
            color_four  = currenthsvFrame.InRange(hsv_min_color_four, hsv_max_color_four);



            //Blob detection
            #region Blob Detection

            //Color one detection
            Image <Bgr, Byte> smoothedFrame_cone = new Image <Bgr, byte>(currenthsvFrame.Size);
            CvInvoke.GaussianBlur(color_one, smoothedFrame_cone, new Size(3, 3), 1); //filter out noises

            Mat forgroundMask_cone = new Mat();
            fgDetector.Apply(smoothedFrame_cone, forgroundMask_cone);

            CvBlobs blobs_color_one = new CvBlobs();
            blobDetector.Detect(forgroundMask_cone.ToImage <Gray, byte>(), blobs_color_one);
            blobs_color_one.FilterByArea(minarea, maxarea);


            //Color two Blob Detection
            Image <Bgr, Byte> smoothedFrame_ctwo = new Image <Bgr, byte>(currenthsvFrame.Size);
            CvInvoke.GaussianBlur(color_two, smoothedFrame_ctwo, new Size(3, 3), 1); //filter out noises

            Mat forgroundMask_ctwo = new Mat();
            fgDetector.Apply(smoothedFrame_ctwo, forgroundMask_ctwo);

            CvBlobs blobs_color_two = new CvBlobs();
            blobDetector.Detect(forgroundMask_ctwo.ToImage <Gray, byte>(), blobs_color_two);
            blobs_color_two.FilterByArea(minarea, maxarea);


            //Color three blob detection
            Image <Bgr, Byte> smoothedFrame_cthree = new Image <Bgr, byte>(currenthsvFrame.Size);
            CvInvoke.GaussianBlur(color_three, smoothedFrame_cthree, new Size(3, 3), 1); //filter out noises

            Mat forgroundMask_cthree = new Mat();
            fgDetector.Apply(smoothedFrame_cthree, forgroundMask_cthree);

            CvBlobs blobs_color_three = new CvBlobs();
            blobDetector.Detect(forgroundMask_cthree.ToImage <Gray, byte>(), blobs_color_three);
            blobs_color_three.FilterByArea(minarea, maxarea);


            //Color four detection
            Image <Bgr, Byte> smoothedFrame_cfour = new Image <Bgr, byte>(currenthsvFrame.Size);
            CvInvoke.GaussianBlur(color_four, smoothedFrame_cfour, new Size(3, 3), 1); //filter out noises

            Mat forgroundMask_cfour = new Mat();
            fgDetector.Apply(smoothedFrame_cfour, forgroundMask_cfour);

            CvBlobs blobs_color_four = new CvBlobs();
            blobDetector.Detect(forgroundMask_cfour.ToImage <Gray, byte>(), blobs_color_four);
            blobs_color_four.FilterByArea(minarea, maxarea);

            //Makers Interpretition
            float[] cent_color_one   = new float[2];
            float[] cent_color_two   = new float[2];
            float[] cent_color_three = new float[2];
            float[] cent_color_four  = new float[2];
            //Centroids of Markers
            foreach (var pair in blobs_color_one)
            {
                CvBlob b = pair.Value;
                CvInvoke.Rectangle(frame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
                cent_color_one[0] = b.Centroid.X;
                cent_color_one[1] = b.Centroid.Y;
            }

            foreach (var pair in blobs_color_two)
            {
                CvBlob b = pair.Value;
                CvInvoke.Rectangle(frame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
                cent_color_two[0] = b.Centroid.X;
                cent_color_two[1] = b.Centroid.Y;
            }

            foreach (var pair in blobs_color_three)
            {
                CvBlob b = pair.Value;
                CvInvoke.Rectangle(frame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
                cent_color_three[0] = b.Centroid.X;
                cent_color_three[1] = b.Centroid.Y;
            }

            foreach (var pair in blobs_color_four)
            {
                CvBlob b = pair.Value;
                CvInvoke.Rectangle(frame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
                cent_color_four[0] = b.Centroid.X;
                cent_color_four[1] = b.Centroid.Y;
            }
            #endregion


            #region Calculation
            int   click_flag = 0;
            int[] x_cor      = new int[4];
            int[] y_cor      = new int[4];

            if (blobs_color_one.Count != 0 && blobs_color_two.Count != 0 && blobs_color_three.Count != 0 && blobs_color_four.Count != 0)
            {
                foreach (var pair in blobs_color_one)
                {
                    CvBlob b = pair.Value;
                    foreach (var pairr in blobs_color_two)
                    {
                        CvBlob c = pairr.Value;
                        if ((b.Centroid.X - c.Centroid.X) * (b.Centroid.X - c.Centroid.X) + (b.Centroid.Y - c.Centroid.Y) * (b.Centroid.Y - c.Centroid.Y) <= 5000)
                        {
                            click_flag = 1;
                            x_cor[0]   = ((int)b.Centroid.X);
                            x_cor[1]   = ((int)c.Centroid.X);
                            y_cor[0]   = ((int)b.Centroid.Y);
                            y_cor[1]   = ((int)c.Centroid.Y);
                            break;
                        }
                    }
                    if (click_flag == 1)
                    {
                        break;
                    }
                }
                if (click_flag == 1)
                {
                    click_flag = 0;
                    foreach (var pair in blobs_color_three)
                    {
                        CvBlob b = pair.Value;
                        foreach (var pairr in blobs_color_four)
                        {
                            CvBlob c = pairr.Value;
                            if ((b.Centroid.X - c.Centroid.X) * (b.Centroid.X - c.Centroid.X) + (b.Centroid.Y - c.Centroid.Y) * (b.Centroid.Y - c.Centroid.Y) <= 10000)
                            {
                                click_flag = 1;
                                x_cor[2]   = ((int)b.Centroid.X);
                                x_cor[3]   = ((int)c.Centroid.X);
                                y_cor[2]   = ((int)b.Centroid.Y);
                                y_cor[3]   = ((int)c.Centroid.Y);
                                break;
                            }
                        }
                        if (click_flag == 1)
                        {
                            break;
                        }
                    }
                }
            }

            if (click_flag == 1)
            {
                //MessageBox.Show("clicked");
                SoundPlayer simpleSound = new SoundPlayer(@"click_sound.wav");
                simpleSound.Play();

                Array.Sort(x_cor);
                Array.Sort(y_cor);
                Bitmap   ori_image  = frame_crop.ToImage <Bgr, Byte>().ToBitmap();
                Bitmap   crop_image = new Bitmap(x_cor[2] - x_cor[1], y_cor[2] - y_cor[1]);
                Graphics g          = Graphics.FromImage(crop_image);
                g.DrawImage(ori_image, -x_cor[1], -y_cor[1]);
                //string name = string.Format("SAP_{0:ddMMyyyy_hh_mm_ss}.jpg",DateTime.Now);
                frame.Save(@"C:\Users\Shubhankar\Pictures\Camera Roll\" + string.Format("SAP_{0:ddMMyyyy_hh_mm_ss}_original.jpg", DateTime.Now));
                crop_image.Save(@"C:\Users\Shubhankar\Pictures\Camera Roll\" + string.Format("SAP_{0:ddMMyyyy_hh_mm_ss}.jpg", DateTime.Now));
                Thread.Sleep(500);
            }
            #endregion

            #region Click Gesture


            #endregion

            captureImageBox.Image           = frame;
            grayscaleImageBox.Image         = color_one;
            smoothedGrayscaleImageBox.Image = color_two;
            cannyImageBox.Image             = color_three;
            Color4ImageBox.Image            = color_four;
        }
예제 #8
0
        void ProcessFrame(object sender, EventArgs e)
        {
            Mat frame         = _cameraCapture.QueryFrame();
            Mat smoothedFrame = new Mat();

            CvInvoke.GaussianBlur(frame, smoothedFrame, new Size(3, 3), 1); //filter out noises
                                                                            //frame._SmoothGaussian(3);

            #region use the BG/FG detector to find the forground mask

            Mat forgroundMask = new Mat();
            _fgDetector.Apply(smoothedFrame, forgroundMask);
            #endregion

            CvBlobs blobs = new CvBlobs();
            _blobDetector.Detect(forgroundMask.ToImage <Gray, byte>(), blobs);
            blobs.FilterByArea(100, int.MaxValue);

            float scale = (frame.Width + frame.Width) / 2.0f;
            _tracker.Update(blobs, 0.01 * scale, 5, 5);

            long detectionTime;

            List <Rectangle> faces = new List <Rectangle>();
            List <Rectangle> eyes  = new List <Rectangle>();

            IImage image = (IImage)frame;//这一步是重点
            faceImage = frame.Bitmap;
            DetectFace.Detect(image
                              , "haarcascade_frontalface_default.xml", "haarcascade_eye.xml",
                              faces, eyes,
                              out detectionTime);

            #region 多人识别 多人识别存在较大误差率(图片库 如果高清,识别效果就是好)
            //Graphics g1 = Graphics.FromImage(frame.Bitmap);
            //List<FaceIdentifyModel> tempList = new List<FaceIdentifyModel>();
            //foreach (Rectangle face in faces)
            //{
            //    Image rectImage1 = ImageHelper.CaptureImage(frame.Bitmap, face);
            //    FaceIdentifyModel MoreIdentifyInfo = FaceAPI.FaceIdentify(rectImage1, tb_Group.Text.Trim(), 1, 1);//人脸识别 一个人的识别效果比较好
            //    MoreIdentifyInfo.rect = face;
            //    tempList.Add(MoreIdentifyInfo);
            //}
            //Color color_of_pen1 = Color.Gray;
            //color_of_pen1 = Color.Yellow;
            //Pen pen1 = new Pen(color_of_pen1, 2.0f);

            //Font font1 = new Font("微软雅黑", 16, GraphicsUnit.Pixel);
            //SolidBrush drawBrush1 = new SolidBrush(Color.Yellow);


            //tb_Identify.Text = tempList.ToJson();
            //foreach (var t in tempList)
            //{
            //    g1.DrawRectangle(pen1, t.rect);

            //    if (t.result != null)
            //    {
            //        g1.DrawString(t.result[0].user_info.Replace(",", "\r\n"), font1, drawBrush1, new Point(t.rect.X + 20, t.rect.Y - 20));
            //    }

            //}
            #endregion

            #region 单人识别
            //单人 人脸识别 多人效果比较差
            foreach (Rectangle face in faces)
            {
                #region 采用画图,显示自己的文本框
                Graphics g = Graphics.FromImage(frame.Bitmap);

                ImageModel tempImage = new ImageModel();
                tempImage.Rect  = face;
                tempImage.Image = frame.Bitmap;

                //接口查询速度差
                //string faceInfo = FaceAPI.FaceDetect(ImageHelper.CaptureImage(frame.Bitmap, face));//人脸检测

                Image             rectImage    = ImageHelper.CaptureImage(frame.Bitmap, face);
                FaceIdentifyModel IdentifyInfo = FaceAPI.FaceIdentify(rectImage, tb_Group.Text.Trim(), 1, 1);//人脸识别 一个人的识别效果比较好                                                                                          // tb_Result.Text = faceInfo;
                tb_Identify.Text = IdentifyInfo.ToJson().ToString();
                //采用画板
                Color color_of_pen = Color.Gray;
                color_of_pen = Color.Yellow;
                Pen       pen  = new Pen(color_of_pen, 2.0f);
                Rectangle rect = face;

                g.DrawRectangle(pen, rect);
                Font       font      = new Font("微软雅黑", 16, GraphicsUnit.Pixel);
                SolidBrush drawBrush = new SolidBrush(Color.Yellow);


                if (IdentifyInfo != null)
                {
                    if (IdentifyInfo.result != null)
                    {
                        for (int i = 0; i < IdentifyInfo.result.Count; i++)
                        {
                            string faceInfo = "";
                            faceInfo = IdentifyInfo.result[i].user_info.Replace(",", "\r\n");
                            //显示用户信息
                            g.DrawString(faceInfo, font, drawBrush, new Point(face.X + 20, face.Y - 20));
                        }
                    }
                }

                //CvInvoke.Rectangle(frame, face, new MCvScalar(255.0, 255.0, 255.0), 2);
                //CvInvoke.PutText(frame, faceInfo, new Point(face.X + 20, face.Y - 20), FontFace.HersheyPlain, 1.0, new MCvScalar(255.0, 255.0, 255.0));

                // 保存原始截图
                //System.Drawing.Image ResourceImage = frame.Bitmap;
                //ResourceImage.Save(saveDir + saveFileName);



                //线程队列 保存人脸识别截图
                QueueHelper.WriteImage(tempImage);



                //t1 = new Thread(new ThreadStart(() =>
                //{
                //    faceInfo = FaceAPI.FaceDetect(ImageHelper.CaptureImage(frame.Bitmap, face));
                //    this.Invoke(new Action(() =>
                //    {

                //        g.DrawString(faceInfo, font, drawBrush, new Point(face.X + 20, face.Y - 20));
                //    }));

                //}));

                //t1.IsBackground = true;
                //t1.Start();


                #endregion
            }
            #endregion

            #region 视频调用原有的Open CV 不支持中文字

            //foreach (var pair in _tracker)
            //{
            //    CvTrack b = pair.Value;

            //    #region 视频中调用open CV 上直接画文本框
            //    CvInvoke.Rectangle(frame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
            //    CvInvoke.PutText(frame, "man,show", new Point((int)Math.Round(b.Centroid.X), (int)Math.Round(b.Centroid.Y)), FontFace.HersheyPlain, 1.0, new MCvScalar(255.0, 255.0, 255.0));
            //    if (b.BoundingBox.Width < 100 || b.BoundingBox.Height < 50)
            //    {

            //        continue;
            //    }
            //    #endregion

            //}
            #endregion

            imageBox1.Image = frame;
            imageBox2.Image = forgroundMask;
        }
예제 #9
0
        private void ProcessFrame(object sender, EventArgs arg)
        {
            Mat frame = new Mat();

            capture.Retrieve(frame, 0);
            Mat grayFrame = new Mat();

            CvInvoke.CvtColor(frame, grayFrame, ColorConversion.Bgr2Gray);
            //Mat smallGrayFrame = new Mat();
            //CvInvoke.PyrDown(grayFrame, smallGrayFrame);
            //Mat smoothedGrayFrame = new Mat();
            //CvInvoke.PyrUp(smallGrayFrame, smoothedGrayFrame);

            //Image<Gray, Byte> smallGrayFrame = grayFrame.PyrDown();
            //Image<Gray, Byte> smoothedGrayFrame = smallGrayFrame.PyrUp();
            //Mat cannyFrame = new Mat();
            //CvInvoke.Canny(smoothedGrayFrame, cannyFrame, 100, 60);

            //Image<Gray, Byte> cannyFrame = smoothedGrayFrame.Canny(100, 60);
            Image <Bgra, Byte> _frame     = frame.ToImage <Bgra, Byte>();
            Image <Gray, Byte> _grayFrame = grayFrame.ToImage <Gray, Byte>();

            Image <Gray, Byte>[] rgb_frame = _frame.Split();              //components of rgb image
            Image <Gray, Byte>   red_com   = rgb_frame[2] - _grayFrame;
            var red_bi = red_com.Convert <Gray, byte>().ThresholdBinary(new Gray(redThres), new Gray(255));
            Image <Gray, Byte> blue_com = rgb_frame[0] - _grayFrame;
            var blue_bi = blue_com.Convert <Gray, byte>().ThresholdBinary(new Gray(blueThres), new Gray(255));
            Image <Gray, Byte> green_com = rgb_frame[1] - _grayFrame;
            var green_bi = green_com.Convert <Gray, byte>().ThresholdBinary(new Gray(greenThres), new Gray(255));
            //System.Windows.Forms.MessageBox.Show("");



            ///////////////////////////////////////////////////////////////////////////////////
            //Blob detection

            //Red Blob detection
            Image <Bgr, Byte> smoothedFrame_r = new Image <Bgr, byte>(red_com.Size);

            CvInvoke.GaussianBlur(red_bi, smoothedFrame_r, new Size(3, 3), 1); //filter out noises

            Mat forgroundMask_r = new Mat();

            fgDetector.Apply(smoothedFrame_r, forgroundMask_r);

            CvBlobs blobs_r = new CvBlobs();

            blobDetector.Detect(forgroundMask_r.ToImage <Gray, byte>(), blobs_r);
            blobs_r.FilterByArea(minarea, maxarea);


            //blue Blob Detection
            Image <Bgr, Byte> smoothedFrame_b = new Image <Bgr, byte>(red_com.Size);

            CvInvoke.GaussianBlur(blue_bi, smoothedFrame_b, new Size(3, 3), 1); //filter out noises

            Mat forgroundMask_b = new Mat();

            fgDetector.Apply(smoothedFrame_b, forgroundMask_b);

            CvBlobs blobs_b = new CvBlobs();

            blobDetector.Detect(forgroundMask_b.ToImage <Gray, byte>(), blobs_b);
            blobs_b.FilterByArea(minarea, maxarea);


            //Green blob detection
            Image <Bgr, Byte> smoothedFrame_g = new Image <Bgr, byte>(red_com.Size);

            CvInvoke.GaussianBlur(green_bi, smoothedFrame_g, new Size(3, 3), 1); //filter out noises

            Mat forgroundMask_g = new Mat();

            fgDetector.Apply(smoothedFrame_g, forgroundMask_g);

            CvBlobs blobs_g = new CvBlobs();

            blobDetector.Detect(forgroundMask_g.ToImage <Gray, byte>(), blobs_g);
            blobs_g.FilterByArea(minarea, maxarea);


            //Mouse Interpretition
            float[] cent_r = new float[2];
            float[] cent_g = new float[2];
            float[] cent_b = new float[2];
            //Corsor control with Green Marker
            foreach (var pair in blobs_g)
            {
                CvBlob b = pair.Value;
                CvInvoke.Rectangle(frame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
                cent_g[0] = b.Centroid.X;
                cent_g[1] = b.Centroid.Y;
            }
            if (blobs_g.Count == 1 || mouseflag != 0)
            {
                //Cursor Movement Controlled
                //Primary Screem
                //if (Screen.AllScreens.Length == 1)
                {
                    Cursor.Position = new Point(Screen.PrimaryScreen.Bounds.Width - (int)(cursor_mul * (int)cent_g[0] * Screen.PrimaryScreen.Bounds.Width / capture.Width), (int)(cursor_mul * (int)cent_g[1]) * Screen.PrimaryScreen.Bounds.Height / capture.Height);
                }
                //Secondary Screen
                //Cursor.Position = new Point((int)(cursor_mul * (int)cent_g[0] * Screen.AllScreens[1].Bounds.Width / capture.Width), (int)(cursor_mul * (int)cent_g[1]) * Screen.AllScreens[1].Bounds.Height / capture.Height);
                //Number of Screen = 2 and both a same time

                /*   if (Screen.AllScreens.Length == 2)
                 * {
                 *
                 *     Cursor.Position = new Point((int)(cursor_mul * (int)cent_g[0] * (Screen.AllScreens[1].Bounds.Width + Screen.AllScreens[0].Bounds.Width) / capture.Width),
                 *                             (int)(cursor_mul * (int)cent_g[1]) * (Screen.AllScreens[1].Bounds.Height + Screen.AllScreens[0].Bounds.Height) / capture.Height);
                 * }
                 * //Number of screen =3 and all at same time
                 * if (Screen.AllScreens.Length == 3)
                 * {
                 *
                 *     Cursor.Position = new Point((int)(cursor_mul * (int)cent_g[0] * (Screen.AllScreens[1].Bounds.Width + Screen.AllScreens[0].Bounds.Width + Screen.AllScreens[2].Bounds.Width) / capture.Width),
                 *                             (int)(cursor_mul * (int)cent_g[1]) * (Screen.AllScreens[1].Bounds.Height + Screen.AllScreens[0].Bounds.Height + Screen.AllScreens[0].Bounds.Height) / capture.Height);
                 * }
                 */

                /*
                 * //Check for Clicks
                 * if (blobs_r.Count == 1)
                 * {
                 *  if(blobs_g.Count == 0)
                 *  {
                 *      if(ccount == 1)
                 *      {
                 *          //double click
                 *          mouse_event(MOUSEEVENTF_LEFTDOWN, (int)cent_g[0], (int)cent_g[1], 0, 0);
                 *          mouse_event(MOUSEEVENTF_LEFTUP, (int)cent_g[0], (int)cent_g[1], 0, 0);
                 *          Thread.Sleep(150);
                 *          mouse_event(MOUSEEVENTF_LEFTDOWN, (int)cent_g[0], (int)cent_g[1], 0, 0);
                 *          mouse_event(MOUSEEVENTF_LEFTUP, (int)cent_g[0], (int)cent_g[1], 0, 0);
                 *      }
                 *      else
                 *      {
                 *          ccount--;
                 *      }
                 *  }
                 *
                 *  else if ((cent_g[0] - cent_r[0] >= 10 || cent_r[0] - cent_g[0] <= 10) && (cent_g[1] - cent_r[1] >= 10 || cent_r[1] - cent_g[1] <= 10))
                 *  {
                 *      ccount = safevalue;
                 *      mouseflag = 1;
                 *      //single click
                 *      mouse_event(MOUSEEVENTF_LEFTDOWN, (int)cent_g[0], (int)cent_g[1], 0, 0);
                 *      mouse_event(MOUSEEVENTF_LEFTUP, (int)cent_g[0], (int)cent_g[1], 0, 0);
                 *  }
                 * }
                 * else
                 * {
                 *  ccount = 0;
                 *
                 * }
                 *
                 * }
                 *
                 * if (blobs_b.Count == 1)
                 * {
                 *  foreach (var pair in blobs_b)
                 *  {
                 *      CvBlob b = pair.Value;
                 *      CvInvoke.Rectangle(frame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
                 *      cent_b[0] = b.Centroid.X;
                 *      cent_b[1] = b.Centroid.Y;
                 *  }
                 *
                 *   if (blobs_g.Count == 1 && (cent_g[0] - cent_b[0] >= 10 || cent_b[0] - cent_g[0] <= 10) && (cent_g[1] - cent_b[1] >= 10 || cent_b[1] - cent_g[1] <= 10))
                 *  {
                 *      //right click
                 *      mouse_event(MOUSEEVENTF_RIGHTDOWN, (int)cent_g[0], (int)cent_g[1], 0, 0);
                 *      mouse_event(MOUSEEVENTF_RIGHTUP, (int)cent_g[0], (int)cent_g[1], 0, 0);
                 *  }
                 *
                 *  else if(blobs_g.Count == 0)
                 *  {
                 *      mouse_event(MOUSEEVENTF_VWHEEL, 0, 0, (scroll_y - (int)cent_b[1]) * scroll_mul_v, 0);
                 *      mouse_event(MOUSEEVENTF_HWHEEL, 0, 0, (scroll_x - (int)cent_b[0]) * scroll_mul_h, 0);
                 *      scroll_y = (int)cent_b[1];
                 *      scroll_x = (int)cent_b[0];
                 *
                 *  }
                 */
            }


            captureImageBox.Image           = frame;
            grayscaleImageBox.Image         = red_bi;
            smoothedGrayscaleImageBox.Image = green_bi;
            cannyImageBox.Image             = blue_bi;
        }
예제 #10
0
        public CameraTrackingFindSubjectsReturnModel FindSubjects()
        {
            double    largestW     = 0;
            double    largestH     = 0;
            double    centerX      = 0;
            double    centerY      = 0;
            bool      foundSubject = false;
            Rectangle subject      = new Rectangle();

            // get detection 'blobs' or regions

            CvBlobs blobs = new CvBlobs();

            _blobDetector.Detect(this.lastMask.ToImage <Gray, byte>(), blobs);
            blobs.FilterByArea(100, int.MaxValue);

            float scale = (this.lastFrame.Width + this.lastFrame.Width) / 2.0f;

            _tracker.Update(blobs, 0.01 * scale, 5, 5);

            FrameWidth  = this.lastFrame.Width;
            FrameHeight = this.lastFrame.Height;

            foreach (var pair in _tracker)
            {
                CvTrack b = pair.Value;

                // limit the largest and smallest size boxes we care about.

                if (b.BoundingBox.Width < (this.lastFrame.Width / this.smallestDetectionWidthSizeDivisor) ||
                    b.BoundingBox.Height < (this.lastFrame.Height / this.smallestDetectionHeightSizeDivisor) ||
                    (b.BoundingBox.Width > (this.lastFrame.Width / this.largestDetectionWidthSizeDivisor) &&
                     b.BoundingBox.Height > (this.lastFrame.Height / this.largestDetectionHeightSizeDivisor)))
                {
                    continue;
                }

                // keep track of the largest regions as we only care to track the largest

                if (b.BoundingBox.Width > largestW)
                {
                    subject  = b.BoundingBox;
                    largestW = b.BoundingBox.Width;
                    largestH = b.BoundingBox.Height;
                    centerX  = b.Centroid.X;
                    centerY  = b.Centroid.Y;

                    CvInvoke.Rectangle(
                        this.lastFrame,
                        b.BoundingBox,
                        new MCvScalar(
                            255.0,
                            255.0,
                            255.0),
                        20);

                    CvInvoke.PutText(
                        this.lastFrame,
                        b.Id.ToString(),
                        new Point(
                            (int)Math.Round(b.Centroid.X),
                            (int)Math.Round(b.Centroid.Y)),
                        FontFace.HersheyPlain,
                        1.0,
                        new MCvScalar(255.0, 255.0, 255.0));

                    foundSubject = true;
                }
                else
                {
                    CvInvoke.Rectangle(
                        this.lastFrame,
                        b.BoundingBox,
                        new MCvScalar(
                            255.0,
                            255.0,
                            255.0),
                        1);

                    CvInvoke.PutText(
                        this.lastFrame,
                        b.Id.ToString(),
                        new Point(
                            (int)Math.Round(b.Centroid.X),
                            (int)Math.Round(b.Centroid.Y)),
                        FontFace.HersheyPlain,
                        1.0,
                        new MCvScalar(
                            255.0,
                            255.0,
                            255.0));
                }
            }

            return(new CameraTrackingFindSubjectsReturnModel()
            {
                CenterX = centerX,
                CenterY = centerY,
                BoundingBox = subject,
                FoundSubject = foundSubject
            });
        }
예제 #11
0
파일: gesture.cs 프로젝트: Bhitthal/Iota-2
        private void ExtractBlobAndCrop(Image <Gray, byte> skin)
        {
            using (MemStorage storage = new MemStorage())
            {
                Image <Gray, Byte> smoothedFrame = new Image <Gray, byte>(skin.Size);
                CvInvoke.GaussianBlur(skin, smoothedFrame, new Size(3, 3), 1); //filter out noises


                imageBoxFrameGrabber.Image = skin;


                Mat forgroundMask = new Mat();
                Mat ss            = new Mat();
                ss = skin.Mat;
                //grabber.Retrieve(ss);
                fgDetector.Apply(ss, forgroundMask);

                //imageBox1.Image = forgroundMask;

                CvBlobs blobs = new CvBlobs();
                //blobDetector.Detect(forgroundMask.ToImage<Gray, byte>(), blobs);
                blobDetector.Detect(skin, blobs);
                blobs.FilterByArea(30000, 150000);

                CvBlob b = null;
                CvBlob btemp;
                int    area = 0;
                foreach (var pair in blobs)
                {
                    btemp = pair.Value;
                    if (area < btemp.Area)
                    {
                        b    = pair.Value;
                        area = btemp.Area;
                    }
                }

                //Crop LArgest Blob
                Bitmap skin_bit = skin.ToBitmap();


                //MessageBox.Show("" + area);
                if (area != 0)
                {
                    CvInvoke.Rectangle(currentFrame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
                    //Rectangle rec = new Rectangle(b.BoundingBox.X, b.BoundingBox.Y, b.BoundingBox.Width, b.BoundingBox.Height);

                    Bitmap crop_image = new Bitmap((b.BoundingBox.Width > b.BoundingBox.Height ? b.BoundingBox.Width : b.BoundingBox.Height), (b.BoundingBox.Width > b.BoundingBox.Height ? b.BoundingBox.Width : b.BoundingBox.Height));
                    //Bitmap crop_image = skin_bit.Clone(rec, skin_bit.PixelFormat);
                    Graphics g = Graphics.FromImage(crop_image);
                    g.DrawImage(skin_bit, -b.BoundingBox.X, -b.BoundingBox.Y);
                    //g.DrawImage(skin_bit, -50, -50);


                    croped  = new Image <Gray, Byte>(crop_image).Resize(350, 350, Inter.Cubic);
                    croped1 = new Image <Gray, Byte>(crop_image).Resize(100, 100, Inter.Cubic);
                    croped2 = new Image <Gray, Byte>(crop_image).Resize(50, 50, Inter.Cubic);


                    int gesture_number = fow_prop.image(croped2);
                    label1.Text = "" + gesture_number;


                    imageBoxSkin.Image = croped;
                    crop_image.Dispose();
                    skin_bit.Dispose();
                }
            }
        }
예제 #12
0
파일: mouse.cs 프로젝트: Bhitthal/Iota-2
        private void ProcessFrame(object sender, EventArgs arg)
        {
            Mat frame = new Mat();

            capture.Retrieve(frame, 0);
            Image <Hsv, Byte>  currenthsvFrame = (frame.ToImage <Bgr, Byte>()).Convert <Hsv, Byte>();
            Image <Gray, Byte> color_one       = new Image <Gray, Byte>(frame.Width, frame.Height);
            Image <Gray, Byte> color_two       = new Image <Gray, Byte>(frame.Width, frame.Height);
            Image <Gray, Byte> color_three     = new Image <Gray, Byte>(frame.Width, frame.Height);
            Image <Gray, Byte> color_four      = new Image <Gray, Byte>(frame.Width, frame.Height);


            /*
             * Color one is Red
             * Color two is Blue
             * Color three is Green
             * Color Four is Yellow
             * Green is in Right Index Finger
             * Blue is in Left Index Finger
             * Red in Right Thumb
             * Yelloe in Left Thumb
             */


            /* Hsv hsv_min_color_one = new Hsv(0, 135, 110);
             * Hsv hsv_max_color_one = new Hsv(6, 255, 255);
             * Hsv hsv_min_color_two = new Hsv(112, 53, 10);
             * Hsv hsv_max_color_two = new Hsv(119, 255, 255);
             * Hsv hsv_min_color_three = new Hsv(68, 59, 80);
             * Hsv hsv_max_color_three = new Hsv(85, 255, 255);
             * Hsv hsv_min_color_four = new Hsv(20, 165, 165);
             * Hsv hsv_max_color_four = new Hsv(36, 255, 255);*/
            Hsv hsv_min_color_one = new Hsv(0, 135, 50);
            //Hsv hsv_max_color_one = new Hsv(6, 255, 255);
            Hsv hsv_max_color_one = new Hsv(8, 255, 255);
            Hsv hsv_min_color_two = new Hsv(112, 53, 10);
            Hsv hsv_max_color_two = new Hsv(119, 255, 255);

            /*
             * Hsv hsv_min_color_three = new Hsv(68, 59, 80);
             * Hsv hsv_max_color_three = new Hsv(85, 255, 255);
             * Hsv hsv_min_color_four = new Hsv(20, 165, 165);
             * Hsv hsv_max_color_four = new Hsv(36, 255, 255);
             */
            Hsv hsv_min_color_three = new Hsv(65, 70, 0);
            Hsv hsv_max_color_three = new Hsv(109, 255, 255);
            Hsv hsv_min_color_four  = new Hsv(18, 155, 155);
            Hsv hsv_max_color_four  = new Hsv(35, 255, 255);



            color_one   = currenthsvFrame.InRange(hsv_min_color_one, hsv_max_color_one);
            color_two   = currenthsvFrame.InRange(hsv_min_color_two, hsv_max_color_two);
            color_three = currenthsvFrame.InRange(hsv_min_color_three, hsv_max_color_three);
            color_four  = currenthsvFrame.InRange(hsv_min_color_four, hsv_max_color_four);



            //Blob detection
            #region Blob Detection

            //Color one detection
            Image <Bgr, Byte> smoothedFrame_cone = new Image <Bgr, byte>(currenthsvFrame.Size);
            CvInvoke.GaussianBlur(color_one, smoothedFrame_cone, new Size(3, 3), 1); //filter out noises

            Mat forgroundMask_cone = new Mat();
            fgDetector.Apply(smoothedFrame_cone, forgroundMask_cone);

            CvBlobs blobs_color_one = new CvBlobs();
            blobDetector.Detect(forgroundMask_cone.ToImage <Gray, byte>(), blobs_color_one);
            blobs_color_one.FilterByArea(minarea, maxarea);


            //Color two Blob Detection
            Image <Bgr, Byte> smoothedFrame_ctwo = new Image <Bgr, byte>(currenthsvFrame.Size);
            CvInvoke.GaussianBlur(color_two, smoothedFrame_ctwo, new Size(3, 3), 1); //filter out noises

            Mat forgroundMask_ctwo = new Mat();
            fgDetector.Apply(smoothedFrame_ctwo, forgroundMask_ctwo);

            CvBlobs blobs_color_two = new CvBlobs();
            blobDetector.Detect(forgroundMask_ctwo.ToImage <Gray, byte>(), blobs_color_two);
            blobs_color_two.FilterByArea(minarea, maxarea);


            //Color three blob detection
            Image <Bgr, Byte> smoothedFrame_cthree = new Image <Bgr, byte>(currenthsvFrame.Size);
            CvInvoke.GaussianBlur(color_three, smoothedFrame_cthree, new Size(3, 3), 1); //filter out noises

            Mat forgroundMask_cthree = new Mat();
            fgDetector.Apply(smoothedFrame_cthree, forgroundMask_cthree);

            CvBlobs blobs_color_three = new CvBlobs();
            blobDetector.Detect(forgroundMask_cthree.ToImage <Gray, byte>(), blobs_color_three);
            blobs_color_three.FilterByArea(minarea, maxarea);


            //Color four detection
            Image <Bgr, Byte> smoothedFrame_cfour = new Image <Bgr, byte>(currenthsvFrame.Size);
            CvInvoke.GaussianBlur(color_four, smoothedFrame_cfour, new Size(3, 3), 1); //filter out noises

            Mat forgroundMask_cfour = new Mat();
            fgDetector.Apply(smoothedFrame_cfour, forgroundMask_cfour);

            CvBlobs blobs_color_four = new CvBlobs();
            blobDetector.Detect(forgroundMask_cfour.ToImage <Gray, byte>(), blobs_color_four);
            blobs_color_four.FilterByArea(minarea, maxarea);

            #endregion

            //Makers Interpretition
            float[] cent_color_one   = new float[2];
            float[] cent_color_two   = new float[2];
            float[] cent_color_three = new float[2];
            float[] cent_color_four  = new float[2];

            cent_color_one[0]   = 0;
            cent_color_one[1]   = 0;
            cent_color_two[0]   = 0;
            cent_color_two[1]   = 0;
            cent_color_three[0] = green_history_x;
            cent_color_three[1] = green_history_y;
            cent_color_four[0]  = 0;
            cent_color_four[1]  = 0;

            //Corsor control with Green Marker

            if (blobs_color_three.Count == 1 || mouseflag != 0)
            {
                foreach (var pair in blobs_color_three)
                {
                    CvBlob b = pair.Value;
                    CvInvoke.Rectangle(frame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
                    cursor_history_x.Enqueue((int)b.Centroid.X);
                    cursor_history_y.Enqueue((int)b.Centroid.Y);

                    cursor_history_x.Dequeue();
                    cursor_history_y.Dequeue();

                    cent_color_three[0] = (int)b.Centroid.X;
                    cent_color_three[1] = (int)b.Centroid.Y;

                    /*int temp_sum = 0;
                     * int[] temp = cursor_history_x.ToArray();
                     * for (int i = 0; i < queue_cursor_length; i++)
                     *  temp_sum += temp[i];
                     * cent_color_three[0] = temp_sum / queue_cursor_length;
                     *
                     * temp_sum = 0;
                     * temp = cursor_history_y.ToArray();
                     * for (int i = 0; i < queue_cursor_length; i++)
                     *  temp_sum += temp[i];
                     * cent_color_three[1] = temp_sum / queue_cursor_length;
                     *
                     * green_history_x = (int)cent_color_three[0];
                     * green_history_y = (int)cent_color_three[1];*/
                }

                //Cursor Movement Controlled
                //Primary Screem
                // if (Screen.AllScreens.Length == 1)
                {
                    //Cursor.Position = new Point(Screen.PrimaryScreen.Bounds.Width - (int)(cursor_mul * (int)cent_color_three[0] * Screen.PrimaryScreen.Bounds.Width / capture.Width), (int)(cursor_mul * (int)cent_color_three[1]) * Screen.PrimaryScreen.Bounds.Height / capture.Height);
                    Cursor.Position = new Point((int)((cursor_mul_x * (int)cent_color_three[0]) * (Screen.PrimaryScreen.Bounds.Width) / capture.Width) + cursor_add_x, (((int)cursor_mul_y * (int)cent_color_three[1]) * Screen.PrimaryScreen.Bounds.Height / capture.Height) + cursor_add_y);
                    //mouse_event(MOUSEEVENTF_MOVE, ( (-(int)cent_color_three[0] + green_history_x)), ( (-(int)cent_color_three[1] + green_history_y)),0,0);
                    //mouse_event(MOUSEEVENTF_ABSOLUTE, 0, 0, 0, 0);
                }
                //Secondary Screen
                //Cursor.Position = new Point((int)(cursor_mul * (int)cent_color_three[0] * Screen.AllScreens[1].Bounds.Width / capture.Width), (int)(cursor_mul * (int)cent_color_three[1]) * Screen.AllScreens[1].Bounds.Height / capture.Height);
                //Number of Screen = 2 and both a same time

                /*      if (Screen.AllScreens.Length == 2)
                 *   {
                 *
                 *       Cursor.Position = new Point((int)(cursor_mul * (int)cent_color_three[0] * (Screen.AllScreens[1].Bounds.Width + Screen.AllScreens[0].Bounds.Width) / capture.Width),
                 *                               (int)(cursor_mul * (int)cent_color_three[1]) * (Screen.AllScreens[1].Bounds.Height + Screen.AllScreens[0].Bounds.Height) / capture.Height);
                 *   }
                 *   //Number of screen =3 and all at same time
                 *   if (Screen.AllScreens.Length == 3)
                 *   {
                 *
                 *       Cursor.Position = new Point((int)(cursor_mul * (int)cent_color_three[0] * (Screen.AllScreens[1].Bounds.Width + Screen.AllScreens[0].Bounds.Width + Screen.AllScreens[2].Bounds.Width) / capture.Width),
                 *                               (int)(cursor_mul * (int)cent_color_three[1]) * (Screen.AllScreens[1].Bounds.Height + Screen.AllScreens[0].Bounds.Height + Screen.AllScreens[0].Bounds.Height) / capture.Height);
                 *   }
                 */


                //Check for Clicks
                if (blobs_color_one.Count == 1)
                {
                    foreach (var pair in blobs_color_one)
                    {
                        CvBlob b = pair.Value;
                        CvInvoke.Rectangle(frame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
                        cent_color_one[0] = b.Centroid.X;
                        cent_color_one[1] = b.Centroid.Y;
                    }
                    if (blobs_color_three.Count == 0)
                    {
                        if (ccount == 1)
                        {
                            //double click
                            mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, (uint)cent_color_three[0], (uint)cent_color_three[1], 0, 0);
                            mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP, (uint)cent_color_three[0], (uint)cent_color_three[1], 0, 0);
                            Thread.Sleep(150);
                            mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, (uint)cent_color_three[0], (uint)cent_color_three[1], 0, 0);
                            mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP, (uint)cent_color_three[0], (uint)cent_color_three[1], 0, 0);
                        }
                        else
                        {
                            ccount--;
                        }
                    }

                    else if ((cent_color_one[0] - cent_color_three[0]) * (cent_color_one[0] - cent_color_three[0]) + (cent_color_one[1] - cent_color_three[1]) * (cent_color_one[1] - cent_color_three[1]) <= 5000)
                    {
                        ccount    = safevalue;
                        mouseflag = 1;
                        //single click
                        mouse_event(MOUSEEVENTF_LEFTDOWN, (uint)cent_color_three[0], (uint)cent_color_three[1], 0, 0);
                        mouse_event(MOUSEEVENTF_LEFTUP, (uint)cent_color_three[0], (uint)cent_color_three[1], 0, 0);
                        mouse_event(MOUSEEVENTF_ABSOLUTE, 0, 0, 0, 0);
                    }
                }
                else
                {
                    ccount = 0;
                }
            }


            if (blobs_color_two.Count == 1)
            {
                foreach (var pair in blobs_color_two)
                {
                    CvBlob b = pair.Value;
                    CvInvoke.Rectangle(frame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
                    cent_color_two[0] = b.Centroid.X;
                    cent_color_two[1] = b.Centroid.Y;
                }

                if (blobs_color_three.Count == 1 && ((cent_color_three[0] - cent_color_two[0]) * (cent_color_three[0] - cent_color_two[0]) + (cent_color_three[1] - cent_color_two[1]) * (cent_color_three[1] - cent_color_two[1]) <= 5000))
                {
                    //right click
                    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTDOWN, (uint)cent_color_three[0], (uint)cent_color_three[1], 0, 0);
                    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTUP, (uint)cent_color_three[0], (uint)cent_color_three[1], 0, 0);
                }

                else //if(blobs_g.Count == 0)
                {
                    //MessageBox.Show("d");
                    //Cursor.Position = new Point(Screen.PrimaryScreen.Bounds.Width - (int)(cursor_mul * green_history_x * Screen.PrimaryScreen.Bounds.Width / capture.Width), (int)(cursor_mul * green_history_y) * Screen.PrimaryScreen.Bounds.Height / capture.Height);
                    //mouse_event(MOUSEEVENTF_VWHEEL, 0, 0, (scroll_y - (int)cent_color_two[1]) * scroll_mul_v, 0);
                    mouse_event(MOUSEEVENTF_HWHEEL, 0, 0, (uint)((scroll_x - (int)cent_color_two[0]) * scroll_mul_h), 0);
                    mouse_event(MOUSEEVENTF_VWHEEL, (uint)Cursor.Position.X, (uint)Cursor.Position.Y, 50, 0);
                    //mouse_event(MOUSEEVENTF_HWHEEL, 0, 0, 50, 0);
                    scroll_y = (int)cent_color_two[1];
                    scroll_x = (int)cent_color_two[0];
                }
            }


            captureImageBox.Image           = frame;
            grayscaleImageBox.Image         = color_one;
            smoothedGrayscaleImageBox.Image = color_two;
            cannyImageBox.Image             = color_three;
            Color4ImageBox.Image            = color_four;
        }
예제 #13
0
        public static WriteableBitmap DrawBlobBoundingBoxsCV(WriteableBitmap writeableBitmap, WriteableBitmap gradientBitmapRef, WriteableBitmap realBitmapRef)
        {
            Bitmap normalBitmap = BitmapFromWriteableBitmap(writeableBitmap);
               var cvImage = new Image<Gray, byte>(new Bitmap(normalBitmap));

               //var classifications = ClassifyBitmap( writeableBitmap, cvImage );
               if (cvImage != null)
               {
               // This takes our nice looking color png and converts it to black and white
               Image<Gray, byte> greyImg = cvImage.Convert<Gray, byte>();

               // We again threshold it based on brightness...BUT WE INVERT THE PNG. BLOB DETECTOR DETECTS WHITE NOT BLACK
               // this will esentially eliminate the color differences
               // you could also do cool things like threshold only certain colors here for a color based blob detector
               Image<Gray, Byte> greyThreshImg = greyImg.ThresholdBinaryInv(new Gray(150), new Gray(255));

               Emgu.CV.Cvb.CvBlobs resultingImgBlobs = new Emgu.CV.Cvb.CvBlobs();
               Emgu.CV.Cvb.CvBlobDetector bDetect = new Emgu.CV.Cvb.CvBlobDetector();
               uint numWebcamBlobsFound = bDetect.Detect(greyThreshImg, resultingImgBlobs);

               // This is a simple way of just drawing all blobs reguardless of their size and not iterating through them
               // It draws on top of whatever you input. I am inputting the threshold image. Specifying an alpha to draw with of 0.5 so its half transparent.
               //Emgu.CV.Image<Bgr, byte> blobImg = bDetect.DrawBlobs(webcamThreshImg, resultingWebcamBlobs, Emgu.CV.Cvb.CvBlobDetector.BlobRenderType.Default, 0.5);

               // Here we can iterate through each blob and use the slider to set a threshold then draw a red box around it
               Image<Rgb, byte> blobImg = greyThreshImg.Convert<Rgb, byte>();
               Rgb red = new Rgb(255, 0, 0);

               int blobNumber = 0;

               // Lets try and iterate the blobs?
               foreach (Emgu.CV.Cvb.CvBlob targetBlob in resultingImgBlobs.Values)
               {
                   int imageArea = blobImg.Width*blobImg.Height;
                   int blobArea = targetBlob.Area;
                   int blobBoundingBoxArea = targetBlob.BoundingBox.Width*targetBlob.BoundingBox.Height;

                   // Only use blobs with area greater than some threshold
                   // If the blob bounding rect is basically size of the whole image ignore it for now
                   if (blobArea > 200.0 && blobBoundingBoxArea < (imageArea*0.99))
                   {
                       Rectangle rectangle = targetBlob.BoundingBox;

                       int CentroidX = (int)targetBlob.Centroid.X;
                       int CentroidY = (int)targetBlob.Centroid.Y;

                       int croppedWidth = rectangle.Width + 50;
                       int croppedHeight = rectangle.Height + 50;

                       int CroppedX = CentroidX - (int)(croppedWidth / 2.0);
                       int CroppedY = CentroidY - (int)(croppedHeight / 2.0);

                       var croppedBlobBitmap = writeableBitmap.Crop(CroppedX, CroppedY, croppedWidth, croppedHeight);
                       var croppedGradientBlobBitmap = gradientBitmapRef.Crop(CroppedX, CroppedY, croppedWidth, croppedHeight);
                       var croppedRealBitmapRef = realBitmapRef.Crop(CroppedX, CroppedY, croppedWidth, croppedHeight);

                       double blobAngle = -RadianToDegree(CalculateBlobAngle(targetBlob));

                       CroppedX = (int) (croppedWidth/2.0);
                       CroppedY = (int) (croppedHeight/2.0);

                       var rotatedandCroppedBlobBitmap = RotateWriteableBitmap(croppedBlobBitmap, blobAngle);
                       var rotatedGradientBlobBitmap = RotateColorWriteableBitmap(croppedGradientBlobBitmap, blobAngle);
                       var rotatedRealBitmapRef = RotateColorWriteableBitmap(croppedRealBitmapRef, blobAngle);

                       var refinedBitmap = DrawBlobBoundingBoxsAroundCroppedBitmap(rotatedandCroppedBlobBitmap, rotatedGradientBlobBitmap, rotatedRealBitmapRef, 1);
                       rotatedGradientBlobBitmap = DrawBlobBoundingBoxsAroundCroppedBitmap(rotatedandCroppedBlobBitmap, rotatedGradientBlobBitmap, rotatedRealBitmapRef, 2);
                       rotatedRealBitmapRef = DrawBlobBoundingBoxsAroundCroppedBitmap(rotatedandCroppedBlobBitmap, rotatedGradientBlobBitmap, rotatedRealBitmapRef, 3);

                       var areaCheck = refinedBitmap.PixelHeight*refinedBitmap.PixelWidth;

                       if (areaCheck >= 200)
                       {
                           blobNumber++;

                           var thresholded = refinedBitmap.Clone();
                           ThresholdBitmap(thresholded, 10, false);
                           BitmapColorer.ColorBitmap(thresholded);

                           System.Windows.Media.Color blobColor = PixelColorOfCentralBlob(thresholded);
                           BitmapColorer.EraseAllButCertainColorandWhite(thresholded, blobColor);

                           var shouldFlip = shouldFlipThresholdedBitmap(thresholded, blobColor);

                           if (shouldFlip)
                           {
                               thresholded = thresholded.Rotate(180);
                               rotatedGradientBlobBitmap = rotatedGradientBlobBitmap.Rotate(180);
                               rotatedRealBitmapRef = rotatedRealBitmapRef.Rotate(180);
                           }

                           var orientedBitmap = NormalizeBitmapSize(thresholded);
                           var orientedGradientBitmap = NormalizeBitmapSize(rotatedGradientBlobBitmap);
                           var orientedRealBitmap = NormalizeBitmapSize(rotatedRealBitmapRef);

                           ApplyBlobMaskToOtherBitmaps(orientedBitmap, orientedGradientBitmap, orientedRealBitmap, blobColor);

                           string fileName1 = saveDirectory + "\\croppedBlob" + blobNumber + ".png";

                           ExtensionMethods.Save(orientedRealBitmap, fileName1);
                       }
                   }
               }
               }

               return writeableBitmap;
        }
예제 #14
0
        public static WriteableBitmap DrawBlobBoundingBoxsAroundCroppedBitmap(WriteableBitmap writeableBitmap,
           WriteableBitmap gradientBitmapRef, WriteableBitmap realBitmapRef, int returnBitmapIndex)
        {
            Bitmap normalBitmap = BitmapFromWriteableBitmap(writeableBitmap);
               var cvImage = new Image<Gray, byte>(new Bitmap(normalBitmap));

               if (cvImage != null)
               {
               Image<Gray, byte> greyImg = cvImage.Convert<Gray, byte>();

               Image<Gray, Byte> greyThreshImg = greyImg.ThresholdBinaryInv(new Gray(150), new Gray(255));

               Emgu.CV.Cvb.CvBlobs resultingImgBlobs = new Emgu.CV.Cvb.CvBlobs();
               Emgu.CV.Cvb.CvBlobDetector bDetect = new Emgu.CV.Cvb.CvBlobDetector();
               uint numWebcamBlobsFound = bDetect.Detect(greyThreshImg, resultingImgBlobs);

               Image<Rgb, byte> blobImg = greyThreshImg.Convert<Rgb, byte>();
               Rgb red = new Rgb(255, 0, 0);

               int blobNumber = 0;

               foreach (Emgu.CV.Cvb.CvBlob targetBlob in resultingImgBlobs.Values)
               {
                   int imageArea = blobImg.Width*blobImg.Height;
                   int blobArea = targetBlob.Area;
                   int blobBoundingBoxArea = targetBlob.BoundingBox.Width*targetBlob.BoundingBox.Height;

                   if (blobArea > 200.0 && blobBoundingBoxArea < (imageArea*0.99))
                   {
                       blobNumber++;
                       Rectangle rectangle = targetBlob.BoundingBox;
                       Rect convertedRect = new Rect(rectangle.X - 10, rectangle.Y - 10, rectangle.Width + 20,
                           rectangle.Height + 20);
                       //BitmapColorer.DrawRectangle(writeableBitmap, convertedRect);

                       writeableBitmap = writeableBitmap.Crop(rectangle.X - 10, rectangle.Y - 10, rectangle.Width + 20,
                           rectangle.Height + 20);
                       gradientBitmapRef = gradientBitmapRef.Crop(rectangle.X - 10, rectangle.Y - 10,
                           rectangle.Width + 20, rectangle.Height + 20);
                       realBitmapRef = realBitmapRef.Crop(rectangle.X - 10, rectangle.Y - 10, rectangle.Width + 20,
                           rectangle.Height + 20);
                   }
               }
               }
               if (returnBitmapIndex == 1)
               {
               return writeableBitmap;
               }
               else if (returnBitmapIndex == 2)
               {
               return gradientBitmapRef;
               }
               else
               {
               return realBitmapRef;
               }
        }
        void ProcessFrame(object sender, EventArgs e)
        {
            Mat frame = _cameraCapture.QueryFrame();
            Image <Bgr, Byte> ImageFrame = frame.ToImage <Bgr, Byte>();
            long matchTime;
            Mat  result     = new Mat();
            Mat  mask       = new Mat();
            Mat  homography = new Mat();


            Rectangle personROI = new Rectangle();

            Mat modelImage    = new Mat();
            Mat observedImage = new Mat();



            Rectangle roi = new Rectangle(213, 0, 213, 480); // set the roi image.ROI = new Rectangle(x, Y, Width, Height);

            //ImageFrame.ROI = roi;

            ImageFrame.Draw(roi, new Bgr(Color.Green), 5);

            if (ImageFrame != null)   // confirm that image is valid
            {
                Rectangle[] results = FindPedestrian.Find(frame, true, out processingTime);
                if (people.Count == 0)
                {
                    foreach (Rectangle rect in results)
                    {
                        if (rect.Width >= 150)
                        {
                            personROI.X      = rect.X;
                            personROI.Y      = rect.Y;
                            personROI.Width  = rect.Width;
                            personROI.Height = rect.Height;

                            Mat person = new Mat(frame, personROI);
                            people.Add(person);

                            ImageFrame.Draw(rect, new Bgr(Color.Red), 5);
                            //Console.WriteLine(index);
                        }
                    }
                }
                else
                {
                    foreach (Rectangle rect in results)
                    {
                        ImageFrame.Draw(rect, new Bgr(Color.Red), 5);
                        var check = false;
                        var temp  = new List <Mat>(people);

                        foreach (Mat aperson in people)
                        {
                            Mat img = new Mat(frame, rect);
                            observedImage = aperson;
                            modelImage    = img;
                            result        = Draw(modelImage, observedImage, out matchTime, out check);
                            if (!check)
                            {
                                temp.Add(img);
                                ++counter;
                                Console.WriteLine("Counter: " + counter);
                                break;
                            }
                        }
                        people = new List <Mat>(temp);

                        Console.WriteLine("End for frame processing");
                    }
                }

                Mat smoothedFrame = new Mat();
                CvInvoke.GaussianBlur(frame, smoothedFrame, new Size(3, 3), 1);     //filter out noises
                                                                                    //frame._SmoothGaussian(3);

                #region use the BG/FG detector to find the forground mask
                Mat forgroundMask = new Mat();
                //_fgDetector.Apply(smoothedFrame, forgroundMask);

                #endregion
                CvBlobs blobs = new CvBlobs();
                _blobDetector.Detect(forgroundMask.ToImage <Gray, byte>(), blobs);
                blobs.FilterByArea(1000, int.MaxValue);
                //_tracker.Process(smoothedFrame, forgroundMask);

                foreach (var pair in blobs)
                {
                    CvBlob b = pair.Value;
                    CvInvoke.Rectangle(frame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
                    //CvInvoke.PutText(frame,  blob.ID.ToString(), Point.Round(blob.Center), FontFace.HersheyPlain, 1.0, new MCvScalar(255.0, 255.0, 255.0));
                }

                imageBox1.Image = ImageFrame;
                //Console.WriteLine(ImageFrame.Size);
                imageBox2.Image = result;
                //imageBox2.Image = forgroundMask;
            }
            //people.Clear();
        }
예제 #16
0
파일: Class1.cs 프로젝트: hc2014/AI_FACE
        void ProcessFrame(object sender, EventArgs e)
        {
            Mat frame         = _cameraCapture.QueryFrame();
            Mat smoothedFrame = new Mat();

            CvInvoke.GaussianBlur(frame, smoothedFrame, new Size(3, 3), 1); //filter out noises
                                                                            //frame._SmoothGaussian(3);

            #region use the BG/FG detector to find the forground mask
            Mat forgroundMask = new Mat();
            _fgDetector.Apply(smoothedFrame, forgroundMask);
            #endregion

            CvBlobs blobs = new CvBlobs();
            _blobDetector.Detect(forgroundMask.ToImage <Gray, byte>(), blobs);
            blobs.FilterByArea(100, int.MaxValue);

            float scale = (frame.Width + frame.Width) / 2.0f;
            _tracker.Update(blobs, 0.01 * scale, 5, 5);

            long detectionTime;

            List <Rectangle> faces = new List <Rectangle>();
            List <Rectangle> eyes  = new List <Rectangle>();

            IImage image = (IImage)frame;//这一步是重点
            faceImage = frame.Bitmap;
            DetectFace.Detect(image
                              , "haarcascade_frontalface_default.xml", "haarcascade_eye.xml",
                              faces, eyes,
                              out detectionTime);

            #region 多人识别
            Graphics g1 = Graphics.FromImage(frame.Bitmap);
            List <FaceIdentifyModel> tempList = new List <FaceIdentifyModel>();
            foreach (Rectangle face in faces)
            {
                Image             rectImage1       = ImageHelper.CaptureImage(frame.Bitmap, face);// 自己封装的方法,通过大图截取矩形框的人脸图片,返回Image 对象
                FaceIdentifyModel MoreIdentifyInfo = FaceAPI.FaceIdentify(rectImage1, tb_Group.Text.Trim(), 1, 1);
                MoreIdentifyInfo.rect = face;
                tempList.Add(MoreIdentifyInfo);
            }
            Color color_of_pen1 = Color.Gray;
            color_of_pen1 = Color.Yellow;
            Pen pen1 = new Pen(color_of_pen1, 2.0f);

            Font       font1      = new Font("微软雅黑", 16, GraphicsUnit.Pixel);
            SolidBrush drawBrush1 = new SolidBrush(Color.Yellow);


            tb_Identify.Text = tempList.ToJson();
            foreach (var t in tempList)
            {
                g1.DrawRectangle(pen1, t.rect);

                if (t.result != null)
                {
                    g1.DrawString(t.result[0].user_info.Replace(",", "\r\n"), font1, drawBrush1, new Point(t.rect.X + 20, t.rect.Y - 20));
                }
                125
            }
            #endregion

            imageBox1.Image = frame;
            imageBox2.Image = forgroundMask;
        }