image.process\detection\object_detection.mbt

82.2% · 60 / 73 points · 222 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/// # 目标检测 object detection
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 目标检测基础(v12.4.0)
  7. 7// 检测框 + IoU + NMS 非极大值抑制 + 检测框绘制
  8. 8
  9. 9///|
  10. 10/// 检测框
  11. 11pub(all) struct BoundingBox {
  12. 12 x : Int32 // 左上角 x
  13. 13 y : Int32 // 左上角 y
  14. 14 width : Int32 // 宽度
  15. 15 height : Int32 // 高度
  16. 16 score : Double // 置信度
  17. 17 class_id : Int32 // 类别 ID
  18. 18}
  19. 19
  20. 20///|
  21. 21/// 创建检测框
  22. 22pub fn bbox_new(
  23. 23 x : Int32,
  24. 24 y : Int32,
  25. 25 width : Int32,
  26. 26 height : Int32,
  27. 27 score~ : Double = 0.0,
  28. 28 class_id~ : Int32 = 0,
  29. 29) -> BoundingBox raise @types.LoadError {
  30. 30 // 输入验证
  31. 31● if width <= 0 {
  32. 32● raise @types.LoadError::DecodeFailed("bbox_new: width必须大于0")
  33. 33 }
  34. 34 if height <= 0 {
  35. 35● raise @types.LoadError::DecodeFailed("bbox_new: height必须大于0")
  36. 36 }
  37. 37 if score < 0.0 || score > 1.0 {
  38. 38● raise @types.LoadError::DecodeFailed("bbox_new: score必须在0-1之间")
  39. 39 }
  40. 40 if class_id < 0 {
  41. 41● raise @types.LoadError::DecodeFailed(
  42. 42 "bbox_new: class_id必须大于等于0",
  43. 43 )
  44. 44 }
  45. 45
  46. 46 BoundingBox::{ x, y, width, height, score, class_id, }
  47. 47}
  48. 48
  49. 49///|
  50. 50/// 计算两个检测框的 IoU(交并比)
  51. 51pub fn bbox_iou(
  52. 52 a : BoundingBox,
  53. 53 b : BoundingBox,
  54. 54) -> Double raise @types.LoadError {
  55. 55 // 输入验证
  56. 56● if a.width <= 0 || a.height <= 0 {
  57. 57○ raise @types.LoadError::DecodeFailed(
  58. 58 "bbox_iou: 检测框a的尺寸必须大于0",
  59. 59 )
  60. 60 }
  61. 61 if b.width <= 0 || b.height <= 0 {
  62. 62○ raise @types.LoadError::DecodeFailed(
  63. 63 "bbox_iou: 检测框b的尺寸必须大于0",
  64. 64 )
  65. 65 }
  66. 66
  67. 67● let x1 = a.x.max(b.x)
  68. 68● let y1 = a.y.max(b.y)
  69. 69● let x2 = (a.x + a.width).min(b.x + b.width)
  70. 70● let y2 = (a.y + a.height).min(b.y + b.height)
  71. 71● let intersection_w = (x2 - x1).max(0)
  72. 72● let intersection_h = (y2 - y1).max(0)
  73. 73● let intersection = intersection_w.to_double() * intersection_h.to_double()
  74. 74● let area_a = a.width.to_double() * a.height.to_double()
  75. 75● let area_b = b.width.to_double() * b.height.to_double()
  76. 76 let union = area_a + area_b - intersection
  77. 77 if union > 0.0 {
  78. 78● intersection / union
  79. 79 } else {
  80. 80○ 0.0
  81. 81 }
  82. 82}
  83. 83
  84. 84///|
  85. 85/// NMS 非极大值抑制
  86. 86pub fn nms(
  87. 87 boxes : Array[BoundingBox],
  88. 88 iou_threshold~ : Double = 0.5,
  89. 89) -> Array[BoundingBox] raise @types.LoadError {
  90. 90 // 输入验证
  91. 91● if boxes.length() == 0 {
  92. 92● raise @types.LoadError::DecodeFailed("nms: 检测框列表不能为空")
  93. 93 }
  94. 94 if iou_threshold < 0.0 || iou_threshold > 1.0 {
  95. 95○ raise @types.LoadError::DecodeFailed("nms: iou_threshold必须在0-1之间")
  96. 96 }
  97. 97
  98. 98 let threshold = iou_threshold
  99. 99 // 按置信度降序排序(选择排序)
  100. 100 let sorted = boxes
  101. 101● let n = sorted.length()
  102. 102 for i in 0..<n {
  103. 103● let mut max_idx = i
  104. 104 for j in (i + 1)..<n {
  105. 105● if sorted[j].score > sorted[max_idx].score {
  106. 106○ max_idx = j
  107. 107 }
  108. 108 }
  109. 109 if max_idx != i {
  110. 110○ let tmp = sorted[i]
  111. 111 sorted[i] = sorted[max_idx]
  112. 112 sorted[max_idx] = tmp
  113. 113 }
  114. 114 }
  115. 115 // NMS
  116. 116● let suppressed : Array[Bool] = Array::make(n, false)
  117. 117 let result : Array[BoundingBox] = []
  118. 118 for i in 0..<n {
  119. 119● if suppressed[i] {
  120. 120● continue
  121. 121 }
  122. 122● result.push(sorted[i])
  123. 123 for j in (i + 1)..<n {
  124. 124● if suppressed[j] {
  125. 125○ continue
  126. 126 }
  127. 127● if bbox_iou(sorted[i], sorted[j]) > threshold {
  128. 128● suppressed[j] = true
  129. 129 }
  130. 130 }
  131. 131 }
  132. 132 result
  133. 133}
  134. 134
  135. 135///|
  136. 136/// 将检测结果可视化为带框图像
  137. 137pub fn draw_detections(
  138. 138 img : @types.Image,
  139. 139 boxes : Array[BoundingBox],
  140. 140) -> @types.Image raise @types.LoadError {
  141. 141 // 输入验证
  142. 142● if img.width <= 0 || img.height <= 0 {
  143. 143● raise @types.LoadError::DecodeFailed(
  144. 144 "draw_detections: 图像尺寸必须大于0",
  145. 145 )
  146. 146 }
  147. 147 if img.channels < 1 || img.channels > 4 {
  148. 148○ raise @types.LoadError::DecodeFailed(
  149. 149 "draw_detections: 通道数必须在1-4之间",
  150. 150 )
  151. 151 }
  152. 152 let expected_size = img.width * img.height * img.channels
  153. 153● if img.data.length() != expected_size {
  154. 154○ raise @types.LoadError::DecodeFailed(
  155. 155 "draw_detections: 数据大小不匹配,期望 " +
  156. 156○ expected_size.to_string() +
  157. 157 ",实际 " +
  158. 158○ img.data.length().to_string(),
  159. 159 )
  160. 160 }
  161. 161
  162. 162 let w = img.width
  163. 163 let h = img.height
  164. 164 let channels = img.channels
  165. 165● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  166. 166 // 复制原图
  167. 167 for i in 0..<(w * h * channels) {
  168. 168● out_data[i] = img.data[i]
  169. 169 }
  170. 170 // 绘制检测框(红色,2像素宽)
  171. 171 for bbox in boxes {
  172. 172● let color = if channels >= 3 {
  173. 173● [b'\xFF', b'\x00', b'\x00']
  174. 174 } else {
  175. 175○ [b'\xFF']
  176. 176 }
  177. 177 // 上下边
  178. 178 for x in bbox.x..<(bbox.x + bbox.width) {
  179. 179● for dy in 0..<2 {
  180. 180● let y_top = bbox.y + dy
  181. 181 let y_bottom = bbox.y + bbox.height - 1 - dy
  182. 182 if y_top >= 0 && y_top < h && x >= 0 && x < w {
  183. 183● let idx = (y_top * w + x) * channels
  184. 184● for c in 0..<channels.min(color.length()) {
  185. 185● out_data[idx + c] = color[c]
  186. 186 }
  187. 187 }
  188. 188 if y_bottom >= 0 && y_bottom < h && x >= 0 && x < w {
  189. 189● let idx = (y_bottom * w + x) * channels
  190. 190● for c in 0..<channels.min(color.length()) {
  191. 191● out_data[idx + c] = color[c]
  192. 192 }
  193. 193 }
  194. 194 }
  195. 195 }
  196. 196 // 左右边
  197. 197 for y in bbox.y..<(bbox.y + bbox.height) {
  198. 198● for dx in 0..<2 {
  199. 199● let x_left = bbox.x + dx
  200. 200 let x_right = bbox.x + bbox.width - 1 - dx
  201. 201 if x_left >= 0 && x_left < w && y >= 0 && y < h {
  202. 202● let idx = (y * w + x_left) * channels
  203. 203● for c in 0..<channels.min(color.length()) {
  204. 204● out_data[idx + c] = color[c]
  205. 205 }
  206. 206 }
  207. 207 if x_right >= 0 && x_right < w && y >= 0 && y < h {
  208. 208● let idx = (y * w + x_right) * channels
  209. 209● for c in 0..<channels.min(color.length()) {
  210. 210● out_data[idx + c] = color[c]
  211. 211 }
  212. 212 }
  213. 213 }
  214. 214 }
  215. 215 }
  216. 216 @types.Image::{
  217. 217 width: w,
  218. 218 height: h,
  219. 219 channels,
  220. 220● data: Bytes::from_array(out_data),
  221. 221 }
  222. 222}