SixLabors / ImageSharp

:camera: A modern, cross-platform, 2D Graphics library for .NET
https://sixlabors.com/products/imagesharp/
Other
7.26k stars 844 forks source link

Add QuadDistortion to ProjectiveTransformBuilder #2748

Open Socolin opened 1 month ago

Socolin commented 1 month ago

Prerequisites

Description

Add 2 new methods: PrependQuadDistortion and AppendQuadDistortion to ProjectiveTransformBuilder.

Those method allow to apply a distortion on an image by specifying the requested coordinate for each corner.

Example:

using (var img1 = Image.Load("cat-original.png"))
{
    var topLeft = new Point(25, 25);
    var topRight = new Point(200, 75);
    var bottomRight = new Point(225, 175);
    var bottomLeft = new Point(38, 225);

    img1.Mutate(c => c.Transform(new ProjectiveTransformBuilder()
        .AppendQuadDistort(topLeft, topRight, bottomRight, bottomLeft)));
    img1.Save("output-small2.png");
}

Source image Result image

CLAassistant commented 1 month ago

CLA assistant check
All committers have signed the CLA.

CLAassistant commented 1 month ago

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

JimBobSquarePants commented 1 month ago

Thanks @Socolin for this, it is very interesting! I'll have to put my math's hat on to look at this.

I'm curious. Is this potentially an alternative (better) approach to ProjectiveTransformBuilder.PrependTaper?

Socolin commented 5 days ago

FYI: The performance test I did

TestPerformanceGaussianElimination.zip


BenchmarkDotNet v0.13.12, Ubuntu 22.04.4 LTS (Jammy Jellyfish)
AMD Ryzen 9 7950X, 1 CPU, 32 logical and 16 physical cores
.NET SDK 8.0.302
  [Host]     : .NET 8.0.6 (8.0.624.26715), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
  DefaultJob : .NET 8.0.6 (8.0.624.26715), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI

| Method            | Mean     | Error   | StdDev  |
|------------------ |---------:|--------:|--------:|
| Matrix            | 526.6 ns | 1.02 ns | 0.96 ns |
| MatrixCustom      | 237.0 ns | 3.31 ns | 3.09 ns |
| MatrixCustomFloat | 234.1 ns | 4.64 ns | 4.34 ns |
| Matrix8x8         | 293.5 ns | 0.32 ns | 0.28 ns |