image.vision\ocr\deskew.mbt

92.5% · 86 / 93 points · 203 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 倾斜校正 ocr/deskew
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 倾斜校正
  7. 7// 基于霍夫变换的倾斜检测与校正
  8. 8
  9. 9///|
  10. 10/// 检测图像倾斜角度(基于水平投影的方差最大化)
  11. 11/// 返回角度(度),正值表示顺时针倾斜
  12. 12pub fn detect_skew_angle(img : @types.Image) -> Double raise @types.LoadError {
  13. 13 // 输入验证
  14. 14● if img.width <= 0 || img.height <= 0 {
  15. 15● raise @types.LoadError::DecodeFailed(
  16. 16 "detect_skew_angle: 图像尺寸必须大于0",
  17. 17 )
  18. 18 }
  19. 19 if img.channels != 1 {
  20. 20● raise @types.LoadError::DecodeFailed(
  21. 21 "detect_skew_angle: 仅支持单通道灰度图",
  22. 22 )
  23. 23 }
  24. 24 let expected_size = img.width * img.height
  25. 25● if img.data.length() != expected_size {
  26. 26● raise @types.LoadError::DecodeFailed(
  27. 27 "detect_skew_angle: 数据大小不匹配,期望 " +
  28. 28● expected_size.to_string() +
  29. 29 ",实际 " +
  30. 30● img.data.length().to_string(),
  31. 31 )
  32. 32 }
  33. 33
  34. 34 let w = img.width
  35. 35 let h = img.height
  36. 36 let data = img.data
  37. 37 // 在 -15 到 +15 度范围内搜索最佳倾斜角度
  38. 38 let mut best_angle = 0.0
  39. 39 let mut best_variance = 0.0
  40. 40 let step = 0.5 // 0.5 度步长
  41. 41 let mut angle = -15.0
  42. 42 while angle <= 15.0 {
  43. 43● let variance = projection_variance_at_angle(data, w, h, angle)
  44. 44 if variance > best_variance {
  45. 45● best_variance = variance
  46. 46 best_angle = angle
  47. 47 }
  48. 48 angle += step
  49. 49 }
  50. 50 best_angle
  51. 51}
  52. 52
  53. 53///|
  54. 54/// 计算指定角度下的水平投影方差
  55. 55fn projection_variance_at_angle(
  56. 56 data : Bytes,
  57. 57 width : Int32,
  58. 58 height : Int32,
  59. 59 angle_deg : Double,
  60. 60) -> Double {
  61. 61● let angle_rad = angle_deg * 3.141592653589793 / 180.0
  62. 62● let cos_a = @math.cos(angle_rad)
  63. 63● let sin_a = @math.sin(angle_rad)
  64. 64 // 计算旋转后的图像尺寸
  65. 65● let new_h = (height.to_double() * cos_a.abs() +
  66. 66● width.to_double() * sin_a.abs()).to_int() +
  67. 67 1
  68. 68 // 计算水平投影
  69. 69● let projection : Array[Int32] = Array::make(new_h, 0)
  70. 70 for y in 0..<height {
  71. 71● for x in 0..<width {
  72. 72● if data[y * width + x].to_int() < 128 {
  73. 73 // 黑色像素(文字)
  74. 74● let new_y = ((x.to_double() - width.to_double() / 2.0) * sin_a +
  75. 75● (y.to_double() - height.to_double() / 2.0) * cos_a +
  76. 76● new_h.to_double() / 2.0).to_int()
  77. 77 if new_y >= 0 && new_y < new_h {
  78. 78● projection[new_y] += 1
  79. 79 }
  80. 80 }
  81. 81 }
  82. 82 }
  83. 83 // 计算投影的方差
  84. 84 let mut sum = 0
  85. 85 let mut sum_sq = 0
  86. 86 let mut count = 0
  87. 87 for i in 0..<new_h {
  88. 88● sum += projection[i]
  89. 89 sum_sq += projection[i] * projection[i]
  90. 90 count += 1
  91. 91 }
  92. 92 if count == 0 {
  93. 93○ return 0.0
  94. 94 }
  95. 95● let mean = sum.to_double() / count.to_double()
  96. 96● let variance = sum_sq.to_double() / count.to_double() - mean * mean
  97. 97 variance
  98. 98}
  99. 99
  100. 100///|
  101. 101/// 倾斜校正(自动检测倾斜角度并旋转)
  102. 102pub fn deskew(img : @types.Image) -> @types.Image raise @types.LoadError {
  103. 103 // 输入验证
  104. 104● if img.width <= 0 || img.height <= 0 {
  105. 105● raise @types.LoadError::DecodeFailed("deskew: 图像尺寸必须大于0")
  106. 106 }
  107. 107 if img.channels != 1 {
  108. 108● raise @types.LoadError::DecodeFailed("deskew: 仅支持单通道灰度图")
  109. 109 }
  110. 110 let expected_size = img.width * img.height
  111. 111● if img.data.length() != expected_size {
  112. 112● raise @types.LoadError::DecodeFailed(
  113. 113 "deskew: 数据大小不匹配,期望 " +
  114. 114● expected_size.to_string() +
  115. 115 ",实际 " +
  116. 116● img.data.length().to_string(),
  117. 117 )
  118. 118 }
  119. 119
  120. 120● let angle = detect_skew_angle(img)
  121. 121● rotate_document(img, -angle)
  122. 122}
  123. 123
  124. 124///|
  125. 125/// 文档旋转(带白色背景填充)
  126. 126/// angle: 旋转角度(度),正值表示逆时针
  127. 127pub fn rotate_document(
  128. 128 img : @types.Image,
  129. 129 angle : Double,
  130. 130) -> @types.Image raise @types.LoadError {
  131. 131 // 输入验证
  132. 132● if img.width <= 0 || img.height <= 0 {
  133. 133○ raise @types.LoadError::DecodeFailed(
  134. 134 "rotate_document: 图像尺寸必须大于0",
  135. 135 )
  136. 136 }
  137. 137 if img.channels < 1 || img.channels > 4 {
  138. 138○ raise @types.LoadError::DecodeFailed(
  139. 139 "rotate_document: 通道数必须在1-4之间",
  140. 140 )
  141. 141 }
  142. 142 let expected_size = img.width * img.height * img.channels
  143. 143● if img.data.length() != expected_size {
  144. 144○ raise @types.LoadError::DecodeFailed(
  145. 145 "rotate_document: 数据大小不匹配,期望 " +
  146. 146○ expected_size.to_string() +
  147. 147 ",实际 " +
  148. 148○ img.data.length().to_string(),
  149. 149 )
  150. 150 }
  151. 151
  152. 152 let w = img.width
  153. 153 let h = img.height
  154. 154 let ch = img.channels
  155. 155 let angle_rad = angle * 3.141592653589793 / 180.0
  156. 156● let cos_a = @math.cos(angle_rad)
  157. 157● let sin_a = @math.sin(angle_rad)
  158. 158 // 计算新图像尺寸
  159. 159● let new_w = (w.to_double() * cos_a.abs() + h.to_double() * sin_a.abs()).to_int() +
  160. 160 1
  161. 161● let new_h = (w.to_double() * sin_a.abs() + h.to_double() * cos_a.abs()).to_int() +
  162. 162 1
  163. 163 // 创建白色背景的新图像
  164. 164● let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\xFF')
  165. 165 let data = img.data
  166. 166 // 反向映射:对新图像的每个像素,找到原图像对应的像素
  167. 167 for y in 0..<new_h {
  168. 168● for x in 0..<new_w {
  169. 169 // 相对于中心的坐标
  170. 170● let cx = x.to_double() - new_w.to_double() / 2.0
  171. 171● let cy = y.to_double() - new_h.to_double() / 2.0
  172. 172 // 反向旋转
  173. 173● let src_x = cx * cos_a + cy * sin_a + w.to_double() / 2.0
  174. 174● let src_y = -cx * sin_a + cy * cos_a + h.to_double() / 2.0
  175. 175 // 双线性插值
  176. 176● let x0 = src_x.to_int()
  177. 177● let y0 = src_y.to_int()
  178. 178 let x1 = x0 + 1
  179. 179 let y1 = y0 + 1
  180. 180● let fx = src_x - x0.to_double()
  181. 181● let fy = src_y - y0.to_double()
  182. 182 if x0 >= 0 && x1 < w && y0 >= 0 && y1 < h {
  183. 183● for c in 0..<ch {
  184. 184● let v00 = data[(y0 * w + x0) * ch + c].to_int().to_double()
  185. 185● let v01 = data[(y0 * w + x1) * ch + c].to_int().to_double()
  186. 186● let v10 = data[(y1 * w + x0) * ch + c].to_int().to_double()
  187. 187● let v11 = data[(y1 * w + x1) * ch + c].to_int().to_double()
  188. 188 // 双线性插值
  189. 189 let v0 = v00 * (1.0 - fx) + v01 * fx
  190. 190 let v1 = v10 * (1.0 - fx) + v11 * fx
  191. 191 let v = v0 * (1.0 - fy) + v1 * fy
  192. 192● out[(y * new_w + x) * ch + c] = @types.clamp_byte(v.to_int())
  193. 193 }
  194. 194 }
  195. 195 }
  196. 196 }
  197. 197 @types.Image::{
  198. 198 width: new_w,
  199. 199 height: new_h,
  200. 200 channels: ch,
  201. 201● data: Bytes::from_array(out),
  202. 202 }
  203. 203}