Пример #1
0
        float detectScaleByScalingAnchor()
        {
            Template.Anchor.CvImage sda = PageCollection.ActiveTemplate.GetScalingAnchor();
            CvImage searchRectangleCI;

            if (sda.SearchRectangleMargin >= 0)
            {
                RectangleF sr = getSearchRectangle(sda.Rectangle(), sda.SearchRectangleMargin);
                Bitmap     b  = GetRectangleFromActiveTemplateBitmap(sr.X / Settings.Constants.Image2PdfResolutionRatio, sr.Y / Settings.Constants.Image2PdfResolutionRatio, sr.Width / Settings.Constants.Image2PdfResolutionRatio, sr.Height / Settings.Constants.Image2PdfResolutionRatio);
                if (b == null)
                {
                    throw new Exception("The scaling anchor's rectangle is null.");
                }
                searchRectangleCI = new CvImage(b);
            }
            else
            {
                searchRectangleCI = ActiveTemplateCvImage;
            }
            CvImage.Match m = sda.Image.FindBestMatchWithinImage(searchRectangleCI, sda.Threshold, sda.ScaleDeviation, PageCollection.ActiveTemplate.CvImageScalePyramidStep);
            if (m != null)
            {
                return(m.Scale);
            }
            return(0);
        }
Пример #2
0
        /// <summary>
        /// Runs through pyramid only until something is found.
        /// </summary>
        /// <param name="cvImage"></param>
        /// <param name="padding"></param>
        /// <param name="threshold"></param>
        /// <param name="scaleDeviation"></param>
        /// <param name="scaleStep"></param>
        /// <returns></returns>
        public List <Match> FindWithinImage(CvImage cvImage, Size padding, float threshold, float scaleDeviation, int scaleStep)
        {
            List <Match> ms;

            ms = findWithinImage(image, padding, cvImage.image, threshold);
            if (ms.Count > 0)
            {
                return(ms);
            }
            //running through pyramid
            int stepCount = Convert.ToInt32(scaleDeviation * Width / scaleStep);

            for (int i = 1; i <= stepCount; i++)
            {
                float scaleDelta            = scaleStep * i / Width;
                Image <Gray, byte> template = image.Resize(1 + scaleDelta, Inter.Linear);
                ms = findWithinImage(template, padding, cvImage.image, threshold);
                if (ms.Count > 0)
                {
                    return(ms);
                }
                template = image.Resize(1 - scaleDelta, Inter.Linear);
                ms       = findWithinImage(template, padding, cvImage.image, threshold);
                if (ms.Count > 0)
                {
                    return(ms);
                }
            }
            return(ms);
        }
Пример #3
0
        public System.Drawing.Point?FindWithinImage(CvImage cvImage, float threshold, float scaleDeviation, int scaleStep /*, out float detectedScale*/)
        {
            System.Drawing.Point?p;
            p = findWithinImage(Image, cvImage.Image, threshold);
            if (p != null)
            {
                return(p);
            }
            //running though pyramid
            int stepCount = Convert.ToInt32(scaleDeviation * Width / scaleStep);

            for (int i = 1; i <= stepCount; i++)
            {
                double             scaleDelta = scaleStep * i / Width;
                Image <Gray, byte> template   = Image.Resize(1 + scaleDelta, Inter.Linear);
                p = findWithinImage(template, cvImage.Image, threshold);
                if (p != null)
                {
                    return(p);
                }
                template = Image.Resize(1 - scaleDelta, Inter.Linear);
                p        = findWithinImage(template, cvImage.Image, threshold);
                if (p != null)
                {
                    return(p);
                }
            }
            return(null);
        }
Пример #4
0
        public Match FindBestMatchWithinImage(CvImage cvImage, float threshold, float scaleDeviation, int scaleStep)
        {
            Match bestMatch          = null;
            Tuple <Point, float> p_s = findBestMatchWithinImage(image, cvImage.image);

            if (p_s != null && p_s.Item2 > threshold)
            {
                bestMatch = new Match {
                    Rectangle = new Rectangle(p_s.Item1, new Size(image.Width, image.Height)), Scale = 1, Score = p_s.Item2
                }
            }
            ;
            //running through pyramid
            int stepCount = Convert.ToInt32(scaleDeviation * Width / scaleStep);

            for (int i = 1; i <= stepCount; i++)
            {
                float scaleDelta            = (float)scaleStep * i / Width;
                float scale                 = 1 + scaleDelta;
                Image <Gray, byte> template = image.Resize(scale, Inter.Linear);
                p_s = findBestMatchWithinImage(template, cvImage.image);
                if (p_s != null && (bestMatch == null && p_s.Item2 > threshold || bestMatch != null && p_s.Item2 > bestMatch.Score))
                {
                    bestMatch = new Match {
                        Rectangle = new Rectangle(p_s.Item1, new Size(template.Width, template.Height)), Scale = scale, Score = p_s.Item2
                    }
                }
                ;
                scale    = 1 - scaleDelta;
                template = image.Resize(scale, Inter.Linear);
                p_s      = findBestMatchWithinImage(template, cvImage.image);
                if (p_s != null && (bestMatch == null && p_s.Item2 > threshold || bestMatch != null && p_s.Item2 > bestMatch.Score))
                {
                    bestMatch = new Match {
                        Rectangle = new Rectangle(p_s.Item1, new Size(template.Width, template.Height)), Scale = scale, Score = p_s.Item2
                    }
                }
                ;
            }
            return(bestMatch);
        }
Пример #5
0
        /// <summary>
        /// Run through pyramid and detect the best scale.
        /// </summary>
        /// <param name="cvImage"></param>
        /// <param name="padding"></param>
        /// <param name="threshold"></param>
        /// <param name="scaleDeviation"></param>
        /// <param name="scaleStep"></param>
        /// <param name="bestScale"></param>
        /// <returns></returns>
        public List <Match> FindWithinImage(CvImage cvImage, Size padding, float threshold, float scaleDeviation, int scaleStep, out float bestScale)
        {
            bestScale = 1;
            List <Match> bestMatches = findWithinImage(image, padding, cvImage.image, threshold);
            float        bestScore   = bestMatches.Max(a => a.Score);
            //running through pyramid
            int stepCount = Convert.ToInt32(scaleDeviation * Width / scaleStep);

            for (int i = 1; i <= stepCount; i++)
            {
                float scaleDelta            = scaleStep * i / Width;
                float scale                 = 1 + scaleDelta;
                Image <Gray, byte> template = image.Resize(scale, Inter.Linear);
                List <Match>       ms       = findWithinImage(template, padding, cvImage.image, threshold);
                float s = ms.Max(a => a.Score);
                if (bestScore > s)
                {
                    break;
                }
                bestScale   = scale;
                bestMatches = ms;
                bestScore   = s;
            }
            for (int i = 1; i <= stepCount; i++)
            {
                float scaleDelta            = scaleStep * i / Width;
                float scale                 = 1 - scaleDelta;
                Image <Gray, byte> template = image.Resize(scale, Inter.Linear);
                List <Match>       ms       = findWithinImage(template, padding, cvImage.image, threshold);
                float s = ms.Max(a => a.Score);
                if (bestScore > s)
                {
                    break;
                }
                bestScale   = scale;
                bestMatches = ms;
                bestScore   = s;
            }
            return(bestMatches);
        }
Пример #6
0
        public bool FindMatchesWithinImage(CvImage cvImage, float threshold, float scaleDeviation, int scaleStep, Func <Match, bool> getNext)
        {
            foreach (Match m in findMatchsWithinImage(image, 1, cvImage.image, threshold))
            {
                if (!getNext(m))
                {
                    return(true);
                }
            }
            //running through pyramid
            int stepCount = Convert.ToInt32(scaleDeviation * Width / scaleStep);

            for (int i = 1; i <= stepCount; i++)
            {
                float scaleDelta            = (float)scaleStep * i / Width;
                float scale                 = 1 + scaleDelta;
                Image <Gray, byte> template = image.Resize(scale, Inter.Linear);
                foreach (Match m in findMatchsWithinImage(template, scale, cvImage.image, threshold))
                {
                    if (!getNext(m))
                    {
                        return(true);
                    }
                }
                scale    = 1 - scaleDelta;
                template = image.Resize(scale, Inter.Linear);
                foreach (Match m in findMatchsWithinImage(template, scale, cvImage.image, threshold))
                {
                    if (!getNext(m))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Пример #7
0
        bool _findAnchor(Template.Anchor a, PointF parentAnchorPoint0, Func <PointF, bool> proceedOnFound)
        {
            if (!a.IsSet())
            {
                return(false);
            }
            RectangleF rectangle = a.Rectangle();
            RectangleF searchRectangle;

            {
                if (a.ParentAnchorId != null)
                {
                    RectangleF pameir = pageCollection.ActiveTemplate.Anchors.Find(x => x.Id == a.ParentAnchorId).Rectangle();
                    rectangle.X += parentAnchorPoint0.X - pameir.X;
                    rectangle.Y += parentAnchorPoint0.Y - pameir.Y;
                }
                if (a.SearchRectangleMargin >= 0)
                {
                    searchRectangle = getSearchRectangle(rectangle, a.SearchRectangleMargin);
                }
                else
                {
                    searchRectangle = new RectangleF();//not used, just stub to compile
                }
            }

            switch (a.Type)
            {
            case Template.Anchor.Types.PdfText:
            {
                Template.Anchor.PdfText pt = (Template.Anchor.PdfText)a;
                List <Template.Anchor.PdfText.CharBox> cbs = pt.CharBoxs;
                if (pageCollection.ActiveTemplate.IgnoreInvisiblePdfChars)
                {
                    cbs = cbs.Where(x => !Pdf.InvisibleCharacters.Contains(x.Char)).ToList();
                }
                if (cbs.Count < 1)
                {
                    int w = (int)(Bitmap.Width * Settings.Constants.Image2PdfResolutionRatio - rectangle.Width);
                    int h = (int)(Bitmap.Height * Settings.Constants.Image2PdfResolutionRatio - rectangle.Height);
                    for (int i = 0; i < w; i++)
                    {
                        for (int j = 0; j < h; j++)
                        {
                            RectangleF actualR = new RectangleF(i, j, rectangle.Width, rectangle.Height);
                            if (PdfCharBoxs.FirstOrDefault(x => actualR.Contains(x.R) && (!pageCollection.ActiveTemplate.IgnoreInvisiblePdfChars || !Pdf.InvisibleCharacters.Contains(x.Char))) == null &&
                                !proceedOnFound(actualR.Location)
                                )
                            {
                                return(true);
                            }
                        }
                    }
                    return(false);
                }
                IEnumerable <Pdf.CharBox> tcb0s;
                if (pt.SearchRectangleMargin < 0)
                {
                    tcb0s = PdfCharBoxs.Where(x => x.Char == cbs[0].Char);
                }
                else
                {
                    tcb0s = PdfCharBoxs.Where(x => x.Char == cbs[0].Char && searchRectangle.Contains(x.R));
                }
                List <Pdf.CharBox> tcbs = new List <Pdf.CharBox>();
                foreach (Pdf.CharBox tcb0 in tcb0s)
                {
                    tcbs.Clear();
                    tcbs.Add(tcb0);
                    for (int i = 1; i < cbs.Count; i++)
                    {
                        PointF p;
                        if (pt.PositionDeviationIsAbsolute)
                        {
                            p = new PointF(tcb0.R.X + cbs[i].Rectangle.X - cbs[0].Rectangle.X, tcb0.R.Y + cbs[i].Rectangle.Y - cbs[0].Rectangle.Y);
                        }
                        else
                        {
                            p = new PointF(tcbs[i - 1].R.X + cbs[i].Rectangle.X - cbs[i - 1].Rectangle.X, tcbs[i - 1].R.Y + cbs[i].Rectangle.Y - cbs[i - 1].Rectangle.Y);
                        }
                        foreach (Pdf.CharBox bt in PdfCharBoxs.Where(x => x.Char == cbs[i].Char))
                        {
                            if (Math.Abs(bt.R.X - p.X) <= pt.PositionDeviation && Math.Abs(bt.R.Y - p.Y) <= pt.PositionDeviation)
                            {
                                tcbs.Add(bt);
                                break;
                            }
                        }
                        if (tcbs.Count - 1 < i)
                        {
                            break;
                        }
                    }
                    if (tcbs.Count == cbs.Count)
                    {
                        SizeF      shift   = new SizeF(tcbs[0].R.X - cbs[0].Rectangle.X, tcbs[0].R.Y - cbs[0].Rectangle.Y);
                        RectangleF actualR = new RectangleF(rectangle.X + shift.Width, rectangle.Y + shift.Height, rectangle.Width, rectangle.Height);
                        if (PdfCharBoxs.FirstOrDefault(x => actualR.Contains(x.R) && !tcbs.Contains(x) && (!pageCollection.ActiveTemplate.IgnoreInvisiblePdfChars || !Pdf.InvisibleCharacters.Contains(x.Char))) == null &&
                            !proceedOnFound(actualR.Location)
                            )
                        {
                            return(true);
                        }
                    }
                }
            }
                return(false);

            case Template.Anchor.Types.OcrText:
            {
                Template.Anchor.OcrText ot = (Template.Anchor.OcrText)a;
                List <Template.Anchor.OcrText.CharBox> cbs = ot.CharBoxs;
                if (cbs.Count < 1)
                {
                    int w = (int)(ActiveTemplateBitmap.Width * Settings.Constants.Image2PdfResolutionRatio - rectangle.Width);
                    int h = (int)(ActiveTemplateBitmap.Height * Settings.Constants.Image2PdfResolutionRatio - rectangle.Height);
                    for (int i = 0; i < w; i++)
                    {
                        for (int j = 0; j < h; j++)
                        {
                            RectangleF actualR = new RectangleF(i, j, rectangle.Width, rectangle.Height);
                            if (ActiveTemplateOcrCharBoxs.FirstOrDefault(x => actualR.Contains(x.R)) == null &&
                                !proceedOnFound(actualR.Location)
                                )
                            {
                                return(true);
                            }
                        }
                    }
                    return(false);
                }
                List <Ocr.CharBox>        searchRectangleOcrCharBoxs;
                IEnumerable <Ocr.CharBox> tcb0s;
                if (ot.OcrEntirePage)
                {
                    searchRectangleOcrCharBoxs = ActiveTemplateOcrCharBoxs;
                    if (ot.SearchRectangleMargin < 0)
                    {
                        tcb0s = searchRectangleOcrCharBoxs.Where(x => x.Char == cbs[0].Char);
                    }
                    else
                    {
                        tcb0s = searchRectangleOcrCharBoxs.Where(x => x.Char == cbs[0].Char && searchRectangle.Contains(x.R));
                    }
                }
                else if (ot.SearchRectangleMargin < 0)
                {
                    searchRectangleOcrCharBoxs = ActiveTemplateOcrCharBoxs;
                    tcb0s = searchRectangleOcrCharBoxs.Where(x => x.Char == cbs[0].Char);
                }
                else
                {
                    //RectangleF contaningRectangle = searchRectangle;
                    //for (int i = 1; i < ot.CharBoxs.Count; i++)
                    //    contaningRectangle = RectangleF.Union(contaningRectangle, getSearchRectangle(ot.CharBoxs[i].Rectangle.GetSystemRectangleF(), a.SearchRectangleMargin));
                    //searchRectangleOcrCharBoxs = Ocr.This.GetCharBoxs(GetRectangleFromActiveTemplateBitmap(contaningRectangle.X / Settings.Constants.Image2PdfResolutionRatio, contaningRectangle.Y / Settings.Constants.Image2PdfResolutionRatio, contaningRectangle.Width / Settings.Constants.Image2PdfResolutionRatio, contaningRectangle.Height / Settings.Constants.Image2PdfResolutionRatio));
                    //PointF searchRectanglePosition = new PointF(contaningRectangle.X < 0 ? 0 : contaningRectangle.X, contaningRectangle.Y < 0 ? 0 : contaningRectangle.Y);
                    //searchRectangleOcrCharBoxs.ForEach(x => { x.R.X += contaningRectangle.X; x.R.Y += contaningRectangle.Y; });
                    //RectangleF mainElementSearchRectangle = new RectangleF(searchRectangle.X - searchRectanglePosition.X, searchRectangle.Y - searchRectanglePosition.Y, searchRectangle.Width, searchRectangle.Height);
                    //tcb0s = searchRectangleOcrCharBoxs.Where(x => x.Char == cbs[0].Char && contaningRectangle.Contains(x.R));

                    searchRectangleOcrCharBoxs = Ocr.This.GetCharBoxs(GetRectangleFromActiveTemplateBitmap(searchRectangle.X / Settings.Constants.Image2PdfResolutionRatio, searchRectangle.Y / Settings.Constants.Image2PdfResolutionRatio, searchRectangle.Width / Settings.Constants.Image2PdfResolutionRatio, searchRectangle.Height / Settings.Constants.Image2PdfResolutionRatio));
                    PointF searchRectanglePosition = new PointF(searchRectangle.X < 0 ? 0 : searchRectangle.X, searchRectangle.Y < 0 ? 0 : searchRectangle.Y);
                    searchRectangleOcrCharBoxs.ForEach(x => { x.R.X += searchRectanglePosition.X; x.R.Y += searchRectanglePosition.Y; });
                    tcb0s = searchRectangleOcrCharBoxs.Where(x => x.Char == cbs[0].Char && searchRectangle.Contains(x.R));
                }
                List <Ocr.CharBox> tcbs = new List <Ocr.CharBox>();
                foreach (Ocr.CharBox tcb0 in tcb0s)
                {
                    tcbs.Clear();
                    tcbs.Add(tcb0);
                    for (int i = 1; i < cbs.Count; i++)
                    {
                        PointF p;
                        if (ot.PositionDeviationIsAbsolute)
                        {
                            p = new PointF(tcb0.R.X + cbs[i].Rectangle.X - cbs[0].Rectangle.X, tcb0.R.Y + cbs[i].Rectangle.Y - cbs[0].Rectangle.Y);
                        }
                        else
                        {
                            p = new PointF(tcbs[i - 1].R.X + cbs[i].Rectangle.X - cbs[i - 1].Rectangle.X, tcbs[i - 1].R.Y + cbs[i].Rectangle.Y - cbs[i - 1].Rectangle.Y);
                        }
                        foreach (Ocr.CharBox bt in searchRectangleOcrCharBoxs.Where(x => x.Char == cbs[i].Char))
                        {
                            if (Math.Abs(bt.R.X - p.X) <= ot.PositionDeviation && Math.Abs(bt.R.Y - p.Y) <= ot.PositionDeviation)
                            {
                                tcbs.Add(bt);
                                break;
                            }
                        }
                        if (tcbs.Count - 1 < i)
                        {
                            break;
                        }
                    }
                    if (tcbs.Count == cbs.Count)
                    {
                        SizeF      shift   = new SizeF(tcbs[0].R.X - cbs[0].Rectangle.X, tcbs[0].R.Y - cbs[0].Rectangle.Y);
                        RectangleF actualR = new RectangleF(rectangle.X + shift.Width, rectangle.Y + shift.Height, rectangle.Width, rectangle.Height);
                        if (searchRectangleOcrCharBoxs.FirstOrDefault(x => actualR.Contains(x.R) && !tcbs.Contains(x)) == null &&
                            !proceedOnFound(actualR.Location)
                            )
                        {
                            return(true);
                        }
                    }
                }
            }
                return(false);

            case Template.Anchor.Types.ImageData:
            {
                Template.Anchor.ImageData idv = (Template.Anchor.ImageData)a;
                Point     searchRectanglePosition;
                ImageData id0;
                if (idv.SearchRectangleMargin < 0)
                {
                    id0 = ActiveTemplateImageData;
                    searchRectanglePosition = new Point(0, 0);
                }
                else
                {
                    searchRectanglePosition = new Point(searchRectangle.X < 0 ? 0 : (int)searchRectangle.X, searchRectangle.Y < 0 ? 0 : (int)searchRectangle.Y);
                    using (Bitmap b = GetRectangleFromActiveTemplateBitmap(searchRectangle.X / Settings.Constants.Image2PdfResolutionRatio, searchRectangle.Y / Settings.Constants.Image2PdfResolutionRatio, searchRectangle.Width / Settings.Constants.Image2PdfResolutionRatio, searchRectangle.Height / Settings.Constants.Image2PdfResolutionRatio))
                    {
                        id0 = new ImageData(b);
                    }
                }
                Point?p_ = idv.Image.FindWithinImage(id0, idv.BrightnessTolerance, idv.DifferentPixelNumberTolerance, idv.FindBestImageMatch);
                if (p_ == null)
                {
                    return(false);
                }
                Point p = (Point)p_;
                return(!proceedOnFound(new PointF(searchRectanglePosition.X + p.X, searchRectanglePosition.Y + p.Y)));
            }

            case Template.Anchor.Types.CvImage:
            {
                Template.Anchor.CvImage civ = (Template.Anchor.CvImage)a;
                Point   searchRectanglePosition;
                CvImage ci0;
                if (civ.SearchRectangleMargin < 0)
                {
                    ci0 = ActiveTemplateCvImage;
                    searchRectanglePosition = new Point(0, 0);
                }
                else
                {
                    searchRectanglePosition = new Point(searchRectangle.X < 0 ? 0 : (int)searchRectangle.X, searchRectangle.Y < 0 ? 0 : (int)searchRectangle.Y);
                    ci0 = new CvImage(GetRectangleFromActiveTemplateBitmap(searchRectangle.X / Settings.Constants.Image2PdfResolutionRatio, searchRectangle.Y / Settings.Constants.Image2PdfResolutionRatio, searchRectangle.Width / Settings.Constants.Image2PdfResolutionRatio, searchRectangle.Height / Settings.Constants.Image2PdfResolutionRatio));
                }
                Point?p_ = civ.Image.FindWithinImage(ci0, civ.Threshold, civ.ScaleDeviation, pageCollection.ActiveTemplate.CvImageScalePyramidStep);
                if (p_ == null)
                {
                    return(false);
                }
                Point p = (Point)p_;
                return(!proceedOnFound(new PointF(searchRectanglePosition.X + p.X, searchRectanglePosition.Y + p.Y)));
            }

            default:
                throw new Exception("Unknown option: " + a.Type);
            }
        }
Пример #8
0
        public List <Match> FindWithinImage(CvImage cvImage, Size padding, float threshold, float scale)
        {
            Image <Gray, byte> template = image.Resize(scale, Inter.Linear);

            return(findWithinImage(template, padding, cvImage.image, threshold));
        }