Win2D 梯形校正

Win2D Keystone Correction

我正在尝试使用 Win2D/C# 使用高架投影仪投影图像,我需要使用 Win2D 效果进行梯形校正(预扭曲图像)作为最后一步。

基本上我正在绘制一个矩形,然后在渲染之前尝试使用 Transform3DEffect 对其进行变形。我不知道要使用什么矩阵转换组合来让它工作。做一个完整的相机投影似乎有点矫枉过正,因为我只需要在一个方向上变形(见下图)。我应该使用什么转换?

使用像下面这样的图片,可以得到类似的效果。

https://i.stack.imgur.com/5QnEm.png

我不确定 "bending" 的结果。

用于创建置换贴图的代码(使用 GDI+,因为您可以快速设置像素)。 您可以找到 here

的 LockBitmap
static void DrawDisplacement(int width, int height, LockBitmap lbmp)
    {
        for (int x = 0; x < width; x++)
            for (int y = 0; y < height; y++)
            {
                int roff = (int)((((width >> 1) - x) / (float)(width >> 1)) * ((height - y) / (float)height) * 127);
                int goff = 0;

                lbmp.SetPixel(x, y, Color.FromArgb(127 - roff, 127 - goff, 0));
            }
    }

在 Win2D 中绘图看起来像这样,其中 displacementImage 是加载的文件,屏幕外是我在其上绘制网格的 'CanvasRenderTarget'。

//Scaling for fitting the image to the content
ICanvasImage scaledDisplacement = new Transform2DEffect
{
    BorderMode = EffectBorderMode.Hard,
    Source = displacementImage,
    TransformMatrix = Matrix3x2.CreateScale((float) (sender.Size.Width / displacementImage.Bounds.Width), (float) (sender.Size.Height / displacementImage.Bounds.Height)),
    Sharpness = 1f,
    BufferPrecision = CanvasBufferPrecision.Precision32Float,
    InterpolationMode = CanvasImageInterpolation.HighQualityCubic,
};

//Blurring, for a better result
ICanvasImage displacement = new GaussianBlurEffect
{
    BorderMode = EffectBorderMode.Hard,
    Source = scaledDisplacement,
    BufferPrecision = CanvasBufferPrecision.Precision32Float,
    BlurAmount = 2,
    Optimization = EffectOptimization.Quality,
};

ICanvasImage graphicsEffect = new DisplacementMapEffect
{
    Source = offscreen,
    Displacement = displacement,
    XChannelSelect = EffectChannelSelect.Red,
    YChannelSelect = EffectChannelSelect.Green,
    Amount = 800,//change for more or less displacement
    BufferPrecision = CanvasBufferPrecision.Precision32Float,
};