Subject and background¶
Subject selection¶
| Fact | Value |
|---|---|
| Status | Implemented and tested |
| Group | Automatic |
| Engine | Browser |
| Code | web/src/core/selection/subject.ts, subjectMask |
Background selection¶
| Fact | Value |
|---|---|
| Status | Implemented and tested |
| Group | Automatic |
| Engine | Browser |
| Code | web/src/core/selection/subject.ts, subjectMask |
Overview¶
Select > Subject guesses the main object of the image. Select > Background selects everything else. Both work without AI, from color, detail and position. They give a starting point that you fix with the other tools. AI selection is planned for a later phase.
How to use them¶
- Open an image with one clear object.
- Choose Select > Subject or Select > Background. The work runs in the background and takes about a second.
- Fix mistakes with the selection brush, the eraser or Quick Selection.
How it works¶
Subject:
- Make a copy of at most 512 pixels on the longer side.
- Compute frequency-tuned and spectral residual saliency, edge density and a center prior.
- Cluster the colors with k-means and score each cluster by saliency and how little of the border it covers.
- Split the scores with Otsu's threshold and keep the strongest connected parts.
- Fill holes, scale up to the image size and snap the edge to the image.
Background:
- Find the subject as Select > Subject does.
- Invert the result, so the background is selected.
Mathematical foundation¶
Everything below runs on a copy of at most 512 pixels on the longer side, made by averaging.
Frequency-tuned saliency (Achanta et al., 2009) is
where \(I_\mu\) is the mean CIELAB color of the image, \(I_\omega\) the image after a small blur, and the norm the CIELAB distance. PixelNexus blurs with the 5x5 binomial kernel. Pixels whose color differs from the image average stand out.
Spectral residual (Hou and Zhang, 2007) works on a 64x64 gray copy. With \(A\) the log amplitude and \(P\) the phase of its Fourier transform,
where \(h_3 * A\) is the 3x3 mean of \(A\), with the spectrum wrapping around. The parts of the spectrum that are not typical for images mark unusual places. PixelNexus smooths \(S_{\text{sr}}\) with a Gaussian of sigma 2.5 pixels at the 64x64 size.
The rest is PixelNexus's own heuristic:
- Edge density is the Sobel gradient of luma, averaged over a box 1/8 of the longer side wide.
- Center prior is \(\exp\!\big(-(u^2 + v^2) / (2 \cdot 0.5^2)\big)\), with \(u\) and \(v\) from -1 to 1 across the image.
- The saliency is \(0.4\, S_{\text{ft}} + 0.3\, S_{\text{sr}} + 0.3\,\) edge density, scaled to 0 to 1, times \(0.4 + 0.6 \times\) center prior, scaled again.
- k-means with 8 clusters groups the colors. Each cluster scores its mean saliency times \(1 - b\), where \(b\) is its share of a frame 2% wide along the image border. The background usually touches the border all around.
- Each pixel scores the mean of its saliency and its cluster score. Otsu's threshold splits the scores into two classes.
- The connected part with the largest summed score stays. So does every part with at least half that score and at least 0.5% of the image. Holes inside are filled.
- The mask is scaled up with bilinear interpolation and snapped to edges, with a reach of 1.5 times the scale factor, from 2 to 24 pixels.
Background selection is the inverse: each value becomes 255 minus the value.
Implementation¶
The work runs in a Web Worker, so the studio stays responsive. On a 512 x 512 test image it took about one second on the development PC.
Limitations¶
- A busy background, or a subject that is not colorful or detailed, fools it.
- It keeps the main object. Several objects of the same size may be kept or dropped by the score rule in step 6.
- It is a heuristic. Its weights are choices, not results from a paper.
Tests¶
References¶
- Achanta, R., Hemami, S., Estrada, F. and Süsstrunk, S. Frequency-tuned Salient Region Detection. IEEE Conference on Computer Vision and Pattern Recognition, 1597 to 1604, 2009. DOI 10.1109/CVPR.2009.5206596.
- Hou, X. and Zhang, L. Saliency Detection: A Spectral Residual Approach. IEEE Conference on Computer Vision and Pattern Recognition, 2007. DOI 10.1109/CVPR.2007.383267.