Magnetic lasso¶
| Fact | Value |
|---|---|
| Status | Implemented and tested |
| Group | Lasso |
| Engine | Browser |
| Code | web/src/core/selection/livewire.ts, livePath |
Overview¶
The magnetic lasso draws an outline that clings to edges in the image. You move the pointer roughly along the edge of an object, and the outline finds the exact edge. It works well on objects with a clear outline against a different background.
How to use it¶
- Choose the magnetic lasso in the tool strip.
- Click on the edge of the object. The first point snaps to the nearest strong edge.
- Move the pointer along the edge. You do not need to hold the button. The outline follows the edge.
- Click to fix a point where the outline should not move any more. Backspace removes the last point.
- Close the outline on the first point, with a double-click or with Enter. Escape cancels.
How it works¶
- Find the largest Sobel gradient of luma in the image, Gmax, once.
- Snap the first click to the strongest edge within the width setting.
- As the pointer moves, snap it to the strongest edge within the width.
- Find the cheapest 8-connected path from the last anchor, where steps on strong edges are cheap.
- Fix an anchor when the path gets longer than the anchor spacing, or when you click.
- On closing, find a path back to the start that does not reuse the path so far.
- Fill the closed path as a polygon and combine it with the selection by the mode.
Mathematical foundation¶
The tool is a live wire, after the intelligent scissors of Mortensen and Barrett. The image is a graph. Each pixel is a node, linked to its 8 neighbors. The outline between two points is the path with the lowest total cost.
PixelNexus uses a simpler local cost than the paper. It keeps only the gradient magnitude term:
where
- \(G(q)\) is the Sobel gradient magnitude of BT.709 luma at pixel \(q\),
- \(G_{\max}\) is the largest \(G\) in the image,
- \(t\) is the contrast setting, from 0 to 1,
- \(\varepsilon = 0.1\) is a small cost for every step,
- \(\lVert q - p \rVert\) is 1 for a side neighbor and \(\sqrt{2}\) for a diagonal one.
This is an exact definition of what the code computes. Steps on strong edges cost little, so the cheapest path runs along them. Where there is no edge, every step costs about its length, so the path is a straight line. The small cost \(\varepsilon\) stops a path from wandering along an edge for nothing.
Dijkstra's algorithm finds the cheapest path. It only searches a box around the last point and the pointer. The box grows on each side by \(w + \max(24, 2w)\) pixels, where \(w\) is the width setting. When the pointer moves far, the tool sets points on the way, so each search stays small.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Width width |
10 | 1 to 40 px | How far from the pointer the tool looks for an edge. |
Contrast contrast |
10 | 1 to 100 % | The weakest edge the tool follows, in percent of the strongest edge in the image. |
Anchor spacing spacing |
40 | 5 to 200 px | The path length after which the tool fixes an anchor point by itself. |
Feather feather |
0 | 0 to 250 px | Softens the edge of the new selection. |
Anti-alias antiAlias |
on | On or off | Gives edge pixels a value for the part of them that the shape covers. |
Implementation¶
The code is in web/src/core/selection/livewire.ts and the gesture in web/src/selection/gestures.ts. The largest gradient of the image is computed once per outline, in one pass over three rows of luma at a time. On a 36 megapixel image this takes about half a second on the first click. The closing path back to the first point may not use the pixels of the outline so far, so it cannot run back along the same edge.
Limitations¶
- Where two edges are close, the outline can jump to the wrong one. Fix a point before that place.
- The cost uses brightness only. Two colors of the same brightness give no edge.
- The paper's cost has more terms than the gradient magnitude, and the paper adds on-the-fly training and boundary cooling. PixelNexus leaves these out.
Tests¶
References¶
- Mortensen, E. N. and Barrett, W. A. Intelligent Scissors for Image Composition. Proceedings of SIGGRAPH 95, 191 to 198, 1995. https://history.siggraph.org/?p=118735