Example #1
0
        private List <WPoint[]> GetExclusionPolygons()
        {
            var zones  = AppSettings.Default.ExclusionZones.Split('|');
            var result = new List <WPoint[]>();

            foreach (var zone in zones)
            {
                if (string.IsNullOrWhiteSpace(zone))
                {
                    continue;
                }

                var points  = zone.Split(';');
                var polypts = new List <WPoint>();

                foreach (var point in points)
                {
                    var xy     = point.Split(',');
                    var p      = new DPoint(int.Parse(xy[0]), int.Parse(xy[1]));
                    var spacep = new SpacePoint(Space).FromFrameSpace(p);
                    polypts.Add(spacep.StandardPoint.ToWindowsPoint());
                }

                result.Add(polypts.ToArray());
            }

            return(result);
        }
Example #2
0
        public void ReadHullsFrame()
        {
            //Convert prior spaced hulls to frame space
            ConvexHullsFrame = ConvexHullsPrior.Keys.ToDictionary(k => k, v => new List <DPoint>());

            ConvexHullsPrior.Keys.ForEach(k =>
                                          ConvexHullsFrame[k].AddRange(ConvexHullsPrior[k].Select(pv =>
                                                                                                  Space.ToHeadSpaceFromPriorSpace(pv)
                                                                                                  ))
                                          );

            //Add standard space hulls
            ConvexHullsStandard = ConvexHullsPrior.Keys.ToDictionary(k => k, v => new List <DPoint>());

            ConvexHullsPrior.Keys.ForEach(k =>
                                          ConvexHullsStandard[k].AddRange(ConvexHullsFrame[k].Select(pv =>
                                                                                                     Space.ToStandardSpaceFromHeadSpace(pv)
                                                                                                     ))
                                          );

            //Find the proboscis base point
            var closestPts = ConvexHullsPrior[PointLabels.Proboscis]
                             .OrderByDescending(p => p.Y)
                             .Take(2)
                             .ToList();

            ProboscisHullBase = new SpacePoint(Space)
                                .FromPriorSpace
                                    (new WPoint(
                                        closestPts.Average(p => p.X),
                                        closestPts.Average(p => p.Y)
                                        ));
        }
Example #3
0
        private PartResult GetProbLoc()
        {
            //if (currentFrame.FrameIndex == 649)
            //    System.Diagnostics.Debug.Write("");

            var activePoints = ProboscisActivePoints;

            //Get top active points
            var probTail = ProbHist
                           .FromPointList(ProboscisModel, ProboscisActivePoints)
                           .GetTail(0.35, AppSettings.Default.StationaryThreshold);

            var tailsDeNoised = probTail.MedianFilter(radius: 1);

            ////Tips
            ////Centroid
            var centroid = ProboscisHullBase.StandardPoint;

            if (tailsDeNoised.Count > 0)
            {
                var closestToCentroid = tailsDeNoised.MinBy(p => p.Item1.Distance(centroid)).Item1;
                if (closestToCentroid.Distance(centroid) <= 2)
                {
                    centroid = closestToCentroid;
                }
            }

            ////Crawl to tip
            var crawlSurface = ProboscisCrawlSurface;

            if (crawlSurface == null)
            {
                crawlSurface          = new Frame(Space.StandardWidth, Space.StandardHeight);
                ProboscisCrawlSurface = crawlSurface;
            }

            var pointList = tailsDeNoised.Select(p => p.Item1).ToList();

            //var pointList = tailsDeNoised;
            crawlSurface.ColorPixels(ProboscisActivePoints, Color.Black);
            crawlSurface.ColorPixels(pointList, Color.White);

            var ctr = new SpacePoint(Space)
                      .FromPriorSpace(new WPoint(0, 0))
                      .StandardPoint;

            //Could do with lookup table
            var tip = CrawlToTip(crawlSurface, centroid, ctr, 1, AppSettings.Default.TipCrawlerRadius.Rounded(), AppSettings.Default.MaxTipCrawlerHop);

            //For debugging
            //crawlSurface.MarkPoint(centroid);
            //crawlSurface.MarkPoint(tip, inner: Color.Orange);

            return(new PartResult
            {
                Base = new SpacePoint(Space).FromStandardSpace(ProboscisHullBase.StandardPoint),
                Tip = new SpacePoint(Space).FromStandardSpace(tip),
                DetectedPoints = pointList.Select(sp => new SpacePoint(Space).FromStandardSpace(sp)).ToList()
            });
        }
Example #4
0
        private SideResult GetCoords(bool leftSide)
        {
            var activePoints   = leftSide ? LeftActivePoints : RightActivePoints;
            var FastMotionHist = leftSide ? LeftFastMotionHist : RightFastMotionHist;
            var SlowMotionHist = leftSide ? LeftSlowMotionHist : RightSlowMotionHist;
            var StationaryHist = leftSide ? LeftStationaryHist : RightStationaryHist;
            var prevSoln       = leftSide ? LeftMostRecentSoln : RightMostRecentSoln;

            //Fast tail
            var fastTail = FastMotionHist
                           .FromPointList(FastMotionModel, activePoints)
                           .GetTail(TailPercent, AppSettings.Default.FastThreshold);


            //Slow tail
            var slowTail = SlowMotionHist
                           .FromPointList(SlowMotionModel, activePoints)
                           .GetTail(TailPercent, AppSettings.Default.SlowThreshold);

            //Combine and then Median filter
            slowTail.AddRange(fastTail);

            var tailsDeNoised = slowTail.MedianFilter(radius: 1);

            //Check for extremely slow - stationary
            var statTail = StationaryHist
                           .FromPointList(StationaryModel, activePoints)
                           .GetTail(0.05, AppSettings.Default.StationaryThreshold);

            //Pick the model with most points
            tailsDeNoised = tailsDeNoised.Count > statTail.Count ? tailsDeNoised : statTail;

            //Or if not even stationary's (e.g. very fast), bail with prev soln
            if (tailsDeNoised.Count < 10)
            {
                return(prevSoln);
            }

            //Tips
            //Centroid
            var centroid = GetCentroid(tailsDeNoised);

            //Crawl to tip
            var crawlSurface = leftSide ? LeftCrawlSurface : RightCrawlSurface;

            if (crawlSurface == null)
            {
                crawlSurface = new Frame(Space.StandardWidth, Space.StandardHeight);

                if (leftSide)
                {
                    LeftCrawlSurface = crawlSurface;
                }
                else
                {
                    RightCrawlSurface = crawlSurface;
                }
            }

            var pointList = tailsDeNoised.Select(p => p.Item1).ToList();

            crawlSurface.ColorPixels(activePoints, Color.Yellow);
            crawlSurface.ColorPixels(pointList, Color.White);

            var ctr = new SpacePoint(Space)
                      .FromPriorSpace(new WPoint(leftSide ? -0.5 : 0.5, 0))
                      .StandardPoint;

            //Could do with lookup table
            var tip     = CrawlToTip(crawlSurface, centroid, ctr, 1, AppSettings.Default.TipCrawlerRadius.Rounded(), AppSettings.Default.MaxTipCrawlerHop);
            var baseLoc = CrawlToTip(crawlSurface, centroid, ctr, 1, AppSettings.Default.TipCrawlerRadius.Rounded(), AppSettings.Default.MaxTipCrawlerHop, crawlingAway: false);

            //For Debugging
            //crawlSurface.MarkPoint(baseLoc, inner: Color.Yellow, outer: Color.Black);
            //crawlSurface.MarkPoint(centroid, outer: Color.Black);
            //crawlSurface.MarkPoint(tip, inner: Color.Orange, outer: Color.Black);
            //crawlSurface.MarkPoint(ctr, inner: Color.Purple, outer: Color.Black);

            //Sectors
            var sectors = ComputeSectorWeights(tailsDeNoised, Space.StandardWidth, Space.StandardHeight, Space.HeadAngle);

            var result = new SideResult()
            {
                Tip            = new SpacePoint(Space).FromStandardSpace(tip),
                Base           = new SpacePoint(Space).FromStandardSpace(baseLoc),
                DetectedPoints = pointList.Select(sp => new SpacePoint(Space).FromStandardSpace(sp)).ToList(),
                SectorCounts   = sectors
            };

            if (leftSide)
            {
                LeftMostRecentSoln = result;
            }
            else
            {
                RightMostRecentSoln = result;
            }

            return(result);
        }
Example #5
0
        public void Annotate(Frame target)
        {
            using (var g = Graphics.FromImage(target.Bitmap))
            {
                var yellow1 = new Pen(Color.Yellow, 1);
                var blue7   = new Pen(Color.Blue, 5);
                var red     = new Pen(Color.Red, 1);
                var black   = new Pen(Color.Black, 1);

                var yellow3 = new Pen(Color.Yellow, 3);
                var blue1   = new Pen(Color.Blue, 1);
                var white1  = new Pen(Color.White, 1);

                //Model points
                if (AppSettings.Default.ShowModel && target?.ProcessorResult != null)
                {
                    var procResult = (TipAndPERResult)target.ProcessorResult;

                    Action <List <SpacePoint> > Draw = (pts) =>
                    {
                        if (pts == null || pts.Count == 0)
                        {
                            return;
                        }

                        //var scale = new SpacePoint().FromStandardSpace(new DPoint(100, 100)).SubclippedPoint;

                        //target.ColorPixels(pts.Select(p => p.FramePoint).ToList(), yellow1.Color);

                        var brush = new SolidBrush(Color.Yellow);
                        var scale = pts[0].Space.ScaleX;

                        pts.ForEach(p =>
                        {
                            g.FillEllipse(brush, new Rectangle((p.FramePoint.X - scale / 2).Rounded(), (p.FramePoint.Y - scale / 2).Rounded(), scale.Rounded(), scale.Rounded()));
                        });
                    };


                    Draw(procResult?.Left?.DetectedPoints);
                    Draw(procResult?.Right?.DetectedPoints);
                    Draw(procResult?.Proboscis?.DetectedPoints);
                }

                //Hulls
                if (ConvexHullsPrior != null)
                {
                    InitSpace();

                    var mouthBoundary = ConvexHullsPrior[PointLabels.Mandibles].Select(p => new SpacePoint(Space).FromPriorSpace(p).FramePoint.ToPointF()).ToArray();
                    var left          = ConvexHullsPrior[PointLabels.LeftFlagellumTip].Select(p => new SpacePoint(Space).FromPriorSpace(p).FramePoint.ToPointF()).ToArray();
                    var right         = ConvexHullsPrior[PointLabels.RightFlagellumTip].Select(p => new SpacePoint(Space).FromPriorSpace(p).FramePoint.ToPointF()).ToArray();
                    var rightJ        = ConvexHullsPrior[PointLabels.RightFlagellumBase].Select(p => new SpacePoint(Space).FromPriorSpace(p).FramePoint.ToPointF()).ToArray();
                    var leftJ         = ConvexHullsPrior[PointLabels.LeftFlagellumBase].Select(p => new SpacePoint(Space).FromPriorSpace(p).FramePoint.ToPointF()).ToArray();
                    var head          = ConvexHullsPrior[PointLabels.Head].Select(p => new SpacePoint(Space).FromPriorSpace(p).FramePoint.ToPointF()).ToArray();
                    var prob          = ConvexHullsPrior[PointLabels.Proboscis].Select(p => new SpacePoint(Space).FromPriorSpace(p).FramePoint.ToPointF()).ToArray();


                    g.FillPolygon(new SolidBrush(Color.FromArgb(125, 0, 0, 0)), mouthBoundary);
                    g.FillPolygon(new SolidBrush(Color.FromArgb(125, 0, 0, 0)), head);

                    //Debug exclusion zones
                    //var exc = GetExclusionPolygons()[0].Select(p => new SpacePoint(Space).FromStandardSpace(p.ToDrawingPoint()).FramePoint.ToPointF()).ToArray();
                    //g.DrawPolygon(white1, exc);

                    g.DrawPolygon(white1, left);
                    g.DrawPolygon(white1, right);
                    g.DrawPolygon(white1, prob);

                    //g.DrawPolygon(yellow1, mouthBoundary);
                    //g.DrawPolygon(yellow1, rightJ);
                    //g.DrawPolygon(yellow1, leftJ);
                    g.DrawPolygon(white1, mouthBoundary);
                    g.DrawPolygon(white1, head);
                }

                //Outer border
                g.DrawPolygon(black, new[]
                {
                    AppSettings.Default.Origin.ToPointF(),
                    AppSettings.Default.Origin.Moved(Space.HeadDims.X, 0).ToPointF(),
                    AppSettings.Default.Origin.Moved(Space.HeadDims.X, Space.HeadDims.Y).ToPointF(),
                    AppSettings.Default.Origin.Moved(0, Space.HeadDims.Y).ToPointF()
                });

                //Detected tip locations
                if (target?.ProcessorResult != null)
                {
                    var procResult = (TipAndPERResult)target.ProcessorResult;

                    if (procResult?.Left?.Tip != null)
                    {
                        DrawCircle(g, yellow3, blue1, procResult.Left.Tip.FramePoint);
                    }

                    if (procResult?.Right?.Tip != null)
                    {
                        DrawCircle(g, yellow3, blue1, procResult.Right.Tip.FramePoint);
                    }

                    if (procResult?.Proboscis != null)
                    {
                        DrawCircle(g, yellow3, blue1, procResult.Proboscis.Tip.FramePoint);
                    }
                }

                //Draw the origin
                var center = new SpacePoint(Space).FromPriorSpace(new WPoint(0, 0)).FramePoint;
                g.FillEllipse(new SolidBrush(Color.Yellow), new Rectangle(center.X - 1, center.Y - 1, 3, 3));
                g.FillEllipse(new SolidBrush(Color.Blue), new Rectangle(center.X, center.Y, 1, 1));
            }

            //target.Bitmap.Save(@"Y:\Downloads\BeeVids\19Feb16-Start Hept Tests\frames\" + target.FrameIndex + ".jpg", ImageFormat.Jpeg);
        }