Task 6 of 6

What It Does Badly, and the Fix Everybody Ships

Drag the last task's scrubber slowly and watch the face. The eyes creep together, the head goes oval, and by the end it is a different person: every row of it wide enough to matter loses exactly seven columns, so its widest row comes back thirty columns instead of thirty-seven — and not the same seven in every row (twenty-eight different columns of the face are cut somewhere), so its features stop lining up vertically. That skew is the straight-line artefact seam carving is famous for, showing up here as a warp. Seam carving has no idea what a face is. It knows that skin is smooth and that smooth is cheap, so once the sky is used up the face is the next best bargain in the picture.

The two poles, meanwhile, come through perfectly straight — every seam in this run happened to pass entirely to one side of them. That is luck, not a property, and it is the uncomfortable part: which structures survive depends on where the cheap material happens to be, and you cannot read it off the picture beforehand.

This is why nothing ships it unattended. Every product that offers content-aware resize offers a brush next to it, and the brush paints a mask: a region whose energy gets a large constant added, so every seam routes around it. Twenty is plenty here — the dearest seam this picture has anywhere in the run prices at under nineteen, and the ones actually taken top out at fourteen, so a single protected pixel already prices a seam out of the neighbourhood.

One trap comes free with the idea, and it is the reason the mask is carried in planes with everything else: the mask lives in image space. Carve the picture without carving the mask and the protection slides off the thing it was protecting, one column at a time.

Goal: write maskedEnergy, then measure the result — count the protected pixels that survived, and plot the protected run's costs against the unprotected ones.

Requirements

Hint 1 — the penalty term

The Sobel part is untouched; the mask is one more term on the end:

return Math.sqrt(gx * gx + gy * gy)
     + this.constants.penalty * mask[y][x];

Because the mask is 0 outside the protected region, this costs the rest of the picture exactly nothing.

Hint 2 — counting what survived

planes[4] is the mask after the same 32 carves as the picture, so it is a plain 2D array one column narrower per removal:

let left = 0;
for (let y = 0; y < 72; y++) {
  for (let x = 0; x < planes[4][y].length; x++) left += planes[4][y][x];
}

If that number has dropped, a seam went through the face.

Hint 3 — two series in one chart

plot takes an object of named series and draws them on the same axes:

plot({ 'no mask': unmaskedCosts, 'face protected': costs },
     { title: 'what protection costs', xLabel: 'removal' });

Same idea elsewhere

Weighted energy is how this is done everywhere — Photoshop's content-aware scale takes a protect/remove mask, and the same "add a large constant to the cost field" move drives graph-cut segmentation, path planning around obstacles, and every route planner that has ever been told to avoid motorways. The deeper lesson is the honest one: an algorithm that optimises a proxy will happily destroy whatever the proxy does not measure, and the fix is always to put the missing knowledge into the objective rather than to hope.

All tasks in Seam Carving: Content-Aware Resizing

  1. What Can We Afford to Lose?
  2. The Cheapest Path Down: One Launch per Row
  3. Reading the Seam Back Out
  4. Take It Out, Let the Picture Close Up
  5. Payoff: Thirty-Two Seams
  6. What It Does Badly, and the Fix Everybody Ships

This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.