image.pro\recipe\ecom_white.mbt

74.6% · 100 / 134 points · 369 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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