Example #1
0
        static void Main(string[] args)
        {
            var capture = new VideoCapture(@"G:\videodata\char 5s.mp4");
            var win     = new Window("capture");
            var mat     = new Mat();
            var tracker = TrackerCSRT.Create();
            var bbox    = new Rect();
            var bboxes  = new Rect[] { };

            capture.Read(mat);

            bbox = Cv2.SelectROI("capture", mat);
            //bboxes = Cv2.SelectROIs("capture", mat);

            //for(int i = 0; i < bboxes.Length; i++)
            //{
            //    Console.WriteLine(bboxes[i]);
            //}

            //var mt = MultiTracker.Create();
            Console.WriteLine(bbox);

            var bbox2d = new Rect2d(bbox.X, bbox.Y, bbox.Width, bbox.Height);


            //rect2d to rect

            tracker.Init(mat, bbox2d);



            while (true)
            {
                capture.Read(mat);
                // 読み込めるフレームがなくなったら終了
                if (mat.Empty())
                {
                    break;
                }

                tracker.Update(mat, ref bbox2d);

                Cv2.Rectangle(mat, bbox2d.ToRect(), new Scalar(0, 255, 0));

                //for (int i = 0; i < bboxes.Length; i++)
                //{
                //    Cv2.Rectangle(mat, bbox2d.ToRect(), new Scalar(0, 255, 0));
                //}

                Console.WriteLine(bbox2d);

                win.ShowImage(mat);



                Cv2.WaitKey(33);
            }

            Console.ReadKey();
        }
Example #2
0
        /// <summary>
        /// 获取图像
        /// </summary>
        public void Cap_Run(object o)
        {
            VideoCapture cap = new VideoCapture(Path);

            cap.Read(src);
            double timer = 0;
            double fps   = 0;

            while (true)
            {
                cal   = true;
                fps   = Cv2.GetTickFrequency() / (Cv2.GetTickCount() - timer);
                timer = Cv2.GetTickCount();
                cap.Read(src);                                          //读画面和参数
                bbox = new Rect2d(ptX, ptY, bboxSize, bboxSize);        //更新选择框
                if (Tracker != null && !Tracker.IsDisposed)             //在跟踪?
                {
                    trackStatus = Tracker.Update(src, ref tgt);         //更新跟踪参数
                    _tgt        = tgt.ToRect();                         //冗余但懒得改的转换
                    Cv2.Rectangle(src, _tgt, Scalar.Lime, 2);           //画跟踪框
                    Rect roiRect = tgt.ToRect();                        //roi
                    target.X = (int)(2 * tgt.X + tgt.Width) / 2;        //目标坐标
                    target.Y = (int)(2 * tgt.Y + tgt.Height) / 2;
                    tgtSize  = (int)tgt.Width;
                    try
                    {
                        Mat _roi = new Mat(src, roiRect);               //roi显示在小窗里,窗被我删了
                        _roi.CopyTo(roi);
                    }
                    catch
                    {
                    }
                }
                _bbox = bbox.ToRect();                                  //冗余但懒得改的转换
                Cv2.Rectangle(src, _bbox, Scalar.Red, 1);               //画选择框
                if (trackStatus)                                        //正在跟踪的画面指示
                {
                    Cv2.Line(src, target.X - tgtSize / 2, target.Y, target.X, target.Y + tgtSize / 2, Scalar.Lime, 2);
                    Cv2.Line(src, target.X, target.Y + tgtSize / 2, target.X + tgtSize / 2, target.Y, Scalar.Lime, 2);
                    Cv2.Line(src, target.X + tgtSize / 2, target.Y, target.X, target.Y - tgtSize / 2, Scalar.Lime, 2);
                    Cv2.Line(src, target.X, target.Y - tgtSize / 2, target.X - tgtSize / 2, target.Y, Scalar.Lime, 2);
                    Cv2.PutText(src, "X:" + target.X.ToString() + "|" + "Y:" + target.Y.ToString(), new OpenCvSharp.Point(tgt.X, tgt.Y - 5),
                                HersheyFonts.HersheyPlain, 3, Scalar.Lime, 3);
                }
                Cv2.PutText(src, "FPS:" + ((int)fps).ToString(), new OpenCvSharp.Point(1, videoH - 9), HersheyFonts.HersheyPlain, 2, Scalar.Black, 2);
                Cv2.PutText(src, "FPS:" + ((int)fps).ToString(), new OpenCvSharp.Point(0, videoH - 10), HersheyFonts.HersheyPlain, 2, Scalar.Lime, 2);

                PicBox_Video.Image = BitmapConverter.ToBitmap(src);
                cal = false;
                GC.Collect();
                //Cv2.WaitKey(50);
                if (MyRov.Mode.ControlMode == 2)
                {
                    if (target.X < 600)
                    {
                        TurnLeft();
                    }
                    if (target.X > 1000)
                    {
                        TurnRight();
                    }
                    if (target.Y < 400)
                    {
                        Up();
                    }
                    if (target.Y > 700)
                    {
                        Down();
                    }
                    if (target.X >= 600 && target.X <= 1000 && target.Y >= 400 && target.Y <= 700)
                    {
                        Advance();
                    }
                }
            }
            if (Isopen == 0)
            {
                this.th.Abort();
            }
        }