Add saliency-based smart crop before resize
Replace the center-crop cover resize with a Sobel edge-density search that picks the target-aspect window with the most edge energy, so a subject off-center isn't sliced when the input aspect differs from the frame's. Failures fall back to the centered crop. Golden fixtures are regenerated and an asymmetric-edges input exercises the bias. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
No files matched your search
|
After Width: | Height: | Size: 115 KiB |
|
Before Width: | Height: | Size: 252 KiB After Width: | Height: | Size: 251 KiB |
|
Before Width: | Height: | Size: 234 KiB After Width: | Height: | Size: 235 KiB |
|
Before Width: | Height: | Size: 259 KiB After Width: | Height: | Size: 230 KiB |
|
After Width: | Height: | Size: 101 KiB |
|
Before Width: | Height: | Size: 266 KiB After Width: | Height: | Size: 269 KiB |
|
Before Width: | Height: | Size: 248 KiB After Width: | Height: | Size: 248 KiB |
|
Before Width: | Height: | Size: 261 KiB After Width: | Height: | Size: 258 KiB |
|
After Width: | Height: | Size: 162 KiB |
|
Before Width: | Height: | Size: 308 KiB After Width: | Height: | Size: 308 KiB |
|
Before Width: | Height: | Size: 274 KiB After Width: | Height: | Size: 273 KiB |
|
Before Width: | Height: | Size: 288 KiB After Width: | Height: | Size: 248 KiB |
|
After Width: | Height: | Size: 143 KiB |
|
Before Width: | Height: | Size: 293 KiB After Width: | Height: | Size: 296 KiB |
|
Before Width: | Height: | Size: 268 KiB After Width: | Height: | Size: 266 KiB |
|
Before Width: | Height: | Size: 280 KiB After Width: | Height: | Size: 246 KiB |
|
After Width: | Height: | Size: 34 KiB |
@@ -1,6 +1,8 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using FrameProcessor.Domain;
|
||||
using FrameProcessor.ImagePipeline;
|
||||
using FrameProcessor.ImagePipeline.SmartCrop;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Formats.Jpeg;
|
||||
using SixLabors.ImageSharp.Formats.Png;
|
||||
@@ -35,8 +37,12 @@ public class ImagePipelineTests
|
||||
"portrait.jpg",
|
||||
"tiny-1x1.png",
|
||||
"non-divisible.jpg",
|
||||
"asymmetric-edges.jpg",
|
||||
};
|
||||
|
||||
private static FrameProcessor.ImagePipeline.ImagePipeline CreatePipeline()
|
||||
=> new(new SaliencySmartCropper(NullLogger<SaliencySmartCropper>.Instance));
|
||||
|
||||
public static IEnumerable<object[]> AllCombinations()
|
||||
{
|
||||
foreach (var algo in Enum.GetValues<DitherAlgorithm>())
|
||||
@@ -61,7 +67,7 @@ public class ImagePipelineTests
|
||||
$"Missing golden fixture: {expectedPath}. Regenerate by running 'UPDATE_GOLDEN=1 dotnet test'.");
|
||||
|
||||
var frame = LandscapeFrame with { Dithering = algorithm };
|
||||
var pipeline = new FrameProcessor.ImagePipeline.ImagePipeline();
|
||||
var pipeline = CreatePipeline();
|
||||
using var stream = File.OpenRead(inputPath);
|
||||
var actual = pipeline.Process(stream, frame);
|
||||
var expected = File.ReadAllBytes(expectedPath);
|
||||
@@ -79,7 +85,7 @@ public class ImagePipelineTests
|
||||
|
||||
WriteInputFixtures();
|
||||
|
||||
var pipeline = new FrameProcessor.ImagePipeline.ImagePipeline();
|
||||
var pipeline = CreatePipeline();
|
||||
foreach (var algorithm in Enum.GetValues<DitherAlgorithm>())
|
||||
{
|
||||
foreach (var input in InputNames)
|
||||
@@ -106,6 +112,45 @@ public class ImagePipelineTests
|
||||
WriteJpeg(Path.Combine(inputsDir, "portrait.jpg"), 1200, 1600);
|
||||
WriteJpeg(Path.Combine(inputsDir, "non-divisible.jpg"), 1234, 789);
|
||||
WriteTinyPng(Path.Combine(inputsDir, "tiny-1x1.png"));
|
||||
WriteAsymmetricEdges(Path.Combine(inputsDir, "asymmetric-edges.jpg"));
|
||||
}
|
||||
|
||||
private static void WriteAsymmetricEdges(string path)
|
||||
{
|
||||
// 1600x900 (aspect ~1.78) processed by a 1600x1200 landscape frame
|
||||
// (aspect ~1.33) requires horizontal cropping. Left half is flat gray
|
||||
// (low edge energy); right half is an 8x8 black/white checkerboard
|
||||
// (high edge energy). Saliency should bias the crop window toward the
|
||||
// checkerboard side.
|
||||
const int width = 1600;
|
||||
const int height = 900;
|
||||
using var image = new Image<Rgba32>(width, height);
|
||||
var gray = new Rgba32(0x80, 0x80, 0x80, 0xFF);
|
||||
var black = new Rgba32(0x00, 0x00, 0x00, 0xFF);
|
||||
var white = new Rgba32(0xFF, 0xFF, 0xFF, 0xFF);
|
||||
image.ProcessPixelRows(accessor =>
|
||||
{
|
||||
for (var y = 0; y < accessor.Height; y++)
|
||||
{
|
||||
var row = accessor.GetRowSpan(y);
|
||||
for (var x = 0; x < row.Length; x++)
|
||||
{
|
||||
if (x < width / 2)
|
||||
{
|
||||
row[x] = gray;
|
||||
}
|
||||
else
|
||||
{
|
||||
var cellX = (x - width / 2) / 8;
|
||||
var cellY = y / 8;
|
||||
row[x] = ((cellX + cellY) & 1) == 0 ? black : white;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
var encoder = new JpegEncoder { Quality = 90 };
|
||||
using var fs = File.Create(path);
|
||||
image.SaveAsJpeg(fs, encoder);
|
||||
}
|
||||
|
||||
private static void WriteJpeg(string path, int width, int height)
|
||||
|
||||