Perspective Image Distortion

over.drive picture over.drive · Apr 17, 2011 · Viewed 11.3k times · Source

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

Answer

Doug S picture Doug S · Jan 12, 2012

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