Пример #1
0
        void videoCapture_NewFrame(object sender, EventArgs e)
        {
            videoCapture.ReadTo(ref frame);
            if (frame == null)
            {
                return;
            }

            long preprocessTime, matchTime;
            var  bestRepresentatives = findObjects(frame, out preprocessTime, out matchTime);

            /************************************ drawing ****************************************/
            foreach (var m in bestRepresentatives)
            {
                frame.Draw(m.BoundingRect, Bgr <byte> .Blue, 1);
                frame.Draw(m.Points.Select(x => new Circle(x.X, x.Y, 5)).ToArray(), Bgr <byte> .Blue, -1);

                Console.WriteLine("Best template: " + m.Template.ClassLabel + " score: " + m.Score);
            }

            frame.Draw(String.Format("Matching {0} templates in: {1} ms", templPyrs.Count, matchTime),
                       font, new Point(10, 25), Bgr <byte> .Green);
            /************************************ drawing ****************************************/

            this.pictureBox.Image = frame.ToBitmap(); //it will be just casted (data is shared) 24bpp color

            //frame.Save(String.Format("C:/probaImages/imgMarked_{0}.jpg", i)); b.Save(String.Format("C:/probaImages/img_{0}.jpg", i)); i++;
            GC.Collect();
        }
Пример #2
0
        void videoCapture_NewFrame(object sender, EventArgs e)
        {
            videoCapture.ReadTo(ref frame);
            if (frame == null)
            {
                return;
            }

            var im = frame.ToGray().Cast <float>();

            long start = DateTime.Now.Ticks;

            List <PointF> newPositions;

            processImage(prevIm, im, this.oldPositions, out newPositions);

            prevIm       = im;
            oldPositions = newPositions;

            long end       = DateTime.Now.Ticks;
            long elapsedMs = (end - start) / TimeSpan.TicksPerMillisecond;

            frame.Draw("Processed: " + elapsedMs + " ms", font, new Point(15, 20), Bgr <byte> .Green);
            drawPoints(frame, newPositions);
            this.pictureBox.Image = frame.ToBitmap(); //it will be just casted (data is shared) 24bpp color
            GC.Collect();
        }
Пример #3
0
        /// <summary>
        /// update the framework with objects (templates) find in the frame
        /// </summary>
        /// <param name="templPyrs">the list of templates</param>
        /// <param name="videoCapture">the video stream</param>
        /// <param name="pictureBox">the picture box of window form</param>
        /// /// <returns>nothing.</returns>
        public void CaptureFrame(List <TemplatePyramid> templPyrs, ImageStreamReader videoCapture, PictureBox pictureBox)
        {
            DotImaging.Font font = new DotImaging.Font(FontTypes.HERSHEY_DUPLEX, 1, 0.1f);

            videoCapture.ReadTo(ref frame);

            if (frame == null)
            {
                return;
            }

            long preprocessTime, matchTime;
            var  bestRepresentatives = findObjects(frame, templPyrs, out preprocessTime, out matchTime);

            /************************************ drawing ****************************************/
            foreach (var m in bestRepresentatives)
            {
                frame.Draw(m.BoundingRect, Bgr <byte> .Blue, 1);
                frame.Draw(m.Points.Select(x => new Circle(x.X, x.Y, 5)).ToArray(), Bgr <byte> .Blue, -1);

                Console.WriteLine("Best template: " + m.Template.ClassLabel + "  AT  " + m.BoundingRect.X + "  " + m.BoundingRect.Y + " ANGLE: " + m.Template.Angle + " score: " + m.Score);
            }

            frame.Draw(String.Format("Matching {0} templates in: {1} ms", templPyrs.Count, matchTime),
                       font, new DotImaging.Primitives2D.Point(10, 25), Bgr <byte> .Green);
            /************************************ drawing ****************************************/

            pictureBox.Image = frame.ToBitmap(); //it will be just casted (data is shared) 24bpp color

            //frame.Save(String.Format("C:/probaImages/imgMarked_{0}.jpg", i)); b.Save(String.Format("C:/probaImages/img_{0}.jpg", i)); i++;
            //GC.Collect();
        }
Пример #4
0
        private void loadCurrentImageAnnotations()
        {
            if (capture.Position == capture.Length)
            {
                return;
            }

            drawingManager.Clear();

            capture.ReadTo(ref frame); //the order is relevant (position is automatically increased)
            var imageKey = getCurrentImageKey();

            if (Database.ContainsKey(imageKey))
            {
                drawingManager.AddRange(Database[imageKey]);
            }

            this.pictureBox.Image = frame.ToBitmap();
            pictureBox.Update();

            this.Text                 = getCurrentImageKey() + " -> " + new FileInfo(databaseFileName).Name;
            this.slider.Value         = (int)Math.Max(0, this.capture.Position - 1);
            this.slider.Maximum       = (int)(capture.Length - 1);
            this.lblCurrentFrame.Text = this.slider.Value.ToString();
            this.lblTotalFrames.Text  = this.slider.Maximum.ToString();
        }
Пример #5
0
        void videoCapture_InitFrame(object sender, EventArgs e)
        {
            videoCapture.ReadTo(ref frame);
            if (frame == null)
            {
                return;
            }

            if (isROISelected)
            {
                initTracking(frame);
                Application.Idle -= videoCapture_InitFrame;
                Application.Idle += videoCapture_NewFrame;
                return;
            }
            else
            {
                frame.Draw(roi, Bgr <byte> .Red, 3);
            }
            this.pictureBox.Image = frame.ToBitmap(); //it will be just casted (data is shared)

            GC.Collect();
        }
Пример #6
0
        void capture_NewFrame(object sender, EventArgs e)
        {
            reader.ReadTo <Bgr <byte> >(ref frame);

            if (frame == null)
            {
                /*Application.Idle -= capture_NewFrame;
                 * return;*/
                reader.Seek(0, SeekOrigin.Begin);
                return;
            }

            this.pictureBox.Image = frame.ToBitmap();
            GC.Collect();
        }
Пример #7
0
        void capture_NewFrame(object sender, EventArgs e)
        {
            reader.ReadTo <Bgr <byte> >(ref frame);

            if (frame == null)
            {
                /*Application.Idle -= capture_NewFrame;
                 * return;*/
                reader.Seek(0, SeekOrigin.Begin);
                return;
            }

            this.pictureBox.Image = frame.ToBitmap();

            //LINQ Grayscale conversion

            /*this.pictureBox.Image = frame.AsEnumerable()
             *                           .Select(x => x.ToGray())
             *                           .ToArray2D(frame.Size()).ToBitmap();*/
            GC.Collect();
        }
Пример #8
0
        void videoCapture_ProcessFrame(object sender, EventArgs e)
        {
            videoCapture.ReadTo(ref frame);
            if (frame == null)
            {
                return;
            }

            long start = DateTime.Now.Ticks;

            predict();
            update();

            long end       = DateTime.Now.Ticks;
            long elapsedMs = (end - start) / TimeSpan.TicksPerMillisecond;

            drawParticles(particleFilter.Draw(sampleCount: particleFilter.Count / 2), frame); //draw only better particles
            frame.Draw("Processed: " + elapsedMs + " ms", font, new Point(5, 20), Bgr <byte> .Red);
            this.pictureBox.Image = frame.ToBitmap();                                         //it will be just casted (data is shared)

            GC.Collect();
        }
Пример #9
0
        public KLDemo()
        {
            InitializeComponent();

            lkStorage = new PyrLKStorage <FlowColor>(pyrLevels: 1);

            try
            {
#if FILE_CAPTURE
                string resourceDir = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "Resources");
                videoCapture = new ImageDirectoryCapture(Path.Combine(resourceDir, "ImageSequence"), "*.jpg");

                videoCapture.ReadTo(ref frame);
                prevIm       = frame.ToGray().Cast <float>();
                oldPositions = prevIm.
                               GoodFeaturesToTrack(winSize, 0.05f)
                               .Select(x => new PointF(x.X, x.Y)).Take(100).ToList();
#else
                videoCapture = new CameraCapture(0);
                oldPositions = new List <PointF>();
                prevIm       = new Image <FlowColor, float>(imgSize);
#endif
            }
            catch (Exception)
            {
                MessageBox.Show("Cannot find any camera!");
                return;
            }

            if (videoCapture is CameraCapture)
            {
                (videoCapture as CameraCapture).FrameSize = imgSize;
            }

            this.FormClosing += CamshiftDemo_FormClosing;
            Application.Idle += videoCapture_NewFrame;
            videoCapture.Open();
        }
        void videoCapture_ProcessFrame(object sender, EventArgs e)
        {
            videoCapture.ReadTo <Bgr <byte> >(ref frame);
            if (frame == null)
            {
                return;
            }

            frame.StretchContrast(inPlace: true);

            long start = DateTime.Now.Ticks;

            long matchTimeMs;

            processFrame(frame, out matchTimeMs);

            long end       = DateTime.Now.Ticks;
            long elapsedMs = (end - start) / TimeSpan.TicksPerMillisecond;

            frame.Draw("Processed: " + /*matchTimeMs*/ elapsedMs + " ms", DotImaging.Font.Small, new Point(25, 20), Bgr <byte> .Red);
            this.pictureBox.Image = frame.ToBitmap();
            GC.Collect();
        }
Пример #11
0
        public KLDemo()
        {
            InitializeComponent();

            lkStorage = new PyrLKStorage<FlowColor>(pyrLevels: 1);
            
            try
            {
#if FILE_CAPTURE
                string resourceDir = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "Resources");
                videoCapture = new ImageDirectoryCapture(Path.Combine(resourceDir, "ImageSequence"), "*.jpg");

                videoCapture.ReadTo(ref frame);
                prevIm = frame.ToGray().Cast<float>();
                oldPositions = prevIm.
                              GoodFeaturesToTrack(winSize, 0.05f)
                              .Select(x => new PointF(x.X, x.Y)).Take(100).ToList();
#else
                videoCapture = new CameraCapture(0);
                oldPositions = new List<PointF>();
                prevIm = new Image<FlowColor, float>(imgSize);
#endif
            }
            catch (Exception)
            {
                MessageBox.Show("Cannot find any camera!");
                return;
            }

            if(videoCapture is CameraCapture)
                (videoCapture as CameraCapture).FrameSize = imgSize; 

            this.FormClosing += CamshiftDemo_FormClosing;
            Application.Idle += videoCapture_NewFrame;
            videoCapture.Open();
        }
Пример #12
0
        /// <summary>
        /// Build template with the cemara
        ///
        /// State Machine---->
        /// int -> build template -> resize to find the best -> draw the template then comfirm by user ->
        /// rotate for angles ->done and return
        ///
        /// make sure the object is in green circle
        /// </summary>
        /// <param name="name">template name, default to "Template" if the name is null</param>
        /// <param name="templPyrs">the template list that to be used</param
        /// <param name="videoCapture">the video stream</param>
        /// <param name="pictureBox">the picture box of window form</param>
        /// <param name="minRatio">The ratio of smallest size to original, default to 0.4</param>
        /// /// <returns>nothing.</returns>
        public void TemplateCapture(ref List <TemplatePyramid> templPyrs, ImageStreamReader videoCapture, PictureBox pictureBox, string name = null, float minCalibrationRatio = 0.4f)
        {
            if (name == null)
            {
                name = "Template";
            }

#if runXML
            try
            {
                Console.WriteLine("Reading from existing Template Data");
                templPyrs = fromXML(name);
                Cap       = State.Done;
            }
            catch (Exception)
            {
                Console.WriteLine("\nTemplate NOT found! \n initiating camera...");
            }
#endif
            switch (Cap)
            {
            case State.Init:
                videoCapture.ReadTo(ref frame);
                if (frame == null)
                {
                    return;
                }



                drawFrameWork(frame);

                pictureBox.Image = frame.ToBitmap();     //it will be just casted (data is shared) 24bpp color
                GC.Collect();
                break;

            case State.BuildingTemplate:
                Console.WriteLine("building template");
                videoCapture.ReadTo(ref frame);

                if (frame == null)
                {
                    return;
                }

                templatePic = frame.ToGray();

                //var list = new List<TemplatePyramid>();
                try
                {
                    if (templPyrs == null)
                    {
                        templPyrs = new List <TemplatePyramid>();
                    }
                    rotateLoad(templPyrs, templatePic, 1, frame.Width(), frame.Height(), userFunc: validateFeatures);
                }
                catch (Exception)
                {
                    Console.WriteLine("ERROR IN CREATING TEMPLATE!");

                    return;
                }

                Cap = State.Calibrate;
                break;

            case State.Calibrate:
                Console.WriteLine("calibrating template   " + CabRatio + "    " + templatePic.Width() * CabRatio);
                var bestRepresentatives = findObjects(frame, templPyrs);
                if (bestRepresentatives.Count == 0)
                {
                    if (templPyrs.Count != 0)
                    {
                        templPyrs.RemoveAt(templPyrs.Count - 1);
                    }
                    CabRatio -= (float)0.01;
                    int width  = (int)(templatePic.Width() * CabRatio);
                    int height = (int)(templatePic.Height() * CabRatio);
                    if (CabRatio < minCalibrationRatio)
                    {
                        Console.WriteLine("Calibration failed");
                        CabRatio = 1;
                        Cap      = State.Init;
                    }
                    DotImaging.Primitives2D.Size Nsize = new DotImaging.Primitives2D.Size(width, height);
                    ResiizedtemplatePic = ResizeExtensions_Gray.Resize(templatePic, Nsize, Accord.Extensions.Imaging.InterpolationMode.NearestNeighbor);

                    try
                    {
                        templPyrs.Add(TemplatePyramid.CreatePyramidFromPreparedBWImage(
                                          ResiizedtemplatePic, new FileInfo(name + " #" + TPindex++).Name, 0));
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("ERROR IN CALIBRATING TEMPLATE!");
                    }
                }
                else
                {
                    ResiizedtemplatePic = (ResiizedtemplatePic == null ? templatePic : ResiizedtemplatePic);

                    CaptureFrame(templPyrs, videoCapture, pictureBox);
                    drawFrameWork(frame);
                    pictureBox.Image = frame.ToBitmap();     //it will be just casted (data is shared) 24bpp color

                    Cap = State.Confirm;
                }
                break;

            case State.Confirm:
                Console.WriteLine("comfirm Template, press Y to continue, press R to retry, and other keys to abort");
                string a = Console.ReadLine();

                switch (a)
                {
                case "y":
                    Cap = State.Rotate;
                    break;

                case "r":
                    templPyrs.RemoveAt(templPyrs.Count - 1);

                    Cap = State.Init;
                    break;

                default:
                    Cap = State.Done;
                    break;
                }
                //if (a == "y") Cap = State.Rotate;
                //else
                //{
                //    Cap = State.Init;
                //}
                break;

            case State.Rotate:
                int SqrSide = (int)(frame.Height() / Math.Sqrt(2));
                templPyrs.AddRange(buildTemplate(ResiizedtemplatePic, SqrSide, SqrSide, false, totalAngles, totalSizes, 0.5f, null, validateFeatures));
                //ResiizedtemplatePic, totalAngles, SqrSide, SqrSide, true, userFunc: validateFeatures));


                Cap = State.ConfirmDone;
                break;

            case State.ConfirmDone:
                Console.WriteLine("Do you want to build a new template? press y to build another template, other keys to abort");
                string OtherTemplate = Console.ReadLine();
                if (OtherTemplate == "y" || OtherTemplate == "Y")
                {
                    Cap = State.Init;
                }
                else
                {
                    string resourceDir = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "Resources");
                    XMLTemplateSerializer <ImageTemplatePyramid <ImageTemplate>, ImageTemplate> .ToFile(templPyrs, Path.Combine(resourceDir, name + ".xml"));

                    Cap = State.Done;
                }
                break;

            //break;
            case State.Done:
                CaptureFrame(templPyrs, videoCapture, pictureBox);
                GC.Collect();
                break;
                //break;
            }
            pictureBox.Image = frame.ToBitmap(); //it will be just casted (data is shared) 24bpp color
        }