Exemple #1
0
        public static PointF[] RBSK(Image <Gray, byte> image, AutomatedRodentTracker.Services.RBSK.RBSK rbsk, PointF previousPoint, double movementThreshold)
        {
            List <VectorOfPoint> convertedContours = ImageExtension.GetContours(image, rbsk.Settings.ChainApproxMethod, rbsk.Settings.RetrievalType, rbsk.Settings.PolygonApproximationAccuracy, rbsk.Settings.MinimumPolygonArea);

            //If we have no contours then don't perform RBSK and return
            if (convertedContours.Count <= 0)
            {
                return(null);
            }

            PointF[] bestMatch = null;

            //Created sorted list to store results by probability
            HighestDoubleComparer comaprer = new HighestDoubleComparer();
            SortedList <double, RBSKParallelContainer> containers = new SortedList <double, RBSKParallelContainer>(comaprer);

            //Loop through all the contours
            foreach (VectorOfPoint contourSet in convertedContours)
            {
                Point[] currentContourSet          = contourSet.ToArray();
                List <List <PointF> > allKeyPoints = rbsk.FindKeyPoints(currentContourSet, rbsk.Settings.NumberOfSlides, false);

                if (allKeyPoints.Count == 0)
                {
                    continue;
                }

                //For each contour we are going to generate keypoints for each slide
                for (int i = 0; i <= rbsk.Settings.NumberOfSlides; i++)
                {
                    //Generate keypoints
                    List <PointF> keyPoints = allKeyPoints[i];

                    double tempProbability = 0;

                    //Check rules against keypoints
                    PointF[] headPoints = rbsk.FindPointsFromRules(keyPoints, image, previousPoint, movementThreshold, ref tempProbability);

                    if (headPoints != null)
                    {
                        while (containers.ContainsKey(tempProbability))
                        {
                            tempProbability += 0.000000001;
                        }

                        containers.Add(tempProbability, new RBSKParallelContainer(headPoints, tempProbability, currentContourSet, keyPoints.ToArray()));
                    }
                }
            }

            foreach (var rbskContainer in containers)
            {
                RBSKParallelContainer container = rbskContainer.Value;
                //if (rbsk.CheckAdvancedRules(container.HeadPoints, image))
                //{
                bestMatch = container.HeadPoints;
                break;
                //}
            }

            return(bestMatch);
        }
Exemple #2
0
        /// <summary>
        /// Perform RBSK on an edge detected image;
        /// </summary>
        /// <param name="image">An edge detected image</param>
        /// <param name="rbsk">The Rule Based Sliding Keypoints to perform</param>
        /// <param name="contour">The contour the mouse points were found on</param>
        /// <returns>A set of points which matches the RBSK, returns null if no points are found</returns>
        public static PointF[] RBSKParallel(Image <Gray, Byte> image, AutomatedRodentTracker.Services.RBSK.RBSK rbsk, ref Point[] contour)
        {
            List <VectorOfPoint> convertedContours = ImageExtension.GetContours(image, rbsk.Settings.ChainApproxMethod, rbsk.Settings.RetrievalType, rbsk.Settings.PolygonApproximationAccuracy, rbsk.Settings.MinimumPolygonArea);

            //If we have no contours then don't perform RBSK and return
            if (convertedContours.Count <= 0)
            {
                return(null);
            }

            //Created sorted list to store results by probability
            HighestDoubleComparer comaprer = new HighestDoubleComparer();
            SortedList <double, RBSKParallelContainer> tempContainer = new SortedList <double, RBSKParallelContainer>(comaprer);

            //Sorted list locker object
            object tempLocker = new object();

            //Loop through all the contours
            foreach (VectorOfPoint point in convertedContours)
            {
                Point[] currentContourSet          = point.ToArray();
                List <List <PointF> > allKeyPoints = rbsk.FindKeyPoints(currentContourSet, rbsk.Settings.NumberOfSlides, false);

                //For each contour we are going to generate keypoints for each slide
                Parallel.For(0, rbsk.Settings.NumberOfSlides, (i, state) =>
                {
                    //Generate keypoints
                    List <PointF> keyPoints = allKeyPoints[i];

                    if (keyPoints.Count >= 5)
                    {
                        double tempProbability = 0;

                        //Check rules against keypoints
                        PointF[] headPoints = rbsk.FindPointsFromRules(keyPoints, image, ref tempProbability);

                        if (headPoints != null)
                        {
                            lock (tempLocker)
                            {
                                if (!tempContainer.ContainsKey(tempProbability))
                                {
                                    tempContainer.Add(tempProbability, new RBSKParallelContainer(headPoints, tempProbability, currentContourSet, keyPoints.ToArray()));
                                }
                            }
                        }
                    }
                });
            }

            //Loop through results starting with the highest probability, check it against advanced rules, return the first match (the one with highest probability
            foreach (var rbskContainer in tempContainer)
            {
                RBSKParallelContainer container = rbskContainer.Value;

                if (rbsk.CheckAdvancedRules(container.HeadPoints, image))
                {
                    contour = container.ContourSet;
                    return(container.HeadPoints);
                }
            }

            return(null);
        }