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
        public WhiskerCollectionXml(IWhiskerCollection whiskerCollection)
        {
            if (whiskerCollection != null)
            {
                if (whiskerCollection.LeftWhiskers != null)
                {
                    LeftWhiskers = whiskerCollection.LeftWhiskers.Select(x => new WhiskerSegmentXml(x)).ToArray();
                }


                if (whiskerCollection.RightWhiskers != null)
                {
                    RightWhiskers = whiskerCollection.RightWhiskers.Select(x => new WhiskerSegmentXml(x)).ToArray();
                }
            }
        }
Exemplo n.º 3
0
        public IWhiskerCollection GetWhiskerCollection()
        {
            IWhiskerCollection whiskerCollection = ModelResolver.Resolve <IWhiskerCollection>();

            if (RightWhiskers != null && RightWhiskers.Any())
            {
                whiskerCollection.RightWhiskers = RightWhiskers.Select(x => x.GetSegment()).ToArray();
            }

            if (LeftWhiskers != null && LeftWhiskers.Any())
            {
                whiskerCollection.LeftWhiskers = LeftWhiskers.Select(x => x.GetSegment()).ToArray();
            }

            whiskerCollection.DataLoadComplete();
            return(whiskerCollection);
        }
Exemplo n.º 4
0
        private void PostProcessWhiskers()
        {
            Console.WriteLine("Starting post processing");
            KeyValuePair <int, ISingleFrameExtendedResults> initWhiskers = HeadPoints.First(x => x.Value != null && x.Value.AllWhiskers != null && x.Value.AllWhiskers.LeftWhiskers != null && x.Value.AllWhiskers.LeftWhiskers.Any());

            int initKey = initWhiskers.Key;

            //int width = (int)Video.Width;
            //int height = (int)Video.Height;

            IWhiskerSegment[] leftWhiskers  = initWhiskers.Value.AllWhiskers.LeftWhiskers;
            IWhiskerSegment[] rightWhiskers = initWhiskers.Value.AllWhiskers.RightWhiskers;

            IWhiskerAllocator wa = ModelResolver.Resolve <IWhiskerAllocator>();

            ISingleFrameExtendedResults firstResult = HeadPoints[initKey];
            PointF nosePoint = firstResult.HeadPoint;
            PointF midPoint  = firstResult.HeadPoints[1].MidPoint(firstResult.HeadPoints[3]);

            ITrackSingleWhisker[] leftTrackedWhiskers  = wa.InitialiseWhiskers(leftWhiskers, nosePoint, midPoint);
            ITrackSingleWhisker[] rightTrackedWhiskers = wa.InitialiseWhiskers(rightWhiskers, nosePoint, midPoint);

            initKey++;
            for (int i = initKey; i < HeadPoints.Count; i++)
            {
                //IWhiskerCollection whiskerCollection = preliminaryWhiskers[i];

                IWhiskerSegment[] cLeftWhiskers  = HeadPoints[i].AllWhiskers.LeftWhiskers;
                IWhiskerSegment[] cRightWhiskers = HeadPoints[i].AllWhiskers.RightWhiskers;

                //IWhiskerAllocator wa = ModelResolver.Resolve<IWhiskerAllocator>();

                Dictionary <IWhiskerSegment, ITrackSingleWhisker> left  = wa.AllocateWhiskers(i, leftTrackedWhiskers, cLeftWhiskers, nosePoint, midPoint);
                Dictionary <IWhiskerSegment, ITrackSingleWhisker> right = wa.AllocateWhiskers(i, rightTrackedWhiskers, cRightWhiskers, nosePoint, midPoint);

                foreach (var kvp in left)
                {
                    kvp.Value.CurrentWhisker = kvp.Key;
                    kvp.Value.WhiskerList.Add(i, kvp.Key);
                }

                foreach (var kvp in right)
                {
                    kvp.Value.CurrentWhisker = kvp.Key;
                    kvp.Value.WhiskerList.Add(i, kvp.Key);
                }

                //foreach (ITrackSingleWhisker iWhisker in leftTrackedWhiskers)
                //{
                //    iWhisker.FindPotentialWhisker(i, cLeftWhiskers);
                //}

                //foreach (ITrackSingleWhisker iWhisker in rightTrackedWhiskers)
                //{
                //    iWhisker.FindPotentialWhisker(i, cRightWhiskers);
                //}
            }

            //Dictionary<int, IWhiskerCollection> finalWhiskers = new Dictionary<int, IWhiskerCollection>();


            for (int i = 0; i < HeadPoints.Count; i++)
            {
                IWhiskerCollection whiskerCollection = ModelResolver.Resolve <IWhiskerCollection>();

                List <IWhiskerSegment> leftWhisks  = new List <IWhiskerSegment>();
                List <IWhiskerSegment> rightWhisks = new List <IWhiskerSegment>();

                foreach (var trackedWhisker in leftTrackedWhiskers)
                {
                    if (trackedWhisker.WhiskerList.ContainsKey(i))
                    {
                        leftWhisks.Add(trackedWhisker.WhiskerList[i]);
                    }
                }

                foreach (var trackedWhisker in rightTrackedWhiskers)
                {
                    if (trackedWhisker.WhiskerList.ContainsKey(i))
                    {
                        rightWhisks.Add(trackedWhisker.WhiskerList[i]);
                    }
                }

                whiskerCollection.LeftWhiskers  = leftWhisks.ToArray();
                whiskerCollection.RightWhiskers = rightWhisks.ToArray();

                HeadPoints[i].AllWhiskers = whiskerCollection;
                //finalWhiskers.Add(i, whiskerCollection);
            }

            //AllRects = allRects;
            //WhiskerResults = finalWhiskers;
        }
Exemplo n.º 5
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;
        }