Ejemplo n.º 1
0
        private List <Match> findObjects(Image <Bgr, byte> image, out long preprocessTime, out long matchTime)
        {
            var grayIm = image.Convert <Gray, byte>();

            var bestRepresentatives = new List <Match>();

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            linPyr         = LinearizedMapPyramid.CreatePyramid(grayIm); //prepeare linear-pyramid maps
            preprocessTime = stopwatch.ElapsedMilliseconds;

            stopwatch.Restart();
            List <Match> matches = linPyr.MatchTemplates(templPyrs, threshold);

            stopwatch.Stop(); matchTime = stopwatch.ElapsedMilliseconds;

            var matchGroups = new MatchClustering(minDetectionsPerGroup).Group(matches.ToArray());

            foreach (var group in matchGroups)
            {
                bestRepresentatives.Add(group.Representative);
            }

            return(bestRepresentatives);
        }
Ejemplo n.º 2
0
        private void measure(LinearizedMapPyramid linPyr, List <ModelParticle> particles)
        {
            particles.ForEach(x => x.Weight = 0);

            IDictionary <ModelParams, IEnumerable <ModelParticle> > nonDistinctMembers;
            var uniqueParticles = getDistinctParticles(particles, out nonDistinctMembers); //get distinct particles (there is no need to match the same templates)

            var matches = linPyr.PyramidalMaps.First().MatchTemplates(uniqueParticles, MATCHING_MIN_THRESHOLD);

            if (matches.Count == 0)
            {
                return;
            }

            var groups = matchClustering.Group(matches.ToArray(), MatchClustering.COMPARE_BY_SIZE);

            //var bestGroup = groups/*.Where(x=>x.Neighbours > 3)*/.MaxBy(x => x.Neighbours); //for now
            var bestGroup   = groups.MaxBy(x => x.Representative.BoundingRect.Area());
            var largestSize = bestGroup.Representative.Template.Size;
            var scaleFactor = 1f / (largestSize.Width * largestSize.Height);

            foreach (var m in bestGroup.Detections)
            {
                var particle = (ModelParticle)m.Template;
                var score    = (m.Score / 100) * (particle.Size.Width * particle.Size.Height * scaleFactor); //actual score multiplied with size factor (the bigger the better)
                m.Score = score;
                if (particle.Weight < score)
                {
                    particle.Weight      = score;                        //1 particle may correspond to several matches
                    particle.MetaDataRef = new WeakReference <Match>(m); //avoid circular reference (template inside match is particle again)
                }
            }

            updateNonDistintParticleData(uniqueParticles, nonDistinctMembers); //update the rest of particles (which are the same) if any
        }
Ejemplo n.º 3
0
        /// <summary>
        ///  find the matches in the given image with stopwatch included.
        /// </summary>
        /// <param name="image">input image.</param>
        /// <param name="templPyrs">input template list.</param
        /// <param name="preprocessTime">Time for preprocessing .</param>
        /// <param name="matchTime">The time it takes to find matches</param>
        /// <param name="Threshold">the threshold for the matching algor.</param>
        /// <param name="labels">the specific label(s) that included in the returned List
        ///                      set to null to find all matches</param>
        /// <param name="userFunc">Input User Function for customization and diversity</param>
        /// <returns>List of found matches.</returns>
        public static List <Match> findObjects(Bgr <byte>[,] image, List <TemplatePyramid> templPyrs, out long preprocessTime, out long matchTime, int Threshold = 80, String[] labels = null, int minDetectionsPerGroup = 0, Func <List <Match>, List <Match> > userFunc = null)
        {
            var grayIm = image.ToGray();
            var bestRepresentatives = new List <Match>();

            //userFunc = (userFunc != null) ? userFunc : (inputList) => inputList;
            Stopwatch stopwatch = new Stopwatch();

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

            preprocessTime = stopwatch.ElapsedMilliseconds;

            stopwatch.Restart();
            List <Match> matches = linPyr.MatchTemplates(templPyrs, Threshold);

            stopwatch.Stop(); matchTime = stopwatch.ElapsedMilliseconds;

            var matchGroups = new MatchClustering(minDetectionsPerGroup).Group(matches.ToArray());

            foreach (var group in matchGroups)
            {
                if (labels == null ? true : labels.Contains(group.Representative.Template.ClassLabel))
                {
                    bestRepresentatives.Add(group.Representative);
                }
            }
            if (userFunc != null)
            {
                bestRepresentatives = userFunc(bestRepresentatives);
            }
            return(bestRepresentatives);
        }
Ejemplo n.º 4
0
        private void processFrame(Image <Bgr, byte> img, out long matchTimeMs)
        {
            var grayIm = img.Convert <Gray, byte>();

            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, Bgr8.Blue, 3);
                img.DrawAnnotation(metaData.BoundingRect, text, annotationWidth: 80);
            }

            Console.WriteLine(text);
            /********************************* output **************************************/
        }
Ejemplo n.º 5
0
        private List<Match> findObjects(Bgr<byte>[,] image, out long preprocessTime, out long matchTime)
        {
            var grayIm = image.ToGray();

            var bestRepresentatives = new List<Match>();

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start(); 
            linPyr = LinearizedMapPyramid.CreatePyramid(grayIm); //prepare linear-pyramid maps
            preprocessTime = stopwatch.ElapsedMilliseconds;

            stopwatch.Restart();
            List<Match> matches = linPyr.MatchTemplates(templPyrs, threshold);
            stopwatch.Stop(); matchTime = stopwatch.ElapsedMilliseconds;

            var matchGroups = new MatchClustering(minDetectionsPerGroup).Group(matches.ToArray());
            foreach (var group in matchGroups)
            {
                bestRepresentatives.Add(group.Representative);
            }

            return bestRepresentatives;
        }