You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
4.7 KiB

  1. //----------------------------------------------------------------------------
  2. // Copyright (C) 2004-2017 by EMGU Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.IO;
  10. using Emgu.CV;
  11. using Emgu.CV.Structure;
  12. using Emgu.Util;
  13. using System.Diagnostics;
  14. namespace Emgu.CV.OCR
  15. {
  16. /// <summary>
  17. /// The tesseract page iterator
  18. /// </summary>
  19. public class PageIterator : UnmanagedObject
  20. {
  21. internal PageIterator(IntPtr ptr)
  22. {
  23. _ptr = ptr;
  24. }
  25. /// <summary>
  26. /// Returns orientation for the block the iterator points to.
  27. /// </summary>
  28. public Orientation Orientation
  29. {
  30. get
  31. {
  32. Orientation o = new Orientation();
  33. PageOrientation po = PageOrientation.Up;
  34. WritingDirection wd = WritingDirection.LeftToRight;
  35. TextlineOrder tp = TextlineOrder.TopToBottom;
  36. float deskewAngle = 0;
  37. OcrInvoke.TessPageIteratorGetOrientation(_ptr, ref po, ref wd, ref tp, ref deskewAngle);
  38. o.PageOrientation = po;
  39. o.WritingDirection = wd;
  40. o.TextlineOrder = tp;
  41. o.DeskewAngle = deskewAngle;
  42. return o;
  43. }
  44. }
  45. /// <summary>
  46. /// Returns the baseline of the current object at the given level. The baseline is the line that passes through (x1, y1) and (x2, y2). WARNING: with vertical text, baselines may be vertical! Returns null if there is no baseline at the current position.
  47. /// </summary>
  48. /// <param name="level">Page iterator level</param>
  49. /// <returns>The baseline of the current object at the given level</returns>
  50. public LineSegment2D? GetBaseLine(PageIteratorLevel level)
  51. {
  52. int x1 = 0;
  53. int y1 = 0;
  54. int x2 = 0;
  55. int y2 = 0;
  56. bool found = OcrInvoke.TessPageIteratorGetBaseLine(_ptr, level, ref x1, ref y1, ref x2, ref y2);
  57. if (!found)
  58. return null;
  59. else
  60. return new LineSegment2D(new Point(x1, y1), new Point(x2, y2) );
  61. }
  62. /// <summary>
  63. /// Release the page iterator
  64. /// </summary>
  65. protected override void DisposeObject()
  66. {
  67. OcrInvoke.TessPageIteratorRelease(ref _ptr);
  68. }
  69. }
  70. /// <summary>
  71. /// The orientation
  72. /// </summary>
  73. public struct Orientation
  74. {
  75. /// <summary>
  76. /// Page orientation
  77. /// </summary>
  78. public PageOrientation PageOrientation;
  79. /// <summary>
  80. /// Writing direction
  81. /// </summary>
  82. public WritingDirection WritingDirection;
  83. /// <summary>
  84. /// Textline order
  85. /// </summary>
  86. public TextlineOrder TextlineOrder;
  87. /// <summary>
  88. /// after rotating the block so the text orientation is upright, how many radians does one have to rotate the block anti-clockwise for it to be level? -Pi/4 &lt;= deskew_angle &lt;= Pi/4
  89. /// </summary>
  90. public float DeskewAngle;
  91. }
  92. /// <summary>
  93. /// Page orientation
  94. /// </summary>
  95. public enum PageOrientation
  96. {
  97. /// <summary>
  98. /// Up
  99. /// </summary>
  100. Up = 0,
  101. /// <summary>
  102. /// Right
  103. /// </summary>
  104. Right = 1,
  105. /// <summary>
  106. /// Down
  107. /// </summary>
  108. Down = 2,
  109. /// <summary>
  110. /// Left
  111. /// </summary>
  112. Left = 3,
  113. }
  114. /// <summary>
  115. /// Writing direction
  116. /// </summary>
  117. public enum WritingDirection
  118. {
  119. /// <summary>
  120. /// Left to right
  121. /// </summary>
  122. LeftToRight = 0,
  123. /// <summary>
  124. /// Right to left
  125. /// </summary>
  126. RightToLeft = 1,
  127. /// <summary>
  128. /// Top to bottom
  129. /// </summary>
  130. TopToBottom = 2,
  131. }
  132. /// <summary>
  133. /// Textline order
  134. /// </summary>
  135. public enum TextlineOrder
  136. {
  137. /// <summary>
  138. /// Left to right
  139. /// </summary>
  140. LeftToRight = 0,
  141. /// <summary>
  142. /// Right to left
  143. /// </summary>
  144. RightToLeft = 1,
  145. /// <summary>
  146. /// Top to bottom
  147. /// </summary>
  148. TopToBottom = 2,
  149. }
  150. /// <summary>
  151. /// Page iterator level
  152. /// </summary>
  153. public enum PageIteratorLevel
  154. {
  155. /// <summary>
  156. /// Block of text/image/separator line.
  157. /// </summary>
  158. Block,
  159. /// <summary>
  160. /// Paragraph within a block.
  161. /// </summary>
  162. Para,
  163. /// <summary>
  164. /// Line within a paragraph.
  165. /// </summary>
  166. Textline,
  167. /// <summary>
  168. /// Word within a textline.
  169. /// </summary>
  170. Word,
  171. /// <summary>
  172. /// Symbol/character within a word.
  173. /// </summary>
  174. Symbol
  175. }
  176. }