image.vision\ocr\deskew.mbt
92.5% · 86 / 93 points · 203 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 倾斜校正 ocr/deskew - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 倾斜校正 - 7
// 基于霍夫变换的倾斜检测与校正 - 8
- 9
///| - 10
/// 检测图像倾斜角度(基于水平投影的方差最大化) - 11
/// 返回角度(度),正值表示顺时针倾斜 - 12
pub fn detect_skew_angle(img : @types.Image) -> Double raise @types.LoadError { - 13
// 输入验证 - 14●
if img.width <= 0 || img.height <= 0 { - 15●
raise @types.LoadError::DecodeFailed( - 16
"detect_skew_angle: 图像尺寸必须大于0", - 17
) - 18
} - 19
if img.channels != 1 { - 20●
raise @types.LoadError::DecodeFailed( - 21
"detect_skew_angle: 仅支持单通道灰度图", - 22
) - 23
} - 24
let expected_size = img.width * img.height - 25●
if img.data.length() != expected_size { - 26●
raise @types.LoadError::DecodeFailed( - 27
"detect_skew_angle: 数据大小不匹配,期望 " + - 28●
expected_size.to_string() + - 29
",实际 " + - 30●
img.data.length().to_string(), - 31
) - 32
} - 33
- 34
let w = img.width - 35
let h = img.height - 36
let data = img.data - 37
// 在 -15 到 +15 度范围内搜索最佳倾斜角度 - 38
let mut best_angle = 0.0 - 39
let mut best_variance = 0.0 - 40
let step = 0.5 // 0.5 度步长 - 41
let mut angle = -15.0 - 42
while angle <= 15.0 { - 43●
let variance = projection_variance_at_angle(data, w, h, angle) - 44
if variance > best_variance { - 45●
best_variance = variance - 46
best_angle = angle - 47
} - 48
angle += step - 49
} - 50
best_angle - 51
} - 52
- 53
///| - 54
/// 计算指定角度下的水平投影方差 - 55
fn projection_variance_at_angle( - 56
data : Bytes, - 57
width : Int32, - 58
height : Int32, - 59
angle_deg : Double, - 60
) -> Double { - 61●
let angle_rad = angle_deg * 3.141592653589793 / 180.0 - 62●
let cos_a = @math.cos(angle_rad) - 63●
let sin_a = @math.sin(angle_rad) - 64
// 计算旋转后的图像尺寸 - 65●
let new_h = (height.to_double() * cos_a.abs() + - 66●
width.to_double() * sin_a.abs()).to_int() + - 67
1 - 68
// 计算水平投影 - 69●
let projection : Array[Int32] = Array::make(new_h, 0) - 70
for y in 0..<height { - 71●
for x in 0..<width { - 72●
if data[y * width + x].to_int() < 128 { - 73
// 黑色像素(文字) - 74●
let new_y = ((x.to_double() - width.to_double() / 2.0) * sin_a + - 75●
(y.to_double() - height.to_double() / 2.0) * cos_a + - 76●
new_h.to_double() / 2.0).to_int() - 77
if new_y >= 0 && new_y < new_h { - 78●
projection[new_y] += 1 - 79
} - 80
} - 81
} - 82
} - 83
// 计算投影的方差 - 84
let mut sum = 0 - 85
let mut sum_sq = 0 - 86
let mut count = 0 - 87
for i in 0..<new_h { - 88●
sum += projection[i] - 89
sum_sq += projection[i] * projection[i] - 90
count += 1 - 91
} - 92
if count == 0 { - 93○
return 0.0 - 94
} - 95●
let mean = sum.to_double() / count.to_double() - 96●
let variance = sum_sq.to_double() / count.to_double() - mean * mean - 97
variance - 98
} - 99
- 100
///| - 101
/// 倾斜校正(自动检测倾斜角度并旋转) - 102
pub fn deskew(img : @types.Image) -> @types.Image raise @types.LoadError { - 103
// 输入验证 - 104●
if img.width <= 0 || img.height <= 0 { - 105●
raise @types.LoadError::DecodeFailed("deskew: 图像尺寸必须大于0") - 106
} - 107
if img.channels != 1 { - 108●
raise @types.LoadError::DecodeFailed("deskew: 仅支持单通道灰度图") - 109
} - 110
let expected_size = img.width * img.height - 111●
if img.data.length() != expected_size { - 112●
raise @types.LoadError::DecodeFailed( - 113
"deskew: 数据大小不匹配,期望 " + - 114●
expected_size.to_string() + - 115
",实际 " + - 116●
img.data.length().to_string(), - 117
) - 118
} - 119
- 120●
let angle = detect_skew_angle(img) - 121●
rotate_document(img, -angle) - 122
} - 123
- 124
///| - 125
/// 文档旋转(带白色背景填充) - 126
/// angle: 旋转角度(度),正值表示逆时针 - 127
pub fn rotate_document( - 128
img : @types.Image, - 129
angle : Double, - 130
) -> @types.Image raise @types.LoadError { - 131
// 输入验证 - 132●
if img.width <= 0 || img.height <= 0 { - 133○
raise @types.LoadError::DecodeFailed( - 134
"rotate_document: 图像尺寸必须大于0", - 135
) - 136
} - 137
if img.channels < 1 || img.channels > 4 { - 138○
raise @types.LoadError::DecodeFailed( - 139
"rotate_document: 通道数必须在1-4之间", - 140
) - 141
} - 142
let expected_size = img.width * img.height * img.channels - 143●
if img.data.length() != expected_size { - 144○
raise @types.LoadError::DecodeFailed( - 145
"rotate_document: 数据大小不匹配,期望 " + - 146○
expected_size.to_string() + - 147
",实际 " + - 148○
img.data.length().to_string(), - 149
) - 150
} - 151
- 152
let w = img.width - 153
let h = img.height - 154
let ch = img.channels - 155
let angle_rad = angle * 3.141592653589793 / 180.0 - 156●
let cos_a = @math.cos(angle_rad) - 157●
let sin_a = @math.sin(angle_rad) - 158
// 计算新图像尺寸 - 159●
let new_w = (w.to_double() * cos_a.abs() + h.to_double() * sin_a.abs()).to_int() + - 160
1 - 161●
let new_h = (w.to_double() * sin_a.abs() + h.to_double() * cos_a.abs()).to_int() + - 162
1 - 163
// 创建白色背景的新图像 - 164●
let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\xFF') - 165
let data = img.data - 166
// 反向映射:对新图像的每个像素,找到原图像对应的像素 - 167
for y in 0..<new_h { - 168●
for x in 0..<new_w { - 169
// 相对于中心的坐标 - 170●
let cx = x.to_double() - new_w.to_double() / 2.0 - 171●
let cy = y.to_double() - new_h.to_double() / 2.0 - 172
// 反向旋转 - 173●
let src_x = cx * cos_a + cy * sin_a + w.to_double() / 2.0 - 174●
let src_y = -cx * sin_a + cy * cos_a + h.to_double() / 2.0 - 175
// 双线性插值 - 176●
let x0 = src_x.to_int() - 177●
let y0 = src_y.to_int() - 178
let x1 = x0 + 1 - 179
let y1 = y0 + 1 - 180●
let fx = src_x - x0.to_double() - 181●
let fy = src_y - y0.to_double() - 182
if x0 >= 0 && x1 < w && y0 >= 0 && y1 < h { - 183●
for c in 0..<ch { - 184●
let v00 = data[(y0 * w + x0) * ch + c].to_int().to_double() - 185●
let v01 = data[(y0 * w + x1) * ch + c].to_int().to_double() - 186●
let v10 = data[(y1 * w + x0) * ch + c].to_int().to_double() - 187●
let v11 = data[(y1 * w + x1) * ch + c].to_int().to_double() - 188
// 双线性插值 - 189
let v0 = v00 * (1.0 - fx) + v01 * fx - 190
let v1 = v10 * (1.0 - fx) + v11 * fx - 191
let v = v0 * (1.0 - fy) + v1 * fy - 192●
out[(y * new_w + x) * ch + c] = @types.clamp_byte(v.to_int()) - 193
} - 194
} - 195
} - 196
} - 197
@types.Image::{ - 198
width: new_w, - 199
height: new_h, - 200
channels: ch, - 201●
data: Bytes::from_array(out), - 202
} - 203
}