image.process\feature\integral_image.mbt

92.2% · 59 / 64 points · 273 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/// # 积分图像 integral image
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 积分图像(纯 MoonBit)
  7. 7
  8. 8///|
  9. 9/// 积分图像(Satoh 表达)。integral[y][x] = sum of img[0..y, 0..x]。
  10. 10/// 支持 64 位整数和 Float 两种表达。
  11. 11pub struct IntegralImage {
  12. 12 width : Int32 // 原图宽度
  13. 13 height : Int32 // 原图高度
  14. 14 data : Array[Int64] // (width+1) * (height+1) 的积分表,首行首列为 0
  15. 15}
  16. 16
  17. 17///|
  18. 18/// 计算积分图像。仅处理灰度图(channels=1)或 RGB 图(channels=3/4,取首通道)。
  19. 19pub fn integral_image(
  20. 20 img : @types.Image,
  21. 21) -> IntegralImage raise @types.LoadError {
  22. 22 // 输入验证
  23. 23● if img.width <= 0 || img.height <= 0 {
  24. 24● raise @types.LoadError::DecodeFailed(
  25. 25 "integral_image: 图像尺寸必须大于0",
  26. 26 )
  27. 27 }
  28. 28 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  29. 29● raise @types.LoadError::DecodeFailed(
  30. 30 "integral_image: 仅支持 channels=1/3/4",
  31. 31 )
  32. 32 }
  33. 33 let expected_size = img.width * img.height * img.channels
  34. 34● if img.data.length() != expected_size {
  35. 35● raise @types.LoadError::DecodeFailed(
  36. 36 "integral_image: 数据大小不匹配,期望 " +
  37. 37● expected_size.to_string() +
  38. 38 ",实际 " +
  39. 39● img.data.length().to_string(),
  40. 40 )
  41. 41 }
  42. 42 let w = img.width
  43. 43 let h = img.height
  44. 44 let ch = img.channels
  45. 45 let iw = w + 1
  46. 46 let ih = h + 1
  47. 47● let data_int : Array[Int32] = Array::make(w * h * ch, 0)
  48. 48 for i in 0..<(w * h * ch) {
  49. 49● data_int[i] = img.data[i].to_int()
  50. 50 }
  51. 51● let data : Array[Int64] = Array::make(iw * ih, 0L)
  52. 52 for y in 0..<h {
  53. 53● let mut row_sum = 0L
  54. 54 for x in 0..<w {
  55. 55● row_sum += data_int[(y * w + x) * ch].to_int64()
  56. 56 data[(y + 1) * iw + x + 1] = data[y * iw + x + 1] + row_sum
  57. 57 }
  58. 58 }
  59. 59 IntegralImage::{ width: w, height: h, data, }
  60. 60}
  61. 61
  62. 62///|
  63. 63/// 查询积分图像中矩形区域 [x, x+w) × [y, y+h) 的像素和。
  64. 64pub fn integral_sum(
  65. 65 integral : IntegralImage,
  66. 66 x : Int32,
  67. 67 y : Int32,
  68. 68 w : Int32,
  69. 69 h : Int32,
  70. 70) -> Int64 raise @types.LoadError {
  71. 71 // 参数验证
  72. 72● if integral.width <= 0 || integral.height <= 0 {
  73. 73○ raise @types.LoadError::DecodeFailed(
  74. 74 "integral_sum: 积分图像尺寸必须大于0",
  75. 75 )
  76. 76 }
  77. 77 if x < 0 || y < 0 {
  78. 78● raise @types.LoadError::DecodeFailed("integral_sum: x和y必须 >= 0")
  79. 79 }
  80. 80 if w <= 0 || h <= 0 {
  81. 81● raise @types.LoadError::DecodeFailed("integral_sum: w和h必须大于0")
  82. 82 }
  83. 83 if x + w > integral.width || y + h > integral.height {
  84. 84● raise @types.LoadError::DecodeFailed(
  85. 85 "integral_sum: 矩形区域超出图像边界",
  86. 86 )
  87. 87 }
  88. 88
  89. 89 let iw = integral.width + 1
  90. 90 let x2 = x + w
  91. 91 let y2 = y + h
  92. 92 let p00 = integral.data[y * iw + x]
  93. 93 let p10 = integral.data[y * iw + x2]
  94. 94 let p01 = integral.data[y2 * iw + x]
  95. 95 let p11 = integral.data[y2 * iw + x2]
  96. 96 p11 - p10 - p01 + p00
  97. 97}
  98. 98
  99. 99///|
  100. 100/// 使用积分图像快速计算矩形区域均值。
  101. 101pub fn integral_mean(
  102. 102 integral : IntegralImage,
  103. 103 x : Int32,
  104. 104 y : Int32,
  105. 105 w : Int32,
  106. 106 h : Int32,
  107. 107) -> Float raise @types.LoadError {
  108. 108 // 参数验证
  109. 109● if integral.width <= 0 || integral.height <= 0 {
  110. 110○ raise @types.LoadError::DecodeFailed(
  111. 111 "integral_mean: 积分图像尺寸必须大于0",
  112. 112 )
  113. 113 }
  114. 114 if x < 0 || y < 0 {
  115. 115● raise @types.LoadError::DecodeFailed("integral_mean: x和y必须 >= 0")
  116. 116 }
  117. 117 if w < 0 || h < 0 {
  118. 118● raise @types.LoadError::DecodeFailed("integral_mean: w和h必须 >= 0")
  119. 119 }
  120. 120 if w == 0 || h == 0 {
  121. 121● return 0.0F
  122. 122 }
  123. 123 if x + w > integral.width || y + h > integral.height {
  124. 124● raise @types.LoadError::DecodeFailed(
  125. 125 "integral_mean: 矩形区域超出图像边界",
  126. 126 )
  127. 127 }
  128. 128
  129. 129● let sum = integral_sum(integral, x, y, w, h)
  130. 130● Float::from_int(sum.to_int()) / Float::from_int(w * h)
  131. 131}
  132. 132
  133. 133///|
  134. 134/// 平方积分图像(用于快速计算方差)。
  135. 135pub struct IntegralImageSq {
  136. 136 width : Int32
  137. 137 height : Int32
  138. 138 data : Array[Int64] // sum of img^2
  139. 139}
  140. 140
  141. 141///|
  142. 142/// 计算平方积分图像。
  143. 143pub fn integral_image_sq(
  144. 144 img : @types.Image,
  145. 145) -> IntegralImageSq raise @types.LoadError {
  146. 146 // 输入验证
  147. 147● if img.width <= 0 || img.height <= 0 {
  148. 148● raise @types.LoadError::DecodeFailed(
  149. 149 "integral_image_sq: 图像尺寸必须大于0",
  150. 150 )
  151. 151 }
  152. 152 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  153. 153● raise @types.LoadError::DecodeFailed(
  154. 154 "integral_image_sq: 仅支持 channels=1/3/4",
  155. 155 )
  156. 156 }
  157. 157 let expected_size = img.width * img.height * img.channels
  158. 158● if img.data.length() != expected_size {
  159. 159● raise @types.LoadError::DecodeFailed(
  160. 160 "integral_image_sq: 数据大小不匹配,期望 " +
  161. 161● expected_size.to_string() +
  162. 162 ",实际 " +
  163. 163● img.data.length().to_string(),
  164. 164 )
  165. 165 }
  166. 166 let w = img.width
  167. 167 let h = img.height
  168. 168 let ch = img.channels
  169. 169 let iw = w + 1
  170. 170 let ih = h + 1
  171. 171● let data_int : Array[Int32] = Array::make(w * h * ch, 0)
  172. 172 for i in 0..<(w * h * ch) {
  173. 173● data_int[i] = img.data[i].to_int()
  174. 174 }
  175. 175● let data : Array[Int64] = Array::make(iw * ih, 0L)
  176. 176 for y in 0..<h {
  177. 177● let mut row_sum = 0L
  178. 178 for x in 0..<w {
  179. 179● let v = data_int[(y * w + x) * ch].to_int64()
  180. 180 row_sum += v * v
  181. 181 data[(y + 1) * iw + x + 1] = data[y * iw + x + 1] + row_sum
  182. 182 }
  183. 183 }
  184. 184 IntegralImageSq::{ width: w, height: h, data, }
  185. 185}
  186. 186
  187. 187///|
  188. 188/// 使用积分图像计算矩形区域方差。
  189. 189/// 需要同时提供积分图像和平方积分图像。
  190. 190pub fn integral_variance(
  191. 191 integral : IntegralImage,
  192. 192 integral_sq : IntegralImageSq,
  193. 193 x : Int32,
  194. 194 y : Int32,
  195. 195 w : Int32,
  196. 196 h : Int32,
  197. 197) -> Float raise @types.LoadError {
  198. 198 // 参数验证
  199. 199● if integral.width <= 0 || integral.height <= 0 {
  200. 200○ raise @types.LoadError::DecodeFailed(
  201. 201 "integral_variance: 积分图像尺寸必须大于0",
  202. 202 )
  203. 203 }
  204. 204 if integral_sq.width <= 0 || integral_sq.height <= 0 {
  205. 205○ raise @types.LoadError::DecodeFailed(
  206. 206 "integral_variance: 平方积分图像尺寸必须大于0",
  207. 207 )
  208. 208 }
  209. 209 if integral.width != integral_sq.width ||
  210. 210 integral.height != integral_sq.height {
  211. 211● raise @types.LoadError::DecodeFailed(
  212. 212 "integral_variance: 积分图像和平方积分图像尺寸必须相同",
  213. 213 )
  214. 214 }
  215. 215 if x < 0 || y < 0 {
  216. 216● raise @types.LoadError::DecodeFailed("integral_variance: x和y必须 >= 0")
  217. 217 }
  218. 218 if w <= 0 || h <= 0 {
  219. 219● raise @types.LoadError::DecodeFailed(
  220. 220 "integral_variance: w和h必须大于0",
  221. 221 )
  222. 222 }
  223. 223 if x + w > integral.width || y + h > integral.height {
  224. 224● raise @types.LoadError::DecodeFailed(
  225. 225 "integral_variance: 矩形区域超出图像边界",
  226. 226 )
  227. 227 }
  228. 228
  229. 229● let n = Float::from_int(w * h)
  230. 230● let sum = Float::from_int(integral_sum(integral, x, y, w, h).to_int())
  231. 231● let sum_sq = Float::from_int(
  232. 232● integral_sum_sq(integral_sq, x, y, w, h).to_int(),
  233. 233 )
  234. 234 let mean = sum / n
  235. 235 sum_sq / n - mean * mean
  236. 236}
  237. 237
  238. 238///|
  239. 239/// 查询平方积分图像中矩形区域的像素平方和。
  240. 240pub fn integral_sum_sq(
  241. 241 integral_sq : IntegralImageSq,
  242. 242 x : Int32,
  243. 243 y : Int32,
  244. 244 w : Int32,
  245. 245 h : Int32,
  246. 246) -> Int64 raise @types.LoadError {
  247. 247 // 参数验证
  248. 248● if integral_sq.width <= 0 || integral_sq.height <= 0 {
  249. 249○ raise @types.LoadError::DecodeFailed(
  250. 250 "integral_sum_sq: 平方积分图像尺寸必须大于0",
  251. 251 )
  252. 252 }
  253. 253 if x < 0 || y < 0 {
  254. 254● raise @types.LoadError::DecodeFailed("integral_sum_sq: x和y必须 >= 0")
  255. 255 }
  256. 256 if w <= 0 || h <= 0 {
  257. 257● raise @types.LoadError::DecodeFailed("integral_sum_sq: w和h必须大于0")
  258. 258 }
  259. 259 if x + w > integral_sq.width || y + h > integral_sq.height {
  260. 260● raise @types.LoadError::DecodeFailed(
  261. 261 "integral_sum_sq: 矩形区域超出图像边界",
  262. 262 )
  263. 263 }
  264. 264
  265. 265 let iw = integral_sq.width + 1
  266. 266 let x2 = x + w
  267. 267 let y2 = y + h
  268. 268 let p00 = integral_sq.data[y * iw + x]
  269. 269 let p10 = integral_sq.data[y * iw + x2]
  270. 270 let p01 = integral_sq.data[y2 * iw + x]
  271. 271 let p11 = integral_sq.data[y2 * iw + x2]
  272. 272 p11 - p10 - p01 + p00
  273. 273}