image.process\feature\integral_image.mbt
92.2% · 59 / 64 points · 273 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 积分图像 integral image - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 积分图像(纯 MoonBit) - 7
- 8
///| - 9
/// 积分图像(Satoh 表达)。integral[y][x] = sum of img[0..y, 0..x]。 - 10
/// 支持 64 位整数和 Float 两种表达。 - 11
pub struct IntegralImage { - 12
width : Int32 // 原图宽度 - 13
height : Int32 // 原图高度 - 14
data : Array[Int64] // (width+1) * (height+1) 的积分表,首行首列为 0 - 15
} - 16
- 17
///| - 18
/// 计算积分图像。仅处理灰度图(channels=1)或 RGB 图(channels=3/4,取首通道)。 - 19
pub fn integral_image( - 20
img : @types.Image, - 21
) -> IntegralImage raise @types.LoadError { - 22
// 输入验证 - 23●
if img.width <= 0 || img.height <= 0 { - 24●
raise @types.LoadError::DecodeFailed( - 25
"integral_image: 图像尺寸必须大于0", - 26
) - 27
} - 28
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 29●
raise @types.LoadError::DecodeFailed( - 30
"integral_image: 仅支持 channels=1/3/4", - 31
) - 32
} - 33
let expected_size = img.width * img.height * img.channels - 34●
if img.data.length() != expected_size { - 35●
raise @types.LoadError::DecodeFailed( - 36
"integral_image: 数据大小不匹配,期望 " + - 37●
expected_size.to_string() + - 38
",实际 " + - 39●
img.data.length().to_string(), - 40
) - 41
} - 42
let w = img.width - 43
let h = img.height - 44
let ch = img.channels - 45
let iw = w + 1 - 46
let ih = h + 1 - 47●
let data_int : Array[Int32] = Array::make(w * h * ch, 0) - 48
for i in 0..<(w * h * ch) { - 49●
data_int[i] = img.data[i].to_int() - 50
} - 51●
let data : Array[Int64] = Array::make(iw * ih, 0L) - 52
for y in 0..<h { - 53●
let mut row_sum = 0L - 54
for x in 0..<w { - 55●
row_sum += data_int[(y * w + x) * ch].to_int64() - 56
data[(y + 1) * iw + x + 1] = data[y * iw + x + 1] + row_sum - 57
} - 58
} - 59
IntegralImage::{ width: w, height: h, data, } - 60
} - 61
- 62
///| - 63
/// 查询积分图像中矩形区域 [x, x+w) × [y, y+h) 的像素和。 - 64
pub fn integral_sum( - 65
integral : IntegralImage, - 66
x : Int32, - 67
y : Int32, - 68
w : Int32, - 69
h : Int32, - 70
) -> Int64 raise @types.LoadError { - 71
// 参数验证 - 72●
if integral.width <= 0 || integral.height <= 0 { - 73○
raise @types.LoadError::DecodeFailed( - 74
"integral_sum: 积分图像尺寸必须大于0", - 75
) - 76
} - 77
if x < 0 || y < 0 { - 78●
raise @types.LoadError::DecodeFailed("integral_sum: x和y必须 >= 0") - 79
} - 80
if w <= 0 || h <= 0 { - 81●
raise @types.LoadError::DecodeFailed("integral_sum: w和h必须大于0") - 82
} - 83
if x + w > integral.width || y + h > integral.height { - 84●
raise @types.LoadError::DecodeFailed( - 85
"integral_sum: 矩形区域超出图像边界", - 86
) - 87
} - 88
- 89
let iw = integral.width + 1 - 90
let x2 = x + w - 91
let y2 = y + h - 92
let p00 = integral.data[y * iw + x] - 93
let p10 = integral.data[y * iw + x2] - 94
let p01 = integral.data[y2 * iw + x] - 95
let p11 = integral.data[y2 * iw + x2] - 96
p11 - p10 - p01 + p00 - 97
} - 98
- 99
///| - 100
/// 使用积分图像快速计算矩形区域均值。 - 101
pub fn integral_mean( - 102
integral : IntegralImage, - 103
x : Int32, - 104
y : Int32, - 105
w : Int32, - 106
h : Int32, - 107
) -> Float raise @types.LoadError { - 108
// 参数验证 - 109●
if integral.width <= 0 || integral.height <= 0 { - 110○
raise @types.LoadError::DecodeFailed( - 111
"integral_mean: 积分图像尺寸必须大于0", - 112
) - 113
} - 114
if x < 0 || y < 0 { - 115●
raise @types.LoadError::DecodeFailed("integral_mean: x和y必须 >= 0") - 116
} - 117
if w < 0 || h < 0 { - 118●
raise @types.LoadError::DecodeFailed("integral_mean: w和h必须 >= 0") - 119
} - 120
if w == 0 || h == 0 { - 121●
return 0.0F - 122
} - 123
if x + w > integral.width || y + h > integral.height { - 124●
raise @types.LoadError::DecodeFailed( - 125
"integral_mean: 矩形区域超出图像边界", - 126
) - 127
} - 128
- 129●
let sum = integral_sum(integral, x, y, w, h) - 130●
Float::from_int(sum.to_int()) / Float::from_int(w * h) - 131
} - 132
- 133
///| - 134
/// 平方积分图像(用于快速计算方差)。 - 135
pub struct IntegralImageSq { - 136
width : Int32 - 137
height : Int32 - 138
data : Array[Int64] // sum of img^2 - 139
} - 140
- 141
///| - 142
/// 计算平方积分图像。 - 143
pub fn integral_image_sq( - 144
img : @types.Image, - 145
) -> IntegralImageSq raise @types.LoadError { - 146
// 输入验证 - 147●
if img.width <= 0 || img.height <= 0 { - 148●
raise @types.LoadError::DecodeFailed( - 149
"integral_image_sq: 图像尺寸必须大于0", - 150
) - 151
} - 152
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 153●
raise @types.LoadError::DecodeFailed( - 154
"integral_image_sq: 仅支持 channels=1/3/4", - 155
) - 156
} - 157
let expected_size = img.width * img.height * img.channels - 158●
if img.data.length() != expected_size { - 159●
raise @types.LoadError::DecodeFailed( - 160
"integral_image_sq: 数据大小不匹配,期望 " + - 161●
expected_size.to_string() + - 162
",实际 " + - 163●
img.data.length().to_string(), - 164
) - 165
} - 166
let w = img.width - 167
let h = img.height - 168
let ch = img.channels - 169
let iw = w + 1 - 170
let ih = h + 1 - 171●
let data_int : Array[Int32] = Array::make(w * h * ch, 0) - 172
for i in 0..<(w * h * ch) { - 173●
data_int[i] = img.data[i].to_int() - 174
} - 175●
let data : Array[Int64] = Array::make(iw * ih, 0L) - 176
for y in 0..<h { - 177●
let mut row_sum = 0L - 178
for x in 0..<w { - 179●
let v = data_int[(y * w + x) * ch].to_int64() - 180
row_sum += v * v - 181
data[(y + 1) * iw + x + 1] = data[y * iw + x + 1] + row_sum - 182
} - 183
} - 184
IntegralImageSq::{ width: w, height: h, data, } - 185
} - 186
- 187
///| - 188
/// 使用积分图像计算矩形区域方差。 - 189
/// 需要同时提供积分图像和平方积分图像。 - 190
pub fn integral_variance( - 191
integral : IntegralImage, - 192
integral_sq : IntegralImageSq, - 193
x : Int32, - 194
y : Int32, - 195
w : Int32, - 196
h : Int32, - 197
) -> Float raise @types.LoadError { - 198
// 参数验证 - 199●
if integral.width <= 0 || integral.height <= 0 { - 200○
raise @types.LoadError::DecodeFailed( - 201
"integral_variance: 积分图像尺寸必须大于0", - 202
) - 203
} - 204
if integral_sq.width <= 0 || integral_sq.height <= 0 { - 205○
raise @types.LoadError::DecodeFailed( - 206
"integral_variance: 平方积分图像尺寸必须大于0", - 207
) - 208
} - 209
if integral.width != integral_sq.width || - 210
integral.height != integral_sq.height { - 211●
raise @types.LoadError::DecodeFailed( - 212
"integral_variance: 积分图像和平方积分图像尺寸必须相同", - 213
) - 214
} - 215
if x < 0 || y < 0 { - 216●
raise @types.LoadError::DecodeFailed("integral_variance: x和y必须 >= 0") - 217
} - 218
if w <= 0 || h <= 0 { - 219●
raise @types.LoadError::DecodeFailed( - 220
"integral_variance: w和h必须大于0", - 221
) - 222
} - 223
if x + w > integral.width || y + h > integral.height { - 224●
raise @types.LoadError::DecodeFailed( - 225
"integral_variance: 矩形区域超出图像边界", - 226
) - 227
} - 228
- 229●
let n = Float::from_int(w * h) - 230●
let sum = Float::from_int(integral_sum(integral, x, y, w, h).to_int()) - 231●
let sum_sq = Float::from_int( - 232●
integral_sum_sq(integral_sq, x, y, w, h).to_int(), - 233
) - 234
let mean = sum / n - 235
sum_sq / n - mean * mean - 236
} - 237
- 238
///| - 239
/// 查询平方积分图像中矩形区域的像素平方和。 - 240
pub fn integral_sum_sq( - 241
integral_sq : IntegralImageSq, - 242
x : Int32, - 243
y : Int32, - 244
w : Int32, - 245
h : Int32, - 246
) -> Int64 raise @types.LoadError { - 247
// 参数验证 - 248●
if integral_sq.width <= 0 || integral_sq.height <= 0 { - 249○
raise @types.LoadError::DecodeFailed( - 250
"integral_sum_sq: 平方积分图像尺寸必须大于0", - 251
) - 252
} - 253
if x < 0 || y < 0 { - 254●
raise @types.LoadError::DecodeFailed("integral_sum_sq: x和y必须 >= 0") - 255
} - 256
if w <= 0 || h <= 0 { - 257●
raise @types.LoadError::DecodeFailed("integral_sum_sq: w和h必须大于0") - 258
} - 259
if x + w > integral_sq.width || y + h > integral_sq.height { - 260●
raise @types.LoadError::DecodeFailed( - 261
"integral_sum_sq: 矩形区域超出图像边界", - 262
) - 263
} - 264
- 265
let iw = integral_sq.width + 1 - 266
let x2 = x + w - 267
let y2 = y + h - 268
let p00 = integral_sq.data[y * iw + x] - 269
let p10 = integral_sq.data[y * iw + x2] - 270
let p01 = integral_sq.data[y2 * iw + x] - 271
let p11 = integral_sq.data[y2 * iw + x2] - 272
p11 - p10 - p01 + p00 - 273
}