4

我目前正在处理的应用程序需要透视图像失真功能。基本上我想做的是允许用户将图像加载到应用程序中,并根据他们可以指定的 4 个角点调整其透视图属性。

我看了一下ImageMagic。它具有一些带有透视调整的失真功能,但速度非常慢,并且某些输入给出了不正确的输出。

你们中的任何人都使用过任何其他库或算法。我正在用 C# 编码。

任何指针将不胜感激。

谢谢

4

5 回答 5

6

这似乎正是您(和我)正在寻找的:http: //www.codeproject.com/KB/graphics/YLScsFreeTransform.aspx

它将使用您提供的 4 个 X/Y 坐标拍摄图像并对其进行扭曲。

快速、免费、简单的代码。经过测试,它工作得很好。只需从链接下载代码,然后像这样使用 FreeTransform.cs:

using (System.Drawing.Bitmap sourceImg = new System.Drawing.Bitmap(@"c:\image.jpg")) 
{ 
    YLScsDrawing.Imaging.Filters.FreeTransform filter = new YLScsDrawing.Imaging.Filters.FreeTransform(); 
    filter.Bitmap = sourceImg;
    // assign FourCorners (the four X/Y coords) of the new perspective shape
    filter.FourCorners = new System.Drawing.PointF[] { new System.Drawing.PointF(0, 0), new System.Drawing.PointF(300, 50), new System.Drawing.PointF(300, 411), new System.Drawing.PointF(0, 461)}; 
    filter.IsBilinearInterpolation = true; // optional for higher quality
    using (System.Drawing.Bitmap perspectiveImg = filter.Bitmap) 
    {
        // perspectiveImg contains your completed image. save the image or do whatever.
    } 
} 
于 2012-01-12T09:50:10.240 回答
2

如果是透视变换,应该可以指定一个匹配四个角的4x4变换矩阵。

计算该矩阵,然后在矩阵上的结果图像上应用每个像素,从而产生“映射”像素。请注意,这个“映射”像素很可能位于两个甚至四个像素之间。在这种情况下,使用您最喜欢的插值算法(例如双线性、双三次)来获得插值后的颜色。

这确实是完成它的唯一方法,并且不能更快地完成。如果此功能至关重要并且您绝对需要它快速,那么您需要将任务卸载到 GPU。例如,您可以调用 DirectX 库对纹理应用透视变换。即使在没有 GPU 的情况下,它也可以变得非常快,因为 DirectX 库使用 SIMD 指令来加速矩阵计算和颜色插值。

于 2011-04-17T11:20:04.103 回答
2

Paint .NET可以做到这一点,并且还有效果的自定义实现。您可以索要源代码或使用 Reflector 阅读它并了解如何对其进行编码。

于 2011-04-17T10:44:02.717 回答
1

有同样的问题。这是从gimp移植的源代码的演示代码。

于 2014-11-19T10:04:49.423 回答
1

YLScsFreeTransform 不能按预期工作。更好的解决方案是ImageMagic

以下是在 c# 中使用它的方法:

using(MagickImage image = new MagickImage("test.jpg"))
{
    image.Distort(DistortMethod.Perspective, new double[] { x0,y0, newX0,newY0, x1,y1,newX1,newY1, x2,y2,newX2,newY2, x3,y3,newX3,newY3 });
    control.Image = image.ToBitmap();
}
于 2015-04-03T04:23:26.687 回答