MCPcopy Create free account
hub / github.com/RustCV/RustCV / rectangle

Function rectangle

rustcv/src/imgproc/drawing.rs:67–106  ·  view source on GitHub ↗

在 Mat 上绘制矩形 (In-place) 这是一个手动实现的高性能版本,直接操作 Vec ,避免了任何类型转换。

(mat: &mut Mat, rect: Rect, color: Scalar, thickness: i32)

Source from the content-addressed store, hash-verified

65///
66/// 这是一个手动实现的高性能版本,直接操作 Vec<u8>,避免了任何类型转换。
67pub fn rectangle(mat: &mut Mat, rect: Rect, color: Scalar, thickness: i32) {
68 let x_min = rect.x.max(0);
69 let y_min = rect.y.max(0);
70 let x_max = (rect.x + rect.width).min(mat.cols);
71 let y_max = (rect.y + rect.height).min(mat.rows);
72
73 if x_min >= x_max || y_min >= y_max {
74 return;
75 }
76
77 // 辅助闭包:设置像素
78 // 注意:Rust 借用检查器可能不喜欢我们在循环里多次借用 mat.data,所以我们用 raw slice 或 index
79 // 为了代码清晰,这里用 safe index,release 模式下会被优化
80 let set_pixel = |data: &mut Vec<u8>, step: usize, r: i32, c: i32, color: Scalar| {
81 let idx = (r as usize) * step + (c as usize) * 3;
82 if idx + 2 < data.len() {
83 data[idx] = color.v0;
84 data[idx + 1] = color.v1;
85 data[idx + 2] = color.v2;
86 }
87 };
88
89 let step = mat.step;
90
91 // 绘制上下边
92 for c in x_min..x_max {
93 for t in 0..thickness {
94 set_pixel(&mut mat.data, step, y_min + t, c, color); // Top
95 set_pixel(&mut mat.data, step, y_max - 1 - t, c, color); // Bottom
96 }
97 }
98
99 // 绘制左右边
100 for r in y_min..y_max {
101 for t in 0..thickness {
102 set_pixel(&mut mat.data, step, r, x_min + t, color); // Left
103 set_pixel(&mut mat.data, step, r, x_max - 1 - t, color); // Right
104 }
105 }
106}
107
108// --- 文本渲染 ---
109

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected