Exemplo n.º 1
0
        private void PostProcessWhiskers2(PointF midPoint, Vector orientation, IWhiskerCollection whiskers, IWhiskerCollection finalWhiskers)
        {
            IWhiskerDetector wd            = ModelResolver.Resolve <IWhiskerDetector>();
            double           minAngleDelta = 15;

            //Whisker angle is measured against horizontal
            //Vector horiztonalVec = new Vector(1, 0);
            //double headAngle = Vector.AngleBetween(orientation, horiztonalVec);

            //return;

            if (whiskers.LeftWhiskers != null && whiskers.LeftWhiskers.Any())
            {
                List <IWhiskerSegment> leftWhiskers = whiskers.LeftWhiskers.ToList();

                wd.RemoveDudWhiskers(midPoint, leftWhiskers, orientation, minAngleDelta, true);
                finalWhiskers.LeftWhiskers = leftWhiskers.ToArray();
            }

            if (whiskers.RightWhiskers != null)
            {
                List <IWhiskerSegment> rightWhiskers = whiskers.RightWhiskers.ToList();

                wd.RemoveDudWhiskers(midPoint, rightWhiskers, orientation, minAngleDelta, false);
                finalWhiskers.RightWhiskers = rightWhiskers.ToArray();
            }
        }
Exemplo n.º 2
0
        private void ProcessWhiskers()
        {
            bool time = false;
            //bool showOnce = true;
            //Get quarter size of image around nose point
            int newWidth  = (int)(VideoWidth / ZoomRatio);
            int newHeight = (int)(VideoHeight / ZoomRatio);

            IWhiskerDetector whiskerDetector = ModelResolver.Resolve <IWhiskerDetector>();

            //float scaleRatio = 4;
            //whiskerDetector.LineLength = 8*(int) scaleRatio;
            whiskerDetector.OrientationResolution = 1;
            //Dictionary<int, IWhiskerCollection> whiskers = new Dictionary<int, IWhiskerCollection>();
            int newCount = HeadPoints.Count;
            int counter  = 0;

            for (int i = 0; i < HeadPoints.Count; i++)
            {
                //Stopwatch sw = new Stopwatch();

                ISingleFrameExtendedResults currentFrame = HeadPoints[i];

                counter++;

                if (currentFrame == null)
                {
                    //whiskers.Add(i, null);

                    if (ProgressUpdates != null)
                    {
                        ProgressUpdates(this, new RBSKVideoUpdateEvent(0.5 + (((double)counter / newCount) / 2)));
                    }
                    continue;
                }

                if (currentFrame.HeadPoints == null)
                {
                    //whiskers.Add(i, null);
                    if (ProgressUpdates != null)
                    {
                        ProgressUpdates(this, new RBSKVideoUpdateEvent(0.5 + (((double)counter / newCount) / 2)));
                    }
                    continue;
                }

                if (currentFrame.BodyContour == null || !currentFrame.BodyContour.Any())
                {
                    //whiskers.Add(i, null);
                    if (ProgressUpdates != null)
                    {
                        ProgressUpdates(this, new RBSKVideoUpdateEvent(0.5 + (((double)counter / newCount) / 2)));
                    }
                    continue;
                }

                //if (time)
                //{
                //    sw.Stop();
                //    Console.WriteLine("1) " + sw.ElapsedMilliseconds);
                //    sw.Restart();
                //}

                int x    = (int)currentFrame.HeadPoints[2].X - (newWidth / 2);
                int y    = (int)currentFrame.HeadPoints[2].Y - (newHeight / 2);
                int left = x + newWidth;
                int top  = y + newHeight;

                if (x < 0)
                {
                    x = 0;
                }

                if (y < 0)
                {
                    y = 0;
                }

                if (left > VideoWidth)
                {
                    left = VideoWidth;
                }

                if (top > VideoHeight)
                {
                    top = VideoHeight;
                }

                Rectangle zoomRect = new Rectangle(x, y, left - x, top - y);

                //if (time)
                //{
                //    sw.Stop();
                //    Console.WriteLine("2) " + sw.ElapsedMilliseconds);
                //    sw.Restart();
                //}

                //Video.SetFrame(i);

                //if (time)
                //{
                //    sw.Stop();
                //    Console.WriteLine("3) " + sw.ElapsedMilliseconds);
                //    sw.Restart();
                //}

                Inter interpolationType = Inter.Area;

                //using (Image<Gray, byte> frame = Video.GetGrayFrameImage())
                {
                    Image.ROI           = zoomRect;
                    BackgroundImage.ROI = zoomRect;

                    //if (time)
                    //{
                    //    sw.Stop();
                    //    Console.WriteLine("4) " + sw.ElapsedMilliseconds);
                    //    sw.Restart();
                    //}

                    using (Image <Gray, byte> subImage = Image.Copy())
                        using (Image <Gray, byte> subBinary = BackgroundImage.Copy())
                            using (Image <Gray, byte> zoomedImage = subImage.Resize(ScaleRatio, interpolationType))
                                using (Image <Gray, byte> zoomedBinary = subBinary.Resize(ScaleRatio, interpolationType))
                                {
                                    //if (time)
                                    //{
                                    //    sw.Stop();
                                    //    Console.WriteLine("5) " + sw.ElapsedMilliseconds);
                                    //    sw.Restart();
                                    //}
                                    //if (showOnce)
                                    //{
                                    //    ImageViewer.Show(zoomedImage);
                                    //    ImageViewer.Show(zoomedBinary);
                                    //    showOnce = false;
                                    //}
                                    PointF[] headPointsCorrected = new PointF[5];

                                    int     bodyContourLength    = currentFrame.BodyContour.Length;
                                    Point[] bodyContourCorrected = new Point[bodyContourLength];

                                    for (int j = 0; j < 5; j++)
                                    {
                                        headPointsCorrected[j] = new PointF((currentFrame.HeadPoints[j].X - zoomRect.X) * ScaleRatio, (currentFrame.HeadPoints[j].Y - zoomRect.Y) * ScaleRatio);
                                    }

                                    for (int j = 0; j < bodyContourLength; j++)
                                    {
                                        bodyContourCorrected[j] = new Point((int)((currentFrame.BodyContour[j].X - zoomRect.X) * ScaleRatio), (int)((currentFrame.BodyContour[j].Y - zoomRect.Y) * ScaleRatio));
                                    }

                                    List <IWhiskerSegment>[] cWhisk      = whiskerDetector.FindWhiskers(zoomedImage, zoomedBinary, headPointsCorrected, bodyContourCorrected, (int)ScaleRatio, currentFrame, null);
                                    List <IWhiskerSegment>   cWhiskLeft  = cWhisk[0];
                                    List <IWhiskerSegment>   cWhiskRight = cWhisk[1];

                                    //if (time)
                                    //{
                                    //    sw.Stop();
                                    //    Console.WriteLine("6) " + sw.ElapsedMilliseconds);
                                    //    sw.Restart();
                                    //}

                                    //IWhiskerSegment[] correctedWhiskers = new IWhiskerSegment[cWhisk.Length];
                                    foreach (var whisker in cWhiskLeft)
                                    {
                                        whisker.X = (int)((whisker.X / ScaleRatio) + zoomRect.X);
                                        whisker.Y = (int)((whisker.Y / ScaleRatio) + zoomRect.Y);
                                        Point p1 = new Point((int)((whisker.Line.P1.X / ScaleRatio)) + zoomRect.X, (int)((whisker.Line.P1.Y / ScaleRatio) + zoomRect.Y));
                                        Point p2 = new Point((int)((whisker.Line.P2.X / ScaleRatio)) + zoomRect.X, (int)((whisker.Line.P2.Y / ScaleRatio) + zoomRect.Y));
                                        whisker.Line = new LineSegment2D(p1, p2);
                                    }

                                    foreach (var whisker in cWhiskRight)
                                    {
                                        whisker.X = (int)((whisker.X / ScaleRatio) + zoomRect.X);
                                        whisker.Y = (int)((whisker.Y / ScaleRatio) + zoomRect.Y);
                                        Point p1 = new Point((int)((whisker.Line.P1.X / ScaleRatio)) + zoomRect.X, (int)((whisker.Line.P1.Y / ScaleRatio) + zoomRect.Y));
                                        Point p2 = new Point((int)((whisker.Line.P2.X / ScaleRatio)) + zoomRect.X, (int)((whisker.Line.P2.Y / ScaleRatio) + zoomRect.Y));
                                        whisker.Line = new LineSegment2D(p1, p2);
                                    }
                                    IWhiskerCollection whiskerCollection = ModelResolver.Resolve <IWhiskerCollection>();
                                    whiskerCollection.LeftWhiskers  = cWhiskLeft.ToArray();
                                    whiskerCollection.RightWhiskers = cWhiskRight.ToArray();
                                    currentFrame.AllWhiskers        = whiskerCollection;
                                    //whiskers.Add(i, whiskerCollection);
                                }
                }


                if (ProgressUpdates != null)
                {
                    ProgressUpdates(this, new RBSKVideoUpdateEvent(0.5 + (((double)counter / newCount) / 2)));
                }


                //if (time)
                //{
                //    sw.Stop();
                //    Console.WriteLine("7) " + sw.ElapsedMilliseconds);
                //    sw.Restart();
                //}
            }

            //WhiskerResultsAll = whiskers;
            //PostProcessWhiskers();
            //WhiskerResults = whiskers;
        }