image.pro\recipe\ecom_white.mbt
74.6% · 100 / 134 points · 369 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # ecom-white:电商白底图 - 3
/// - 4
/// 商品图的第一道工序是"去背景换纯白 + 补成正方形"。这里没有语义分割模型, - 5
/// 走的是**边框估计 + 距离掩膜 + 最大连通域 + 掩膜收缩**这套几何/统计路线: - 6
/// - 7
/// 1. 四条边 5% 环的均值当背景色(主图背景通常是纯色影棚,均值足够稳); - 8
/// 2. 逐像素与背景色的曼哈顿距离超 `tol` 判前景; - 9
/// 3. `connected_components(8)` 取面积最大的块,扔掉反光点/粉尘噪点; - 10
/// 4. 掩膜先闭运算补洞、再 `erode` 一次把 1px 边吃掉——这一步就是"光晕 ≤2px"的实现: - 11
/// 合成时对掩膜做**硬判**(>0 才拷原像素),边界不存在半透明混色; - 12
/// 5. 白底合成 + 居中 pad 成正方形。 - 13
/// - 14
/// 验收用两个可核查的数:`border_white_mean`(外框均值 ≥250)、`halo_pixels` - 15
/// ("非纯白但接近白"的像素数,白底干净时应为 0)。 - 16
- 17
///| - 18
/// 外框 5% 环的逐通道均值,当作背景色。 - 19
fn ew_bg_color(img : @types.Image) -> (Int32, Int32, Int32) { - 20●
let w = img.width - 21
let h = img.height - 22
let ch = img.channels - 23◐
let band_x = if w / 20 > 1 { w / 20 } else { 1 } - 24◐
let band_y = if h / 20 > 1 { h / 20 } else { 1 } - 25
let mut sr = 0 - 26
let mut sg = 0 - 27
let mut sb = 0 - 28
let mut n = 0 - 29
for y in 0..<h { - 30●
let is_edge_row = y < band_y || y >= h - band_y - 31
let mut x = 0 - 32
while x < w { - 33●
let is_edge_col = x < band_x || x >= w - band_x - 34
if is_edge_row || is_edge_col { - 35●
let base = (y * w + x) * ch - 36
if ch == 1 { - 37○
let g = img.data[base].to_int() - 38
sr = sr + g - 39
sg = sg + g - 40
sb = sb + g - 41
} else { - 42●
sr = sr + img.data[base].to_int() - 43●
sg = sg + img.data[base + 1].to_int() - 44●
sb = sb + img.data[base + 2].to_int() - 45
} - 46
n = n + 1 - 47
} - 48
x = x + 2 - 49
} - 50
} - 51
if n == 0 { - 52○
(255, 255, 255) - 53
} else { - 54●
(sr / n, sg / n, sb / n) - 55
} - 56
} - 57
- 58
///| - 59
/// 距离掩膜:与背景色的曼哈顿距离和 > `tol * 3` 记前景(1 通道 0/255)。 - 60
fn ew_mask( - 61
img : @types.Image, - 62
bg : (Int32, Int32, Int32), - 63
tol : Int32, - 64
) -> @types.Image { - 65●
let (br, bgg, bb) = bg - 66
let w = img.width - 67
let h = img.height - 68
let ch = img.channels - 69
let n = w * h - 70●
let out : Array[Byte] = Array::make(n, (0 : Byte)) - 71
let need = tol * 3 - 72
for i in 0..<n { - 73●
let base = i * ch - 74
let (r, g, b) = if ch == 1 { - 75○
( - 76○
img.data[base].to_int(), - 77○
img.data[base].to_int(), - 78○
img.data[base].to_int(), - 79
) - 80
} else { - 81●
( - 82●
img.data[base].to_int(), - 83●
img.data[base + 1].to_int(), - 84●
img.data[base + 2].to_int(), - 85
) - 86
} - 87●
let d = (r - br).abs() + (g - bgg).abs() + (b - bb).abs() - 88
if d > need { - 89●
out[i] = b'\xFF' - 90
} - 91
} - 92●
{ width: w, height: h, channels: 1, data: Bytes::from_array(out), } - 93
} - 94
- 95
///| - 96
/// 只保留面积最大的连通域,其余置 0。返回 (新掩膜, 该块面积)。 - 97
fn ew_largest( - 98
mask : @types.Image, - 99
) -> (@types.Image, Int32) raise @types.LoadError { - 100●
let (lab, comps) = @segment.connected_components(mask, 8) - 101●
if comps.length() == 0 { - 102○
return (mask, 0) - 103
} - 104
let mut best : @segment.ConnectedComponent = comps[0] - 105
for c in comps { - 106●
if c.area > best.area { - 107●
best = c - 108
} - 109
} - 110
let n = mask.width * mask.height - 111●
let out : Array[Byte] = Array::make(n, (0 : Byte)) - 112
for i in 0..<n { - 113●
if lab.labels[i] == best.label { - 114●
out[i] = b'\xFF' - 115
} - 116
} - 117
( - 118
{ - 119
width: mask.width, - 120
height: mask.height, - 121
channels: 1, - 122●
data: Bytes::from_array(out), - 123
}, - 124
best.area, - 125
) - 126
} - 127
- 128
///| - 129
/// 白底合成:掩膜 >0 的像素拷原图,其余涂白。硬判,不做 alpha 混合。 - 130
fn ew_compose(img : @types.Image, mask : @types.Image) -> @types.Image { - 131●
let w = img.width - 132
let h = img.height - 133
let ch = img.channels - 134
let n = w * h - 135●
let out : Array[Byte] = Array::make(n * ch, (255 : Byte)) - 136
for i in 0..<n { - 137●
if mask.data[i].to_int() > 0 { - 138●
let base = i * ch - 139
for c in 0..<ch { - 140●
out[base + c] = img.data[base + c] - 141
} - 142
} - 143
} - 144●
{ width: w, height: h, channels: ch, data: Bytes::from_array(out), } - 145
} - 146
- 147
///| - 148
/// 居中 pad 成正方形(白底)。不缩放,只补边。 - 149
fn ew_pad_square(img : @types.Image) -> @types.Image { - 150◐
let side = if img.width >= img.height { img.width } else { img.height } - 151
if side == img.width && side == img.height { - 152●
return img - 153
} - 154
let ch = img.channels - 155●
let out : Array[Byte] = Array::make(side * side * ch, (255 : Byte)) - 156
let ox = (side - img.width) / 2 - 157
let oy = (side - img.height) / 2 - 158
for y in 0..<img.height { - 159●
for x in 0..<img.width { - 160●
let si = (y * img.width + x) * ch - 161
let di = ((y + oy) * side + (x + ox)) * ch - 162
for c in 0..<ch { - 163●
out[di + c] = img.data[si + c] - 164
} - 165
} - 166
} - 167●
{ width: side, height: side, channels: ch, data: Bytes::from_array(out), } - 168
} - 169
- 170
///| - 171
/// 外框 5% 环的像素均值(白底图应 ≥250)。验收口径:背景干净,不靠目视。 - 172
pub fn border_white_mean(img : @types.Image) -> Double { - 173●
let w = img.width - 174
let h = img.height - 175
let ch = img.channels - 176◐
let band_x = if w / 20 > 1 { w / 20 } else { 1 } - 177◐
let band_y = if h / 20 > 1 { h / 20 } else { 1 } - 178
let mut sum = 0.0 - 179
let mut n = 0 - 180
for y in 0..<h { - 181●
let is_edge_row = y < band_y || y >= h - band_y - 182
let mut x = 0 - 183
while x < w { - 184●
let is_edge_col = x < band_x || x >= w - band_x - 185
if is_edge_row || is_edge_col { - 186●
let base = (y * w + x) * ch - 187
let v = if ch == 1 { - 188○
img.data[base].to_int() - 189
} else { - 190●
let r = img.data[base].to_int() - 191●
let g = img.data[base + 1].to_int() - 192●
let b = img.data[base + 2].to_int() - 193
(r * 77 + g * 150 + b * 29) / 256 - 194
} - 195●
sum = sum + v.to_double() - 196
n = n + 1 - 197
} - 198
x = x + 2 - 199
} - 200
} - 201
if n == 0 { - 202○
return 255.0 - 203
} - 204●
sum / n.to_double() - 205
} - 206
- 207
///| - 208
/// 「光晕像素」:不是纯白、但离白很近(三通道和距白 < `tol * 3`)的像素数。 - 209
/// 白底硬判合成后该值应为 0;非 0 说明掩膜把背景混色带进了商品边缘。 - 210
pub fn halo_pixels(img : @types.Image, tol : Int32) -> Int32 { - 211●
let w = img.width - 212
let h = img.height - 213
let ch = img.channels - 214
let n = w * h - 215
let near = tol * 3 - 216
let mut count = 0 - 217
for i in 0..<n { - 218●
let base = i * ch - 219
let (r, g, b) = if ch == 1 { - 220○
( - 221○
img.data[base].to_int(), - 222○
img.data[base].to_int(), - 223○
img.data[base].to_int(), - 224
) - 225
} else { - 226●
( - 227●
img.data[base].to_int(), - 228●
img.data[base + 1].to_int(), - 229●
img.data[base + 2].to_int(), - 230
) - 231
} - 232●
let d = (255 - r).abs() + (255 - g).abs() + (255 - b).abs() - 233
if d > 0 && d < near { - 234○
count = count + 1 - 235
} - 236
} - 237
count - 238
} - 239
- 240
///| - 241
/// ecom-white Recipe:读 → 边框估背景 → 距离掩膜 → 最大连通域 → 闭运算 + 收缩 → 白底合成 → 方形补齐 → 编码。 - 242
/// - 243
/// `tol` 由 `block` 旋钮控制(默认 36):越大越"宽进",把接近背景的浅色商品也当前景。 - 244
/// 掩膜为空(整图就是背景色)时不硬抠,原图直接方形补齐,`steps` 记 `no-foreground`。 - 245
fn recipe_ecom_white( - 246
data : Bytes, - 247
opts : RecipeOpts, - 248
) -> RecipeResult raise @types.ImageError { - 249●
let steps : Array[StepRec] = [] - 250●
let img = step_decode(data, steps) - 251◐
let tol = if opts.block > 0 { opts.block } else { 36 } - 252●
let bg = ew_bg_color(img) - 253
let (br, bgg, bb) = bg - 254●
steps.push({ - 255
name: "bg-estimate", - 256
note: "rgb=\{br},\{bgg},\{bb}", - 257
width: img.width, - 258
height: img.height, - 259
bytes: -1, - 260●
ms: seg_ms(), - 261
}) - 262●
let raw = ew_mask(img, bg, tol) - 263●
steps.push({ - 264
name: "mask", - 265
note: "tol=\{tol}", - 266
width: img.width, - 267
height: img.height, - 268
bytes: -1, - 269●
ms: seg_ms(), - 270
}) - 271●
let closed = @segment.morph_close(raw) - 272●
let (big, area) = ew_largest(closed) catch { - 273○
e => raise @types.image_error_from_load(e) - 274
} - 275●
steps.push({ - 276
name: "largest", - 277
note: "area=\{area}", - 278
width: img.width, - 279
height: img.height, - 280
bytes: -1, - 281●
ms: seg_ms(), - 282
}) - 283
let mut out = if area > 0 { - 284
// 收缩 1px:掩膜边界那圈混着背景色,硬拷会把灰边带进白底 - 285●
let shrunk = @segment.erode(big) - 286●
steps.push({ - 287
name: "shrink", - 288
note: "erode 1", - 289
width: img.width, - 290
height: img.height, - 291
bytes: -1, - 292●
ms: seg_ms(), - 293
}) - 294●
ew_compose(img, shrunk) - 295
} else { - 296○
steps.push({ - 297
name: "compose", - 298
note: "no-foreground", - 299
width: img.width, - 300
height: img.height, - 301
bytes: -1, - 302○
ms: seg_ms(), - 303
}) - 304
img - 305
} - 306
if area > 0 { - 307●
steps.push({ - 308
name: "compose", - 309
note: "white-bg", - 310
width: out.width, - 311
height: out.height, - 312
bytes: -1, - 313●
ms: seg_ms(), - 314
}) - 315
} - 316●
let squared = ew_pad_square(out) - 317
if squared.width != out.width || squared.height != out.height { - 318●
steps.push({ - 319
name: "pad-square", - 320
note: "side=\{squared.width}", - 321
width: squared.width, - 322
height: squared.height, - 323
bytes: -1, - 324●
ms: seg_ms(), - 325
}) - 326
out = squared - 327
} - 328●
let (tw, th) = fit_longest(out, opts.longest) - 329
let mut cur = out - 330
if tw != cur.width || th != cur.height { - 331○
cur = @transform.resize_to_contain( - 332
cur, - 333
tw, - 334
th, - 335○
Array::make(cur.channels, (255 : Byte)), - 336
) catch { - 337○
e => raise @types.image_error_from_load(e) - 338
} - 339○
steps.push({ - 340
name: "resize", - 341
note: "", - 342
width: cur.width, - 343
height: cur.height, - 344
bytes: -1, - 345○
ms: seg_ms(), - 346
}) - 347
} - 348●
let fmt0 = pick_format(cur, opts.format) - 349●
let (bytes, q0) = encode_fit(cur, fmt0, opts.quality, opts.max_bytes) - 350●
steps.push({ - 351
name: "encode", - 352
note: "quality=\{q0}", - 353
width: cur.width, - 354
height: cur.height, - 355●
bytes: bytes.length().to_int(), - 356●
ms: seg_ms(), - 357
}) - 358
{ - 359
recipe: "ecom-white", - 360
image: cur, - 361
bytes, - 362
format: fmt0, - 363
steps, - 364
quality: q0, - 365
redacted: 0, - 366
blurred: false, - 367
codes: [], - 368
} - 369
}