Skip to content

Snap to edges

Fact Value
Also called Snap to edges
Status Implemented and tested
Group Lasso
Engine Browser
Code web/src/core/selection/snap.ts, snapToEdges

Overview

Select > Modify > Snap to edges moves the edge of the selection onto the nearest clear edge in the image. Use it after a rough selection: a marquee a little too large, a quick lasso, or Quick Selection on a large image. This is the edge-assisted selection.

How to use it

  1. Make a selection close to the object, within a few pixels of its outline.
  2. Choose Select > Modify > Snap to edges.
  3. Set how far the edge may move (Reach) and press OK.

Where there is no clear edge within reach, the selection stays where it was.

How it works

  1. Find the band of pixels within the reach of the selection edge, with the exact distance transform.
  2. Rank each band pixel: edges at least as strong as the contrast setting rank by strength, above all others.
  3. Rank weaker pixels higher the nearer they are to the old edge.
  4. Flood the band from both sides in rank order. The floods meet on the strongest edge.
  5. Smooth the result by majority, then soften the edge by one pixel.

Mathematical foundation

The band is the set of pixels within the reach \(r\) of the selection edge. The exact Euclidean distance transform gives each pixel its distance \(\delta\) to the other side of the edge (Felzenszwalb and Huttenlocher, 2012). A pixel is in the band when \(\delta \le r\).

The pixels next to the band keep their state and flood into it from both sides. This is a seeded watershed by flooding: a priority queue always grows the flood with the lowest-ranked pixel next. Each band pixel \(q\) gets the rank

\[ \text{rank}(q) = \begin{cases} \max\!\big(L_t,\ \operatorname{round}(4095\, s(q))\big) & \text{if } s(q) \ge t \\ \left\lfloor (L_t - 1)\left(1 - \operatorname{clamp}\!\left(\dfrac{\delta(q) - 1}{r}\right)\right) \right\rfloor & \text{otherwise} \end{cases} \]

where

  • \(s(q)\) is the edge strength: the Sobel gradient of luma divided by its largest value in the image,
  • \(t\) is the contrast setting, from 0 to 1,
  • \(L_t = \lceil 4095\, t \rceil\) is the first rank that is an edge,
  • \(\delta(q)\) is the distance of \(q\) to the old selection edge.

This is an exact definition of what the code computes. Edges strong enough rank above every other pixel, so the two floods meet on the strongest edge in the band. Other pixels rank higher the nearer they are to the old edge, so where there is no edge the floods meet on the old edge. A sharp corner can round by one pixel there, because both floods reach the corner pixel at the same rank.

After the flood, an optional majority smoothing rounds the result, and a 3x3 mean softens the edge by one pixel.

Parameters

Parameter Default Values Meaning
Reach radius 8 1 to 64 px How far the selection edge may move to meet an image edge.
Contrast contrast 10 1 to 100 % The weakest edge that counts, in percent of the strongest edge in the image.
Smoothing smoothing 1 0 to 20 px Radius of the majority smoothing after snapping.

Limitations

  • A large reach can jump to the edge of a different object.
  • Edge strength uses brightness only. Two colors of the same brightness give no edge.
  • The result is a hard selection with a one-pixel soft edge. Feather it for a softer edge.

Tests

References

  • Felzenszwalb, P. F. and Huttenlocher, D. P. Distance Transforms of Sampled Functions. Theory of Computing 8(19), 415 to 428, 2012. DOI 10.4086/toc.2012.v008a019.