/// <summary>
 /// return true if the oContainedRect contained this
 /// </summary>
 /// <param name="oContainedRect">The o contained rect.</param>
 /// <returns></returns>
 public Boolean IsInside(TOCRRect oContainedRect)
 {
     return((m_iLeft + s_iXThreshold >= oContainedRect.Left) &&
            (Right - s_iXThreshold <= oContainedRect.Right) &&
            (m_iTop + s_iYThreshold >= oContainedRect.Top) &&
            (Bottom - s_iYThreshold <= oContainedRect.Bottom));
 }
        // Check 2 rect overlapping & return the overlapping data
        //   oRect - (input) The rect to check
        //   oOverlapRect - (output) The overlapping area
        /// <summary>
        /// Overlaps the rect.
        /// </summary>
        /// <param name="oRect">The o rect.</param>
        /// <param name="oOverlapRect">The o overlap rect.</param>
        /// <returns></returns>
        public Boolean OverlapRect(TOCRRect oRect,
                                   TOCRRect oOverlapRect)
        {
            if (!IsOverlap(oRect))
            {
                return(false);
            }

            int iNewLeft = Math.Max(m_iLeft, oRect.m_iLeft);
            int iNewTop  = Math.Max(m_iTop, oRect.m_iTop);

            oOverlapRect.m_iWidth  = Math.Min(Right, oRect.Right) - iNewLeft;
            oOverlapRect.m_iHeight = Math.Min(Bottom, oRect.Bottom) - iNewTop;
            oOverlapRect.m_iLeft   = iNewLeft;
            oOverlapRect.m_iTop    = iNewTop;

            // In case there is intersect only with the help of the thresholds the we create "small" rectangle in the intersect area
            if (oOverlapRect.m_iWidth <= 0)
            {
                oOverlapRect.m_iWidth = 1;
            }
            if (oOverlapRect.m_iHeight <= 0)
            {
                oOverlapRect.m_iHeight = 1;
            }

            return(true);
        }
        // Add a rectangle (Union rectangles)
        /// <summary>
        /// Adds the specified o rect.
        /// </summary>
        /// <param name="oRect">The o rect.</param>
        public void Add(TOCRRect oRect)
        {
            // Do nothing if the add rect is empty
            if (oRect.IsEmpty())
            {
                return;
            }

            // Copy the add rect of the target rect is empty
            if (IsEmpty())
            {
                m_iLeft   = oRect.m_iLeft;
                m_iTop    = oRect.m_iTop;
                m_iWidth  = oRect.m_iWidth;
                m_iHeight = oRect.m_iHeight;
            }
            else
            {
                // Union rectangles...
                int iNewRight  = Math.Max(Right, oRect.Right);
                int iNewBottom = Math.Max(Bottom, oRect.Bottom);
                m_iLeft   = Math.Min(m_iLeft, oRect.m_iLeft);
                m_iTop    = Math.Min(m_iTop, oRect.m_iTop);
                m_iWidth  = iNewRight - m_iLeft;
                m_iHeight = iNewBottom - m_iTop;
            }
        }
        //
        /// <summary>
        /// Determines whether the specified and other word are related.
        /// </summary>
        /// <param name="oOtherWord">The other word.</param>
        /// <returns>
        /// True if other word is "related" to the current word
        /// </returns>
        public Boolean IsRelated(TWord oOtherWord)
        {
            // First check we do not have ant chance for intersection
            if (!m_oRectangle.IsOverlap(oOtherWord.m_oRectangle))
            {
                return(false);
            }

            foreach (TChar oChar in this)
            {
                foreach (TChar oOtherChar in oOtherWord)
                {
                    TOCRRect oOverlapRect = new TOCRRect();
                    if (oChar.Rect.OverlapRect(oOtherChar.Rect, oOverlapRect))
                    {
                        if (oOverlapRect.Area * 2 > oChar.Rect.Area)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="TOCRRect"/> class.
 /// </summary>
 /// <param name="oRect">The o rect.</param>
 public TOCRRect(TOCRRect oRect)
 {
     m_iLeft   = oRect.m_iLeft;
     m_iTop    = oRect.m_iTop;
     m_iWidth  = oRect.m_iWidth;
     m_iHeight = oRect.m_iHeight;
 }
 /// <summary>
 /// Rotate the rectangle to be horizontal
 /// </summary>
 /// <param name="oRect">The o rect.</param>
 /// <returns></returns>
 public TOCRRect Deskew(TOCRRect oRect)
 {
     return(new TOCRRect(
                oRect.Left + (oRect.Top * m_iDeskewY) / m_iDeskewX,
                oRect.Top - (oRect.Left * m_iDeskewY) / m_iDeskewX,
                oRect.Width,
                oRect.Height));
 }
        //
        // Public methods
        //

        /// <summary>
        /// Initializes a new instance of the <see cref="TWord" /> class.
        /// </summary>
        public TWord()
        {
            m_oChars      = new TChars();
            m_oRectangle  = new TOCRRect();
            m_sWordData   = "";
            m_iConfidance = 0;
            m_iStyle      = 0;
        }
        //
        // Public methods
        //

        /// <summary>
        /// Initializes a new instance of the <see cref="TPage"/> class.
        /// </summary>
        public TPage()
        {
            m_oLines = new List <TLine>();
            m_oRect  = new TOCRRect();
            // Default values...
            m_iDeskewY    = 0;
            m_iDeskewX    = 4000;
            m_iResolution = 1;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="TChar"/> class.
 /// </summary>
 /// <param name="cCharData">The c character data.</param>
 /// <param name="iConfidance">The i confidance.</param>
 /// <param name="oRectangle">The o rectangle.</param>
 public TChar(
     char cCharData,
     short iConfidance,
     TOCRRect oRectangle)
     : this()
 {
     m_oRectangle = new TOCRRect(oRectangle);
     m_oCharCandidate.Add(new TBasicChar(cCharData, iConfidance));
 }
        /// <summary>
        /// Determines whether the specified o other rect is equal.
        /// </summary>
        /// <param name="oOtherRect">The o other rect.</param>
        /// <returns></returns>
        public Boolean IsEqual(TOCRRect oOtherRect)
        {
            int thresholdX = Math.Min(s_iXThreshold, Width / 2);
            int thresholdY = Math.Min(s_iYThreshold, Height / 2);

            return((Math.Abs(m_iLeft - oOtherRect.m_iLeft) <= thresholdX) &&
                   (Math.Abs(Right - oOtherRect.Right) <= thresholdX) &&
                   (Math.Abs(m_iTop - oOtherRect.m_iTop) <= thresholdY) &&
                   (Math.Abs(Bottom - oOtherRect.Bottom) <= thresholdY));
        }
Beispiel #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TChar"/> class.
 /// </summary>
 /// <param name="cCharData">The c character data.</param>
 /// <param name="iConfidance">The i confidance.</param>
 /// <param name="cCharDataCand2">The c character data cand2.</param>
 /// <param name="iConfidance2">The i confidance2.</param>
 /// <param name="oRectangle">The o rectangle.</param>
 public TChar(
     char cCharData,
     short iConfidance,
     char cCharDataCand2,
     short iConfidance2,
     TOCRRect oRectangle)
     : this(cCharData, iConfidance, oRectangle)
 {
     m_oCharCandidate.Add(new TBasicChar(cCharDataCand2, iConfidance2));
 }
 // return true if the 2 rect have overlapping area (including the threshold )
 /// <summary>
 /// Determines whether the specified o rect is overlap.
 /// </summary>
 /// <param name="oRect">The o rect.</param>
 /// <returns></returns>
 public Boolean IsOverlap(TOCRRect oRect)
 {
     if ((m_iLeft - s_iXThreshold > oRect.Right) ||
         (Right + s_iXThreshold < oRect.m_iLeft) ||
         (m_iTop - s_iYThreshold > oRect.Bottom) ||
         (Bottom + s_iYThreshold < oRect.m_iTop))
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
Beispiel #13
0
        /// <summary>
        /// Recieves BinaryReader and page, and initialize the page from the reader.
        /// </summary>
        public void ReadData(System.IO.BinaryReader binaryReader)
        {
            m_iDeskewY     = binaryReader.ReadInt32();
            m_iDeskewX     = binaryReader.ReadInt32();
            m_iResolution  = binaryReader.ReadInt32();
            m_iImageWidth  = binaryReader.ReadInt32();
            m_iImageHeight = binaryReader.ReadInt32();

            // Read Rectangle data
            int rectangleTop    = binaryReader.ReadInt32();
            int rectangleHeight = binaryReader.ReadInt32();
            int rectangleLeft   = binaryReader.ReadInt32();
            int rectangleWidth  = binaryReader.ReadInt32();

            m_oRect = new TOCRRect(rectangleLeft, rectangleTop, rectangleWidth, rectangleHeight);
        }
Beispiel #14
0
            public int Compare(TLine x, TLine y)
            {
                TLine oLine1 = x;
                TLine oLine2 = y;

                if ((oLine1.Words.Count > 0) && (oLine2.Words.Count > 0))
                {
                    TOCRRect oRect1 = m_oPage.Deskew(oLine1.Word(0).Rect);
                    TOCRRect oRect2 = m_oPage.Deskew(oLine2.Word(0).Rect);
                    return(oRect1.Top - oRect2.Top);
                }
                else
                {
                    TOCRRect oRect1 = m_oPage.Deskew(oLine1.Rect);
                    TOCRRect oRect2 = m_oPage.Deskew(oLine2.Rect);
                    return(oRect1.Top - oRect2.Top);
                }
            }
Beispiel #15
0
 // Add candidate (remove duplicated is exist)
 /// <summary>
 /// Adds the candidate.
 /// </summary>
 /// <param name="oNewCandidate">The o new candidate.</param>
 public void AddCandidate(TChar oNewCandidate)
 {
     foreach (TBasicChar oNewChar in oNewCandidate)
     {
         Boolean bAdd = false;
         int     iIdx = -1;
         // Check if we already have candidate with the same data
         for (int i = 0; (i < m_oCharCandidate.Count) && (iIdx < 0); i++)
         {
             TBasicChar oChar = (TBasicChar)m_oCharCandidate[i];
             if (oChar.CharData == oNewChar.CharData)
             {
                 iIdx = i;
             }
         }
         //int iIdx = m_oCharCandidate.BinarySearch( oNewChar ) ; xxx
         if (iIdx >= 0)
         {
             TBasicChar oExistChar = (TBasicChar)(m_oCharCandidate[iIdx]);
             // The same character have lower confidence - so we now replace the old with the new
             if (oExistChar.CompareTo(oNewChar) > 0)
             {
                 m_oCharCandidate.RemoveAt(iIdx);
                 bAdd = true;
             }
         }
         else
         {
             bAdd = true;
         }
         if (bAdd)
         {
             // Add the new candidate (& sort acurding the candidates confidence )
             m_oCharCandidate.Add(oNewChar);
             m_oCharCandidate.Sort();
             // In case the new candidate is the best, then we take also the char rectangle.
             if (m_oCharCandidate[0] == oNewChar)
             {
                 m_oRectangle = oNewCandidate.m_oRectangle;
             }
         }
     }
 }
Beispiel #16
0
        //
        // Public methods
        //

        /// <summary>
        /// Initializes a new instance of the <see cref="TChar"/> class.
        /// </summary>
        public TChar()
        {
            m_oRectangle     = new TOCRRect();
            m_oCharCandidate = new List <TBasicChar>();
        }
 /// <summary>
 /// Determines whether [is equal bottom] [the specified o other rect].
 /// </summary>
 /// <param name="oOtherRect">The o other rect.</param>
 /// <returns></returns>
 public Boolean IsEqualBottom(TOCRRect oOtherRect)
 {
     return(Math.Abs(Bottom - oOtherRect.Bottom) <= s_iYThreshold);
 }
 /// <summary>
 /// Determines whether [is equal left] [the specified o other rect].
 /// </summary>
 /// <param name="oOtherRect">The o other rect.</param>
 /// <returns></returns>
 public Boolean IsEqualLeft(TOCRRect oOtherRect)
 {
     return(Math.Abs(m_iLeft - oOtherRect.m_iLeft) <= s_iXThreshold);
 }
 /// <summary>
 /// Determines whether [is equal right] [the specified o other rect].
 /// </summary>
 /// <param name="oOtherRect">The o other rect.</param>
 /// <returns></returns>
 public Boolean IsEqualRight(TOCRRect oOtherRect)
 {
     return(Math.Abs(Right - oOtherRect.Right) <= s_iXThreshold);
 }
 /// <summary>
 /// Determines whether [is equal top] [the specified o other rect].
 /// </summary>
 /// <param name="oOtherRect">The o other rect.</param>
 /// <returns></returns>
 public Boolean IsEqualTop(TOCRRect oOtherRect)
 {
     return(Math.Abs(m_iTop - oOtherRect.m_iTop) <= s_iYThreshold);
 }
Beispiel #21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TLine"/> class.
 /// </summary>
 public TLine()
 {
     m_oWords            = new System.Collections.Generic.List <TWord>();
     m_oRect             = new TOCRRect();
     m_oComparerWordLeft = new TWord.TComparerLeft();
 }
Beispiel #22
0
        private void AddWord(TWord oNewWord)
        {
            // Use the Deskew method to use the same Y-Axis
            TOCRRect oNewWordRect = Deskew(oNewWord.Rect);

            // Build candidate lines
            List <TLine> oCandidateLines = new List <TLine>();

            foreach (TLine oLine in m_oLines)
            {
                if (Deskew(oLine.Rect).Bottom + MMtoPixel(20) < oNewWordRect.Top)
                {
                    continue;
                }

                if (Deskew(oLine.Rect).Top - MMtoPixel(20) > oNewWordRect.Bottom)
                {
                    break;
                }

                oCandidateLines.Add(oLine);
            }

            // Try 1: Find closer matching on top
            foreach (TLine oLine in oCandidateLines)
            {
                foreach (TWord oWord in oLine)
                {
                    TOCRRect oWordRect = Deskew(oWord.Rect);
                    // If the word in
                    if (oWordRect.IsEqualTop(oNewWordRect))
                    {
                        oLine.AddWord(oNewWord);
                        return;
                    }
                }
            }

            // Now we remove lines that one word is above or beneath the new word
            List <TLine> oCandidateLines2 = new List <TLine>();

            foreach (TLine oLine in oCandidateLines)
            {
                bool bLineOk = true;
                foreach (TWord oWord in oLine)
                {
                    TOCRRect oWordRect = Deskew(oWord.Rect);

                    // Word is above or beneath the new word
                    if ((oNewWordRect.Top > oWordRect.Bottom) || (oNewWordRect.Bottom < oWordRect.Top))
                    {
                        bLineOk = false;
                        break;
                    }
                }
                if (bLineOk)
                {
                    oCandidateLines2.Add(oLine);
                }
            }


            // Try 2: Find closer matching on bottom
            foreach (TLine oLine in oCandidateLines2)
            {
                foreach (TWord oWord in oLine)
                {
                    TOCRRect oWordRect = Deskew(oWord.Rect);
                    // If the word in
                    if (oWordRect.IsEqualBottom(oNewWordRect))
                    {
                        oLine.AddWord(oNewWord);
                        return;
                    }
                }
            }

            // Try 3: Find far matching from top
            foreach (TLine oLine in oCandidateLines2)
            {
                foreach (TWord oWord in oLine)
                {
                    TOCRRect oWordRect = Deskew(oWord.Rect);
                    if (Math.Abs(oNewWordRect.Top - oWordRect.Top) <=
                        Math.Min(MMtoPixel(2), oWordRect.Height / 2))
                    {
                        oLine.AddWord(oNewWord);
                        return;
                    }
                }
            }
            // Try 4: Find far matching from bottom
            foreach (TLine oLine in oCandidateLines2)
            {
                foreach (TWord oWord in oLine)
                {
                    TOCRRect oWordRect = Deskew(oWord.Rect);

                    if (Math.Abs(oNewWordRect.Bottom - oWordRect.Bottom) <=
                        Math.Min(MMtoPixel(2), oWordRect.Height / 2))
                    {
                        oLine.AddWord(oNewWord);
                        return;
                    }
                }
            }

            // We do not found any line that can take the word, so we create a new line
            TLine oNewLine = new TLine();

            oNewLine.AddWord(oNewWord);
            AddLine(oNewLine);
        }