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

Function put_text

rustcv/src/imgproc/drawing.rs:123–163  ·  view source on GitHub ↗

在图像上绘制文字

(mat: &mut Mat, text: &str, org: Point, font_scale: f32, color: Scalar)

Source from the content-addressed store, hash-verified

121
122/// 在图像上绘制文字
123pub fn put_text(mat: &mut Mat, text: &str, org: Point, font_scale: f32, color: Scalar) {
124 let font = get_font();
125 let scale = Scale::uniform(font_scale * 20.0); // 调整倍率以匹配 OpenCV 手感
126 let start = point(org.x as f32, org.y as f32);
127 let glyphs: Vec<PositionedGlyph> = font.layout(text, scale, start).collect();
128
129 let step = mat.step;
130 let rows = mat.rows;
131 let cols = mat.cols;
132 let channels = 3;
133
134 for glyph in glyphs {
135 if let Some(bounding_box) = glyph.pixel_bounding_box() {
136 // 栅格化每个字符
137 glyph.draw(|x, y, v| {
138 // v 是覆盖率 (0.0 - 1.0),用于抗锯齿混合
139 let px = x as i32 + bounding_box.min.x;
140 let py = y as i32 + bounding_box.min.y;
141
142 if px >= 0 && px < cols && py >= 0 && py < rows {
143 let idx = (py as usize) * step + (px as usize) * channels;
144
145 // 简单的 Alpha Blending
146 // Current Pixel
147 let b_old = mat.data[idx] as f32;
148 let g_old = mat.data[idx + 1] as f32;
149 let r_old = mat.data[idx + 2] as f32;
150
151 let alpha = v;
152 let b_new = (color.v0 as f32 * alpha) + (b_old * (1.0 - alpha));
153 let g_new = (color.v1 as f32 * alpha) + (g_old * (1.0 - alpha));
154 let r_new = (color.v2 as f32 * alpha) + (r_old * (1.0 - alpha));
155
156 mat.data[idx] = b_new as u8;
157 mat.data[idx + 1] = g_new as u8;
158 mat.data[idx + 2] = r_new as u8;
159 }
160 });
161 }
162 }
163}

Callers 1

mainFunction · 0.85

Calls 1

get_fontFunction · 0.85

Tested by

no test coverage detected