Search only the max-fitting crop window, including edge positions

Edge energy is non-negative, so the 85% and 70% candidate windows could
never outscore the full-size window containing them; drop them. Also
always evaluate the last x/y position so a subject touching the right
or bottom edge isn't cut by up to one step of grid misalignment.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
zyberzeroandClaude Opus 5.5 committed 2026-11-03 19:03:53 +01:00
1 parent ac9a0b43a8
commit 36782f15a1
2 files changed
+31 -26

No files matched your search

+1 -1
View File
@@ -182,7 +182,7 @@ Algorithm (`SaliencySmartCropper`):
2. Compute the **max-fitting centered rect** at the target aspect. This is the upper bound on crop size and the fallback if any later step throws.
3. Downscale a clone of the source to **256 px on the long edge** (cheap analysis canvas).
4. Compute per-pixel **Sobel magnitude** on Rec. 601 luma, build a **summed-area table** for O(1) rectangle sums.
5. Slide a target-aspect window over the downscaled image at three sizes (100%, 85%, 70% of the max-fitting size) with an 8 px step, pick the position with maximum total edge energy.
5. Slide the max-fitting target-aspect window over the downscaled image with an 8 px step (always including the last position on each axis, so edge-hugging subjects aren't missed), pick the position with maximum total edge energy. Only one window size is searched: edge energy is non-negative, so a smaller window can never outscore a larger one containing it.
6. Scale the winning rect back to source coordinates, clamp to image bounds, return.
Failure mode: the whole `ComputeCropRect` is wrapped in `try/catch`; on any exception we log a warning and return the centered max-fitting rect. A bad photo must never 500 the upload — same doctrine as MQTT failure being non-fatal.
@@ -16,7 +16,6 @@ public sealed class SaliencySmartCropper : ISmartCropper
private const double AspectMatchTolerance = 0.005;
private const int SmallLongEdge = 256;
private const int SlideStep = 8;
private static readonly double[] CandidateScales = { 1.0, 0.85, 0.7 };
private readonly ILogger<SaliencySmartCropper> _logger;
@@ -66,40 +65,46 @@ public sealed class SaliencySmartCropper : ISmartCropper
var mag = ComputeSobelMagnitude(small);
var sat = BuildSat(mag, smallW, smallH);
// Only the max-fitting window size is searched: edge energy is
// non-negative, so a smaller window never outscores a larger one
// containing it.
var smallMaxRect = MaxFittingCenteredRect(smallW, smallH, tgtAspect);
var winW = smallMaxRect.Width;
var winH = smallMaxRect.Height;
if (winW < 2 || winH < 2)
{
return fallback;
}
var maxX = smallW - winW;
var maxY = smallH - winH;
var stride = smallW + 1;
var bestSmallRect = smallMaxRect;
double bestEnergy = -1;
foreach (var scale in CandidateScales)
// Step by SlideStep but always visit the last position, so a subject
// touching the right/bottom edge isn't cut by grid misalignment.
for (var y = 0; ; y = Math.Min(y + SlideStep, maxY))
{
var winW = (int)Math.Round(smallMaxRect.Width * scale);
var winH = (int)Math.Round(smallMaxRect.Height * scale);
if (winW < 2 || winH < 2 || winW > smallW || winH > smallH)
for (var x = 0; ; x = Math.Min(x + SlideStep, maxX))
{
continue;
}
var maxX = smallW - winW;
var maxY = smallH - winH;
var stride = smallW + 1;
for (var y = 0; y <= maxY; y += SlideStep)
{
for (var x = 0; x <= maxX; x += SlideStep)
var energy = SumRect(sat, stride, x, y, x + winW, y + winH);
if (energy > bestEnergy)
{
var energy = SumRect(sat, stride, x, y, x + winW, y + winH);
if (energy > bestEnergy)
{
bestEnergy = energy;
bestSmallRect = new Rectangle(x, y, winW, winH);
}
bestEnergy = energy;
bestSmallRect = new Rectangle(x, y, winW, winH);
}
if (x == maxX)
{
break;
}
}
}
if (bestEnergy < 0)
{
return fallback;
if (y == maxY)
{
break;
}
}
var sx = (double)srcW / smallW;