Perspective Image Distortion

前端 未结 5 1195
旧时难觅i
旧时难觅i 2021-01-14 02:17

The application I am working on currently requires functionality for Perspective Image Distortion. Basically what I want to do is to allow users to load an image into the ap

相关标签:
5条回答
  • 2021-01-14 03:03

    Paint .NET can do this and there are also custom implementations of the effect. You could ask for the source code or use Reflector to read it and get an idea of how to code it.

    0 讨论(0)
  • 2021-01-14 03:06

    This seems to be exactly what you (and I) were looking for: http://www.codeproject.com/KB/graphics/YLScsFreeTransform.aspx

    It will take an image and distort it using 4 X/Y coordinates you provide.

    Fast, free, simple code. Tested and it works beautifully. Simply download the code from the link, then use FreeTransform.cs like this:

    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.
        } 
    } 
    
    0 讨论(0)
  • 2021-01-14 03:11

    If it is a perspective transform, you should be able to specify a 4x4 transformation matrix that matches the four corners.

    Calculate that matrix, then apply each pixel on the resulting image on the matrix, resulting in the "mapped" pixel. Notice that this "mapped" pixel is very likely going to lie between two or even four pixels. In this case, use your favorite interpolation algorithm (e.g. bilinear, bicubic) to get the interpolated color.

    This really is the only way for it to be done and cannot be done faster. If this feature is crucial and you absolutely need it to be fast, then you'll need to offload the task to a GPU. For example, you can call upon the DirectX library to apply a perspective transformation on a texture. That can make it extremely fast, even when there is no GPU because the DirectX library uses SIMD instructions to accelerate matrix calculations and color interpolations.

    0 讨论(0)
  • 2021-01-14 03:11

    Had the same problem. Here is the demo code with sources ported from gimp.

    0 讨论(0)
  • 2021-01-14 03:15

    YLScsFreeTransform doesn't work as expected. Way better solution is ImageMagic

    Here is how you use it in 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();
    }
    
    0 讨论(0)
提交回复
热议问题