Saliency-based smart crop #2
No files matched your search
@@ -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.
|
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).
|
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.
|
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.
|
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.
|
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 double AspectMatchTolerance = 0.005;
|
||||||
private const int SmallLongEdge = 256;
|
private const int SmallLongEdge = 256;
|
||||||
private const int SlideStep = 8;
|
private const int SlideStep = 8;
|
||||||
private static readonly double[] CandidateScales = { 1.0, 0.85, 0.7 };
|
|
||||||
|
|
||||||
private readonly ILogger<SaliencySmartCropper> _logger;
|
private readonly ILogger<SaliencySmartCropper> _logger;
|
||||||
|
|
||||||
@@ -66,26 +65,28 @@ public sealed class SaliencySmartCropper : ISmartCropper
|
|||||||
var mag = ComputeSobelMagnitude(small);
|
var mag = ComputeSobelMagnitude(small);
|
||||||
var sat = BuildSat(mag, smallW, smallH);
|
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 smallMaxRect = MaxFittingCenteredRect(smallW, smallH, tgtAspect);
|
||||||
var bestSmallRect = smallMaxRect;
|
var winW = smallMaxRect.Width;
|
||||||
double bestEnergy = -1;
|
var winH = smallMaxRect.Height;
|
||||||
|
if (winW < 2 || winH < 2)
|
||||||
foreach (var scale in CandidateScales)
|
|
||||||
{
|
{
|
||||||
var winW = (int)Math.Round(smallMaxRect.Width * scale);
|
return fallback;
|
||||||
var winH = (int)Math.Round(smallMaxRect.Height * scale);
|
|
||||||
if (winW < 2 || winH < 2 || winW > smallW || winH > smallH)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var maxX = smallW - winW;
|
var maxX = smallW - winW;
|
||||||
var maxY = smallH - winH;
|
var maxY = smallH - winH;
|
||||||
var stride = smallW + 1;
|
var stride = smallW + 1;
|
||||||
|
var bestSmallRect = smallMaxRect;
|
||||||
|
double bestEnergy = -1;
|
||||||
|
|
||||||
for (var y = 0; y <= maxY; y += SlideStep)
|
// 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))
|
||||||
{
|
{
|
||||||
for (var x = 0; x <= maxX; x += SlideStep)
|
for (var x = 0; ; x = Math.Min(x + SlideStep, maxX))
|
||||||
{
|
{
|
||||||
var energy = SumRect(sat, stride, x, y, x + winW, y + winH);
|
var energy = SumRect(sat, stride, x, y, x + winW, y + winH);
|
||||||
if (energy > bestEnergy)
|
if (energy > bestEnergy)
|
||||||
@@ -93,13 +94,17 @@ public sealed class SaliencySmartCropper : ISmartCropper
|
|||||||
bestEnergy = energy;
|
bestEnergy = energy;
|
||||||
bestSmallRect = new Rectangle(x, y, winW, winH);
|
bestSmallRect = new Rectangle(x, y, winW, winH);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
if (x == maxX)
|
||||||
|
{
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bestEnergy < 0)
|
if (y == maxY)
|
||||||
{
|
{
|
||||||
return fallback;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var sx = (double)srcW / smallW;
|
var sx = (double)srcW / smallW;
|
||||||
|
|||||||
Reference in new issue
Block a user