开发者

Perspective Image Distortion

The application I am working on currently requires functionality for Perspecti开发者_如何学Cve Image Distortion. Basically what I want to do is to allow users to load an image into the application and adjust its perspective view properties based on 4 corner points that they can specify.

I had a look at ImageMagic. It has some distort functions with perpective adjustment but is very slow and some certain inputs are giving incorrect outputs.

Any of you guys used any other library or algorithm. I am coding in C#.

Any pointers would be much appreciated.

Thanks


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.
    } 
} 


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.


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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜