Result(ContouredImage template, List <Match> goodMatchCollection)
 {
     this.template   = template;
     MatchCollection = goodMatchCollection;
     Rotation        = goodMatchCollection[0].Page2Template180InvariantRotation;
     Scale           = goodMatchCollection[0].Page2TemplateScale;
 }
        public List <Result> FindOnPage(string pageFile, bool allMatches = true)
        {
            List <Result>  results = new List <Result>();
            ContouredImage page    = new ContouredImage(pageFile);

            //float minTemplateContourLength = Math.Max(template.GreyImage.Width, template.GreyImage.Height) / 10;

            if (allMatches)
            {
                List <Match> matches = new List <Match>();
                foreach (Contour templateContour in template.RobustContours)
                {
                    foreach (Contour pageContour in page.RobustContours)
                    {
                        Match m = new Match(templateContour, pageContour);
                        if (m.CvMatch > MaxCvMatchResult)
                        {
                            continue;
                        }
                        if (Math.Abs(m.Page2Template180InvariantRotation) > MaxExpectedRotationDeviation)
                        {
                            continue;
                        }
                        if (Math.Abs(m.Page2TemplateScale - 1) > MaxExpectedScaleDeviation)
                        {
                            continue;
                        }
                        matches.Add(m);
                    }
                }
                foreach (Match m in matches)
                {
                    List <Match> matchPossibleCollection = new List <Match>();
                    matchPossibleCollection.Add(m);

                    foreach (Contour tc in template.RobustContours)
                    {
                        if (tc == m.TemplateContour)
                        {
                            continue;
                        }
                        const float padding             = 0.3f;
                        RotatedRect expectedRotatedRect = new RotatedRect(
                            new PointF(tc.RotatedRect.Center.X * m.Page2TemplateScale, tc.RotatedRect.Center.Y * m.Page2TemplateScale),
                            new SizeF(tc.MinAreaRectangle.Width * (m.Page2TemplateScale + padding), tc.MinAreaRectangle.Height * (m.Page2TemplateScale + padding)),
                            tc.Angle
                            );
                        VectorOfPointF ps = new VectorOfPointF(expectedRotatedRect.GetVertices());
                        Match          m2 = matches.FirstOrDefault(x =>
                                                                   x != m &&
                                                                   x.TemplateContour == tc &&
                                                                   Math.Abs(x.Page2TemplateScale - m.Page2TemplateScale) < 0.2f
                                                                   //&& CvInvoke.PointPolygonTest(ps, x.PageContour.RotatedRectPoints[0], false) > 0
                                                                   //&& CvInvoke.PointPolygonTest(ps, x.PageContour.RotatedRectPoints[1], false) > 0
                                                                   //&& CvInvoke.PointPolygonTest(ps, x.PageContour.RotatedRectPoints[2], false) > 0
                                                                   //&& CvInvoke.PointPolygonTest(ps, x.PageContour.RotatedRectPoints[3], false) > 0
                                                                   );
                        if (m2 != null)
                        {
                            matchPossibleCollection.Add(m2);
                        }
                    }
                    results.Add(CreateResult(template, matchPossibleCollection));
                }
                results = results.OrderByDescending(x => x.MatchCollection.Count).ToList();
                //if ( < Threshold)
                //    break;
                if (results.Count > 0)
                {
                    results = results.Where(x => x.MatchCollection.Count > 15).ToList();
                    VectorOfVectorOfPoint bestMatchCollectionCvContours = new VectorOfVectorOfPoint();
                    //results.ForEach(x => bestMatchCollectionCvContours.Push(new VectorOfPoint(x.RotatedRect.GetVertices().Select(y => new Point((int)y.X, (int)y.Y)).ToArray())));
                    results.ForEach(x => x.MatchCollection.ForEach(a => bestMatchCollectionCvContours.Push(a.PageContour.Points)));
                    MainForm.This.PageBox.Image = drawContours(page.GreyImage, bestMatchCollectionCvContours);
                }
            }
            else
            {
                throw new Exception("TBD");
            }
            if (results.Count > 0)
            {
                return(results);
            }
            return(null);
        }
 public ImageDetectorByContour(string templateFile)
 {
     template = new ContouredImage(templateFile);
     MainForm.This.TemplateBox.Image = drawContours(template.GreyImage, template.CvContours);
 }
 static Result Create(ContouredImage template, List <Match> goodMatchCollection)
 {
     return(new Result(template, goodMatchCollection));
 }