透视图像失真

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

我看了一下ImageMagic。 它具有一些扭曲function,可以进行调整,但速度非常慢,某些输入会产生不正确的输出。

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

任何指针都将非常感激。

谢谢

这似乎正是你(和我)正在寻找的: 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. } } 

Paint .NET可以做到这一点,并且还有效果的自定义实现 。 您可以要求输入源代码或使用Reflector读取它并了解如何编写代码。

如果它是透视变换,您应该能够指定与四个角相匹配的4×4变换矩阵。

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

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

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

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(); }