十種檢查去背邊緣的方法:以七種已知失敗案例評分
Ten ways to check a cutout edge, scored against seven known failures
作者把十種去背邊緣檢查套用於同一張圖片、七種已知失敗案例及固定閾值,逐項比較判斷結果。色度殘差檢查在白色及綠色產品上均達 6/6,表現最佳;邊緣色相檢查則由白色產品的 6/6 降至綠色產品的 2/6。
Four posts so far, each ending with a paragraph explaining what the metric in it cannot see: edge geometry, colour contamination, the edge-hue test and its blind spot. That's an unsatisfying way to leave things — a reader has ten checks now and no way to compare them. So this post does the obvious thing: put every one of them on the same image, the same seven failures, and a fixed threshold, then score the verdicts.
One check gets every verdict right, on both product colours. It is not the one I recommended in the previous post.
The setup
Nothing new here, which is the point — the whole value is that the input never changes.
A white ceramic mug, 1200×800, Unsplash-licensed. An Apple Vision matte (VNGenerateForegroundInstanceMaskRequest), run locally. One clean foreground F extrapolated from the eroded opaque core, one known source background G at RGB (46, 204, 113). 9,959 partial-band pixels, 11,961 inner-ring pixels, a 4.05 px wide alpha ramp.
Seven variants share that matte and differ only in the RGB stored under it. Two materials: the mug as it is (body hue 112.1°, C* 2.17), and the same mug recoloured to the background's hue (150.6°, C* 23.12) to reproduce the failure case from the last post.
| Variant | What it simulates | |
|---|---|---|
| A | clean | correct straight RGB, no spill |
| B | raw blend | edge is alpha·F + (1−alpha)·G, true background fraction 0.498 |
| C | half despill | half the green removed |
| D | over-despill | pushed past neutral, toward magenta |
| E | alpha grown | clean RGB, alpha dilated 2 px |
| F | thresholded | alpha binarized at 128, spilled RGB left on opaque pixels |
A gate for this job has two duties, and both matter: fire on B, C, D, F, and stay silent on A and E. A check that fires on everything scores as badly as one that fires on nothing — the second is how bad cutouts ship, the first is how a gate gets switched off after a week.
Ten checks, thresholded at the values the earlier posts used. No threshold here was tuned to make the table look better; I set them from the published numbers before running the sweep.
The matrix
Filled dot means the check fired. Green cell means that was the right call.
The same thing as numbers, per material — first white ceramic:
| Check | Needs | Thr | A | B | C | D | E | F | Correct |
|---|---|---|---|---|---|---|---|---|---|
| band width | nothing | <1.0 | · | · | · | · | F | F | 2/6 |
| contour roughness | nothing | 10% | · | · | · | · | · | · | 2/6 |
| hole area | nothing | 5% | · | · | · | · | F | · | 1/6 |
| spill fraction | G + F | 0.02 | · | F | F | F | · | · | 5/6 |
| ECD | F | 5.0 | · | F | F | F | · | · | 5/6 |
| inner-ring spill | G + F | 0.02 | · | · | · | · | · | F | 3/6 |
| edge hue | nothing | 10° | · | F | F | F | · | F | 6/6 |
| chroma residual | nothing | 2.0 | · | F | F | F | · | F | 6/6 |
| chroma ceiling | nothing | 2% | · | F | F | F | F | F | 5/6 |
| local ceiling | nothing | 2% | F | F | F | F | · | · | 4/6 |
…and green ceramic:
| Check | Needs | A | B | C | D | E | F | Correct |
|---|---|---|---|---|---|---|---|---|
| band width | nothing | · | · | · | · | F | F | 2/6 |
| contour roughness | nothing | · | · | · | · | · | · | 2/6 |
| hole area | nothing | · | · | · | · | F | · | 1/6 |
| spill fraction | G + F | · | F | F | F | · | · | 5/6 |
| ECD | F | · | F | F | F | · | · | 5/6 |
| inner-ring spill | G + F | · | · | · | · | · | F | 3/6 |
| edge hue | nothing | · | · | · | · | · | · | 2/6 |
| chroma residual | nothing | · | F | F | F | · | F | 6/6 |
| chroma ceiling | nothing | · | F | · | · | F | F | 3/6 |
| local ceiling | nothing | · | F | F | · | · | · | 4/6 |
What the table says, row by row
The three geometry checks are a different gate entirely. Band width collapses to zero on E and F — correct, both removed the ramp. Hole area rises 6% on E because dilating the alpha closes part of the mug's handle gap. Roughness, measured as perimeter over the perimeter of an equal-area circle, moves 1% on E and nothing anywhere else; it needs a much worse failure than these to fire, and I'm leaving it in the table to show that rather than quietly dropping it.
None of the three sees a single colour failure. That's not a criticism — the first post said so explicitly. It's the reason a colour gate and a geometry gate have to run together.
Spill fraction and ECD score 5/6 for the same reason. Both fire on B, C, D and miss F, because F has no partial-alpha band to measure. ECD is the stronger of the two on this input (95.955 / 47.978 / 47.978 — it reads D's over-correction as loudly as C's under-correction, which spill fraction cannot), and spill fraction is the only one that reports an amount: 0.498, matching mean(1−alpha) exactly. Neither distinguishes "wrong" from "wrong the other way" on its own, and both need something a real cutout doesn't ship with.
Inner-ring spill catches exactly one thing. 0.085 on F, 0.000–0.002 everywhere else. It's 3/6 because it's silent on B, C, D — it's not a colour gate, it's the second half of a pair with spill fraction. Spill plus inner-ring spill covers five of six cases and misses E; add band width and you have 6/6, at the cost of G and F.
The edge-hue row is the honest embarrassment. 6/6 on white, 2/6 on green. Same code, same thresholds, same everything except the product's own colour. It was the recommendation of the previous post and it's blind on any product whose hue matches the background it was cut from.
The two chroma gates are one-sided. The global ceiling fires on E on white (5.9% of its edge pixels above the body's C* p99) — a false positive, and on green it misses C and D entirely. The local ceiling fires on A on white (19.5%), because a white product's body chroma is ~2 and the ceiling is too small to mean anything. Both can only see contamination that raises chroma, and both are noisier the paler or the more saturated the product.
The row that wins
Chroma residual: composite the stored pixels onto the shipping background, predict what the chroma should be from the body's mean colour and the alpha, and subtract.
body_mean = rgb2lab(composite(F))[alpha >= 250].mean(axis=0)
white = rgb2lab([[255, 255, 255]]) / 255.0
a = alpha[mask] / 255.0
expected = a[:, None]*body_mean + (1 - a[:, None])*white
C_expected = np.hypot(expected[:, 1], expected[:, 2])
residual = chroma_actual[mask] - C_expected
Six of six on both materials, and it needs nothing but the PNG:
| Variant | white | green |
|---|---|---|
| A clean | +0.43 | +0.05 |
| B raw blend | +10.30 | +7.30 |
| C half despill | +5.22 | +3.67 |
| D over-despill | +2.07 | −3.71 |
| E alpha grown | +1.05 | +0.51 |
| F thresholded | +6.95 | +4.79 |
The sign is the diagnosis: positive is background still in the edge, negative is the edge pushed below what the product can explain. That's what the hue test gave on white, and it survives on green, which the hue test does not.
Two things I'm not going to pretend about. First, on white, D sits at +2.07 against a threshold of 2.0 — it fires, but only just, and a slightly different body-mean estimate would miss it. The green case is the one that shows the signal clearly (−3.71). Second, it's the only row that's green across both materials on these fourteen constructed cases, which is not the same as being the best check in production. Fourteen cases, two product colours, one matte. That's a hypothesis with a good score, not a validated gate.
A correction to the last post
D's hue shift is −105.3° here; the previous post reported −114.2° for the same variant. The difference is clipping: D = F + 0.5·(F − S) pushes some edge pixels above 255, and the earlier code fed those unclipped values to rgb2lab. A shipped 8-bit PNG can't hold them, so this table clips first. −105.3° is the number to use; the verdict doesn't change (both are far outside ±10°, both mean over-despill), but the number in the last post is wrong and this is the correction.
Which one to run
The practical answer is decided by what you have, not by which check is cleverest:
-
You have the source background
G(green screen, controlled sweep): spill fraction for the amount, inner-ring spill for the thresholded case. Add band width for the geometry. Three checks, 6/6 on both materials, and spill fraction is the only one that gives you a number you can compare against ground truth. -
You have a clean foreground
F: ECD on the band. Reads either direction of colour error and reports in 8-bit levels, which is easy to threshold and easy to explain to whoever triages the failures. - You have only the shipped PNG: chroma residual, and be aware the baseline is wide. On this input the clean variant's individual edge pixels straddle ±3 with 9.8% of them outside on white and 27.2% on green, so a single cutout scoring +1.5 means little; a batch distribution shifting is the signal.
- You have only the PNG and the product is white or grey: run both the hue test and the chroma residual. They agree on the obvious cases and disagree on the marginal ones, and the disagreement is informative — in this table, D is where they part.
The one combination I'd avoid is trusting the edge-hue test alone on an unknown product colour. It's the highest-scoring check on white and the lowest on green, and nothing in the file tells you which case you're in.
What this table is not
Nine of these ten checks are mine, applied to variants I constructed from a matte I generated. The failures are synthetic degradations, not outputs from production tools, and the ground truth is a nearest-neighbour extrapolation of the mug's own core. The scores measure internal consistency, not agreement with a human eye.
The piece I'd want before trusting any of this in a pipeline is a batch of real cutouts with real defects, labelled by someone who can say which ones are unusable. That would turn "6/6 on fourteen constructed cases" into a precision and recall. It also needs a product range much wider than two colours of one mug.
I work on PicViva, where these runs go out over seller photos, and the reason this table exists at all is that a check has to be cheap enough to run on every cutout in a batch — which rules out anything needing a reference the pipeline doesn't have. That's the constraint the whole series is built around, and it's why the winner is the one that needs nothing.
If you've built a gate like this, which failure did you find last — the one that made you distrust a check you'd been relying on?
來源:DEV Community · dev.to
