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