Example #1
0
        //通过将sample与模板templates比对,寻找相应的contour,寻找到以后存放在FoundTemplateDesc类中
        public FoundTemplateDesc FindTemplate(Templates templates, Template sample)
        {
            //int maxInterCorrelationShift = (int)(templateSize * maxRotateAngle / Math.PI);
            //maxInterCorrelationShift = Math.Min(templateSize, maxInterCorrelationShift+13);
            double   rate          = 0;
            double   angle         = 0;
            Complex  interCorr     = default(Complex);
            Template foundTemplate = null;

            foreach (var template in templates)
            {
                //
                if (Math.Abs(sample.autoCorrDescriptor1 - template.autoCorrDescriptor1) > maxACFDescriptorDeviation)
                {
                    continue;
                }
                if (Math.Abs(sample.autoCorrDescriptor2 - template.autoCorrDescriptor2) > maxACFDescriptorDeviation)
                {
                    continue;
                }
                if (Math.Abs(sample.autoCorrDescriptor3 - template.autoCorrDescriptor3) > maxACFDescriptorDeviation)
                {
                    continue;
                }
                if (Math.Abs(sample.autoCorrDescriptor4 - template.autoCorrDescriptor4) > maxACFDescriptorDeviation)
                {
                    continue;
                }
                //
                double r = 0;          //可以看作相似度
                if (checkACF)
                {
                    r = template.autoCorr.NormDot(sample.autoCorr).Norma;      //ACF的话不需要FindMaxNorma()
                    if (r < minACF)
                    {
                        continue;
                    }
                }
                if (checkICF)
                {
                    interCorr = template.contour.InterCorrelation(sample.contour).FindMaxNorma();
                    r         = interCorr.Norma / (template.contourNorma * sample.contourNorma);
                    if (r < minICF)
                    {
                        continue;
                    }
                    if (Math.Abs(interCorr.Angle) > maxRotateAngle)
                    {
                        continue;
                    }
                }
                if (template.preferredAngleNoMore90 && Math.Abs(interCorr.Angle) >= Math.PI / 2)
                {
                    continue;//unsuitable angle
                }
                //find max rate
                if (r >= rate)
                {
                    rate          = r;
                    foundTemplate = template;
                    angle         = interCorr.Angle;
                }
            }
            //ignore antipatterns
            if (foundTemplate != null && foundTemplate.name == antiPatternName)
            {
                foundTemplate = null;
            }
            //
            if (foundTemplate != null)
            {
                return new FoundTemplateDesc()
                       {
                           template = foundTemplate, rate = rate, sample = sample, angle = angle
                       }
            }
            ;
            else
            {
                return(null);
            }
        }
Example #2
0
        public FoundTemplateDesc FindTemplate(Templates templates, Template sample)
        {
            double   rate          = 0;
            double   angle         = 0;
            var      interCorr     = new Complex(0, 0);
            Template foundTemplate = null;

            foreach (var template in templates)
            {
                if (Math.Abs(sample.AutoCorrDescriptor1 - template.AutoCorrDescriptor1) > MaxAcfDescriptorDeviation)
                {
                    continue;
                }
                if (Math.Abs(sample.AutoCorrDescriptor2 - template.AutoCorrDescriptor2) > MaxAcfDescriptorDeviation)
                {
                    continue;
                }
                if (Math.Abs(sample.AutoCorrDescriptor3 - template.AutoCorrDescriptor3) > MaxAcfDescriptorDeviation)
                {
                    continue;
                }
                if (Math.Abs(sample.AutoCorrDescriptor4 - template.AutoCorrDescriptor4) > MaxAcfDescriptorDeviation)
                {
                    continue;
                }
                //
                double r = 0;
                if (CheckACF)
                {
                    r = template.ACF.NotmalizedScalarProduct(sample.ACF).Norma;
                    if (r < MinACF)
                    {
                        continue;
                    }
                }
                if (CheckICF)
                {
                    interCorr = template.Contour.InterCorrelationFunction(sample.Contour).FindMaxNormaItem();
                    r         = interCorr.Norma / (template.ContourNorma * sample.ContourNorma);
                    if (r < MinICF)
                    {
                        continue;
                    }
                    if (Math.Abs(interCorr.Angle) > MaxRotateAngle)
                    {
                        continue;
                    }
                }
                if (template.PreferredAngleNoMore90 && Math.Abs(interCorr.Angle) >= Math.PI / 2)
                {
                    continue;                     //unsuitable angle
                }
                //find max rate
                if (r >= rate)
                {
                    rate          = r;
                    foundTemplate = template;
                    angle         = interCorr.Angle;
                }
            }
            //ignore antipatterns
            if (foundTemplate != null && foundTemplate.Name == AntiPatternName)
            {
                foundTemplate = null;
            }
            //
            if (foundTemplate != null)
            {
                return new FoundTemplateDesc {
                           Template = foundTemplate, Rate = rate, Sample = sample, Angle = angle
                }
            }
            ;
            return(null);
        }
    }
Example #3
0
        public FoundTemplateDesc FindTemplate(Templates templates, Template sample)
        {
            FoundTemplateDesc desc2;
            double            num     = 0.0;
            double            angle   = 0.0;
            Complex           complex = new Complex();
            Template          objA    = null;

            using (List <Template> .Enumerator enumerator = templates.GetEnumerator())
            {
                while (true)
                {
                    if (!enumerator.MoveNext())
                    {
                        break;
                    }
                    Template current = enumerator.Current;
                    if ((Math.Abs((int)(sample.autoCorrDescriptor1 - current.autoCorrDescriptor1)) <= this.maxACFDescriptorDeviation) && ((Math.Abs((int)(sample.autoCorrDescriptor2 - current.autoCorrDescriptor2)) <= this.maxACFDescriptorDeviation) && ((Math.Abs((int)(sample.autoCorrDescriptor3 - current.autoCorrDescriptor3)) <= this.maxACFDescriptorDeviation) && (Math.Abs((int)(sample.autoCorrDescriptor4 - current.autoCorrDescriptor4)) <= this.maxACFDescriptorDeviation))))
                    {
                        double num3 = 0.0;
                        if (!this.checkACF || (current.autoCorr.NormDot(sample.autoCorr).Norma >= this.minACF))
                        {
                            if (this.checkICF)
                            {
                                complex = current.contour.InterCorrelation(sample.contour).FindMaxNorma();
                                num3    = complex.Norma / (current.contourNorma * sample.contourNorma);
                                if (num3 < this.minICF)
                                {
                                    continue;
                                }
                                if (Math.Abs(complex.Angle) > this.maxRotateAngle)
                                {
                                    continue;
                                }
                            }
                            if ((!current.preferredAngleNoMore90 || (Math.Abs(complex.Angle) < 1.5707963267948966)) && (num3 >= num))
                            {
                                num   = num3;
                                objA  = current;
                                angle = complex.Angle;
                            }
                        }
                    }
                }
            }
            if ((objA != null) && (objA.name == this.antiPatternName))
            {
                objA = null;
            }
            if (ReferenceEquals(objA, null))
            {
                desc2 = null;
            }
            else
            {
                desc2 = new FoundTemplateDesc {
                    template = objA,
                    rate     = num,
                    sample   = sample,
                    angle    = angle
                };
            }
            return(desc2);
        }
Example #4
0
        private static void FilterByIntersection(ref List <FoundTemplateDesc> templates)
        {
            templates.Sort((t1, t2) => - t1.sample.contour.SourceBoundingRect.Area().CompareTo(t2.sample.contour.SourceBoundingRect.Area()));
            HashSet <int> set  = new HashSet <int>();
            int           item = 0;

            while (true)
            {
                bool flag = item < templates.Count;
                if (!flag)
                {
                    List <FoundTemplateDesc> list = new List <FoundTemplateDesc>();
                    item = 0;
                    while (true)
                    {
                        flag = item < templates.Count;
                        if (!flag)
                        {
                            templates = list;
                            return;
                        }
                        if (!set.Contains(item))
                        {
                            list.Add(templates[item]);
                        }
                        item++;
                    }
                }
                if (!set.Contains(item))
                {
                    Rectangle sourceBoundingRect = templates[item].sample.contour.SourceBoundingRect;
                    int       num2 = templates[item].sample.contour.SourceBoundingRect.Area();
                    sourceBoundingRect.Inflate(4, 4);
                    int num3 = item + 1;
                    while (true)
                    {
                        flag = num3 < templates.Count;
                        if (!flag)
                        {
                            break;
                        }
                        if (sourceBoundingRect.Contains(templates[num3].sample.contour.SourceBoundingRect))
                        {
                            if ((((double)templates[num3].sample.contour.SourceBoundingRect.Area()) / ((double)num2)) <= 0.9)
                            {
                                set.Add(num3);
                            }
                            else if (templates[item].rate > templates[num3].rate)
                            {
                                set.Add(num3);
                            }
                            else
                            {
                                set.Add(item);
                            }
                        }
                        num3++;
                    }
                }
                item++;
            }
        }