4

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

1

5 Answers 5

6

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.
    } 
} 
8
  • 2
    This is not a perspective. Check this out: forums.getpaint.net/index.php?/topic/…
    – ironic
    Nov 16, 2014 at 13:13
  • @ironic: This code is close enough to perspective. A coder can deal with more code/overheard to be more exact, if they wish. It's just a matter of where to strike the balance for each project's needs.
    – Doug S
    Oct 22, 2015 at 1:44
  • define 'close enough'. even though there may be use cases when this code may be used interchangeably with perspective transform code, it is by no means a 'perspective' transformation.
    – ironic
    Oct 30, 2015 at 16:11
  • 2
    @ironic is right: this is not perspective, it is trapezoid transformation, which is much easier to do than perspective.
    – Fortega
    Dec 9, 2015 at 14:51
  • 1
    @DougS, let me clarify what I needed: a transformation that will create an image matching(in terms of pixel positioning) one produced by usual 3d rendering. for this cause trapezoid transformation is quite useless.
    – ironic
    Nov 18, 2016 at 10:04
2

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.

1
  • @igor brejc: excellent addition! Just for the transform it might be overkill but a great source of info.
    – Emond
    Apr 17, 2011 at 12:28
2

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.

1

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

1
  • That's really helpfull! Thanks a lot!
    – Gope
    Nov 18, 2016 at 7:44
1

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();
}
4
  • Please explain how YLScsFreeTransform "doesn't work as expected".
    – Doug S
    Oct 22, 2015 at 1:45
  • Because YLScsFreeTransform has less parameters to apply distort method which makes the result irrelevant to what is intended.
    – GO.exe
    Jan 9, 2016 at 12:27
  • 1
    "Irrelevant to what is intended"? The OP simply wants to "load an image into the application and adjust its perspective view properties based on 4 corner points". YLScsFreeTransform does that. It sounds like you're saying it doesn't do what you want, which is irrelevant to the OP's question.
    – Doug S
    Mar 30, 2016 at 2:13
  • +1 Very good suggestion to use ImageMagic. It helped me discover a lot of things. Still for your example you'll need to add image.VirtualPixelMethod = VirtualPixelMethod.Transparent;
    – 41686d6564
    Aug 29, 2016 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.