コード例 #1
0
        static void Main()
        {
            //load from the Web
            var image = new Uri("http://vignette3.wikia.nocookie.net/disney/images/5/5d/Lena_headey_.jpg")
                        .GetBytes()
                        .DecodeAsColorImage();

            //show image
            image.Show("New Lena"); //to zoom or translate: press and hold Shift and scroll or move your mouse

            image = new Bgr <byte> [480, 640];

            //draw something
            image.Draw(new Rectangle(50, 50, 200, 100), Bgr <byte> .Red, -1);
            image.Draw(new Circle(50, 50, 25), Bgr <byte> .Blue, 5);
            image.Draw(new Box2D(new Point(250, 150), new Size(100, 100), 30), Bgr <byte> .Green, 1);
            image.Draw("Hello world!", Font.Big, new Point(10, 100), Bgr <byte> .White);

            //save and load
            image.Save("out.png");
            ImageIO.LoadColor("out.png").Clone().Show("Saved image", scaleForm: true);

            Console.WriteLine("Press [Enter] to exit.");
            Console.ReadLine();
            UI.CloseAll();
        }
コード例 #2
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();
        }
コード例 #3
0
        private static void drawContour(IList <PointF> controlPoints, Bgr <byte>[,] image)
        {
            const float CONTOUR_TENSION = 0;

            /********************  contour and control points *********************/
            var pointIndices = CardinalSpline.GetEqualyDistributedPointIndices(controlPoints, CONTOUR_TENSION, 500);
            var points       = CardinalSpline.InterpolateAt(controlPoints, CONTOUR_TENSION, pointIndices);

            var normals       = new List <LineSegment2DF>();
            var normalIndices = CardinalSpline.GetEqualyDistributedPointIndices(controlPoints, CONTOUR_TENSION, 100);

            foreach (var idx in normalIndices)
            {
                var pt = CardinalSpline.InterpolateAt(controlPoints, CONTOUR_TENSION, idx);
                var normalDirection = CardinalSpline.NormalAt(controlPoints, CONTOUR_TENSION, idx);
                var orientation     = (int)Angle.ToDegrees(System.Math.Atan2(normalDirection.Y, normalDirection.X));
                var normal          = getLine(orientation, pt, 20);
                normals.Add(normal);
            }
            /********************  contour and control points *********************/

            image.Draw(points.Select(x => x.Round()).ToArray(),
                       Bgr <byte> .Blue,
                       3);

            image.Draw(controlPoints.Select(x => new Circle(x.Round(), 3)), Bgr <byte> .Red, 3);
            //image.Draw(normals, Bgr<byte>.Green, 3, false);
        }
コード例 #4
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();
        }
コード例 #5
0
        /// <summary>
        /// Draw the framework ( a small red circle, blue square and a big green circle)
        /// </summary>
        /// <param name="frame">the video frame that about to draw on</param>
        /// /// <returns>nothing.</returns>
        private static void drawFrameWork(Bgr <byte>[,] frame)
        {
            int   RectWidth, RectHeight;
            int   SqrSide;
            float RectRatio;
            int   maxside = (int)(Math.Sqrt(frame.Width() * frame.Width() + frame.Height() * frame.Height()));

            RectRatio = (float)(frame.Height() > frame.Width() ? frame.Width() : frame.Height()) / maxside;
            //Console.WriteLine(frame.Height() + "  "+  frame.Width() +"   "+maxside + "  " + RectRatio);
            RectWidth  = (int)(frame.Width() * RectRatio);
            RectHeight = (int)(frame.Height() * RectRatio);

            DotImaging.Primitives2D.Rectangle rec = new DotImaging.Primitives2D.Rectangle((frame.Width() - RectWidth) / 2, (frame.Height() - RectHeight) / 2, (int)(frame.Width() * RectRatio), (int)(frame.Height() * RectRatio));
            //frame.Draw(rec, Bgr<byte>.Blue, 2);
            rec.Width = rec.Height;
            rec.X     = (frame.Width() - RectHeight) / 2;
            //frame.Draw(rec, Bgr<byte>.Red, 2);
            Circle cir = new Circle((frame.Width()) / 2, (frame.Height()) / 2, (int)(frame.Height() * RectRatio) / 2);

            //frame.Draw(cir, Bgr<byte>.Green, 2);
            cir.Radius = frame.Height() / 2;
            frame.Draw(cir, Bgr <byte> .Green, 2);
            SqrSide = (int)(frame.Height() / Math.Sqrt(2));
            DotImaging.Primitives2D.Rectangle sqr = new DotImaging.Primitives2D.Rectangle((frame.Width() - SqrSide) / 2, (frame.Height() - SqrSide) / 2, SqrSide, SqrSide);
            frame.Draw(sqr, Bgr <byte> .Blue, 2);
            cir.Radius = SqrSide / 2;
            frame.Draw(cir, Bgr <byte> .Red, 2);
        }
コード例 #6
0
        /// <summary>
        /// Finds peaks and valleys.
        /// The EmguCV's GetConvexHull function will find the global hull meaning if the smaller object is merged with other objects some peaks will not be detected.
        /// </summary>
        private void findPeaksAndValleys()
        {
            var scale = 25; //for bigger humps increase scale

            var contour = image.ToGray().FindContour();

            //find peaks and valeys
            List <int> peakIndeces, valeyIndeces;

            contour.FindExtremaIndices((angle, isPeak) =>
            {
                if ((isPeak && angle > 0 && angle < 90) ||
                    (!isPeak && angle > 0 && angle < 90))
                {
                    return(true);
                }

                return(false);
            },
                                       scale, out peakIndeces, out valeyIndeces);

            //cluster peak and valeys
            var cumulativeDistance = contour.CumulativeEuclideanDistance();
            var peakClusters       = contour.ClusterPoints(peakIndeces, scale * 2, cumulativeDistance).Where(x => x.Count > 3);
            var valeyClusters      = contour.ClusterPoints(valeyIndeces, scale * 2, cumulativeDistance).Where(x => x.Count > 3);

            //get humps
            List <int> humpPeaks;
            var        humps = contour.GetHumps(peakClusters.Select(x => ((int)x.Average() + contour.Count) % contour.Count).ToList() /*get mean value; if it is a negative index make it positive*/,
                                                valeyClusters.Select(x => ((int)x.Average() + contour.Count) % contour.Count).ToList(),
                                                scale, out humpPeaks);


            /******************** DRAWING *************************/

            //draw humps
            for (int i = 0; i < humps.Count; i++)
            {
                var slice = contour.ToCircularList().GetRange(humps[i]);
                image.Draw(slice.ToArray(), Bgr <byte> .Green, 5);
            }

            //draw valeys
            for (int i = 0; i < valeyIndeces.Count; i++)
            {
                image.Draw(new Circle(contour[valeyIndeces[i]], 3), Bgr <byte> .Red, 3);
            }

            //draw peaks
            for (int i = 0; i < peakIndeces.Count; i++)
            {
                image.Draw(new Circle(contour[peakIndeces[i]], 3), Bgr <byte> .Blue, 3);
            }

            //draw contour
            //image.Draw(contour.Select(x => new System.Drawing.Point(x.X, x.Y)), new Bgr(Color.Red), 3);
            pictureBox.Image = image.ToBitmap();
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: bestwpw/dot-imaging
        static void Main()
        {
            //create a managed image
            var image = new Bgr<byte>[480, 640];

            //draw something
            image.Draw(new Rectangle(50, 50, 200, 100), Bgr<byte>.Red, -1);
            image.Draw(new Circle(50, 50, 25), Bgr<byte>.Blue, 5);
            image.Draw(new Box2D(new Point(250, 150), new Size(100, 100), 30), Bgr<byte>.Green, 1);
            image.Draw("Hello world!", Font.Big, new Point(10, 100), Bgr<byte>.White);

            //save and load
            image.Save("out.png");
            ImageIO.LoadColor("out.png").Clone().Show(scaleForm: true);
        }
コード例 #8
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();
        }
コード例 #9
0
 private void drawDetections(IEnumerable <Rectangle> detections, Bgr <byte> color, int thickness)
 {
     foreach (var detection in detections)
     {
         debugImage.Draw(detection, color, thickness);
     }
 }
コード例 #10
0
        public BasicImageOperationsDemo()
        {
            InitializeComponent();

            //create a managed image
            var image = new Bgr <byte> [480, 640];

            //draw something
            image.Draw(new Rectangle(50, 50, 200, 100), Bgr <byte> .Red, -1);
            image.Draw(new Circle(50, 50, 25), Bgr <byte> .Blue, 5);
            image.Draw(new Box2D(new Point(250, 150), new Size(100, 100), 30), Bgr <byte> .Green, 1);
            image.Draw("Hello world!", CvFont.Big, new Point(10, 100), Bgr <byte> .White);

            //save and load
            image.Save("out.png");
            pictureBox.Image = ImageIO.LoadColor("out.png").ToBitmap();
        }
コード例 #11
0
        private void drawParticles(IEnumerable <ColorParticle> particles, Bgr <byte>[,] img)
        {
            var circles = particles.Select(x => new Circle {
                X = (int)x.Position.X, Y = (int)x.Position.Y, Radius = 2
            });

            img.Draw(circles, Bgr <byte> .Blue, 5);
        }
コード例 #12
0
        public BasicImageOperationsDemo()
        {
            InitializeComponent();

            //create a managed image
            var image = new Bgr<byte>[480, 640];

            //draw something
            image.Draw(new Rectangle(50, 50, 200, 100), Bgr<byte>.Red, -1);
            image.Draw(new Circle(50, 50, 25), Bgr<byte>.Blue, 5);
            image.Draw(new Box2D(new Point(250, 150), new Size(100, 100), 30), Bgr<byte>.Green, 1);
            image.Draw("Hello world!", CvFont.Big, new Point(10, 100), Bgr<byte>.White);

            //save and load
            image.Save("out.png");
            pictureBox.Image = ImageIO.LoadColor("out.png").ToBitmap();
        }
コード例 #13
0
        private void drawPoints(Bgr <byte>[,] im, List <PointF> points)
        {
            foreach (var pt in points)
            {
                var rect = new RectangleF(pt.X, pt.Y, 1, 1);
                rect.Inflate(winSize / 2, winSize / 2);

                im.Draw(rect, Bgr <byte> .Red, 2);
            }
        }
コード例 #14
0
ファイル: Program.cs プロジェクト: gitter-badger/dot-imaging
        static void Main()
        {
            //load from the Web
            var image = new Uri("http://vignette3.wikia.nocookie.net/disney/images/5/5d/Lena_headey_.jpg")
                            .GetBytes()
                            .DecodeAsColorImage();

            //show image
            image.Show("New Lena"); //to zoom or translate: press and hold Shift and scroll or move your mouse

            image = new Bgr<byte>[480, 640];

            //draw something
            image.Draw(new Rectangle(50, 50, 200, 100), Bgr<byte>.Red, -1);
            image.Draw(new Circle(50, 50, 25), Bgr<byte>.Blue, 5);
            image.Draw(new Box2D(new Point(250, 150), new Size(100, 100), 30), Bgr<byte>.Green, 1);
            image.Draw("Hello world!", Font.Big, new Point(10, 100), Bgr<byte>.White);

            //save and load
            image.Save("out.png");
            ImageIO.LoadColor("out.png").Clone().Show("Saved image", scaleForm: true);
        }
コード例 #15
0
        void videoCapture_InitFrame(object sender, EventArgs e)
        {
            videoCapture.ReadTo(ref frame);
            if (frame == null)
            {
                return;
            }

            videoCapture.Seek(-1, SeekOrigin.Current);

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

            GC.Collect();
        }
コード例 #16
0
        private static void drawTemplatesToFiles(Dictionary <ModelParams, ITemplate> dict, string path)
        {
            const int BORDER_OFFSET = 10;

            foreach (var pair in dict)
            {
                var mParams  = pair.Key;
                var fileName = String.Format("template_{0}_scale{1}_angle_{2}.bmp", mParams.ModelTypeIndex, mParams.Scale, mParams.Angle);
                fileName = Path.Combine(path, fileName);

                var template = pair.Value;
                var img      = new Bgr <byte> [template.Size.Height + 2 * BORDER_OFFSET, template.Size.Width + 2 * BORDER_OFFSET];
                img.Draw(template.Features.Select(x => new Point(x.X + BORDER_OFFSET, x.Y + BORDER_OFFSET)).ToArray(), Bgr <byte> .Red, 3);
                img.Save(fileName);
            }
        }
コード例 #17
0
        private void processFrame(Bgr <byte>[,] img, out long matchTimeMs)
        {
            var grayIm = img.ToGray();

            linPyr = LinearizedMapPyramid.CreatePyramid(grayIm); //prepare linear-pyramid maps

            /******************************* match templates + particle filter stuff ******************************/
            matchTimeMs = Diagnostics.MeasureTime(() =>
            {
                predict();
                update();
            });
            /******************************* match templates + particle filter stuff ******************************/

            /******************************* reset tracking (if necessary) ******************************/
            var p = particleFilter.MaxBy(x => x.Weight);

            if (p.Weight == 0)
            {
                if (resetClock.ElapsedMilliseconds > MAX_NONOBJ_TIME)
                {
                    particleFilter = initialParticles;
                }

                return;
            }
            resetClock.Restart();
            /******************************* reset tracking (if necessary) ******************************/

            /********************************* output **************************************/
            var metaData = getData(p.MetaDataRef);
            var text     = String.Format("S:{0:00}, A:{1:00}",
                                         p.ModelParameters.Scale, p.ModelParameters.Angle);

            if (metaData != null)
            {
                //img.Draw(p.MetaData, new Bgr(Color.Blue), 1);
                img.Draw(metaData.Points.ToArray(), Bgr <byte> .Blue, 3);
                img.DrawAnnotation(metaData.BoundingRect, text, DotImaging.Font.Small);
            }

            Console.WriteLine(text);
            /********************************* output **************************************/
        }
コード例 #18
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();
        }
コード例 #19
0
        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();
        }
コード例 #20
0
 private void drawParticles(IEnumerable<ColorParticle> particles, Bgr<byte>[,] img)
 {
     var circles = particles.Select(x => new Circle { X = (int)x.Position.X, Y = (int)x.Position.Y, Radius = 2 });
     img.Draw(circles, Bgr<byte>.Blue, 5);
 }