2

I have a typical source image with a 3:4 aspect ratio. I also have a set of coordinates that I need to map the image onto in a separate image. The coordinates are not perfectly rectangular; if anything they define an irregular mesh.

So for example, (0,0) might map to (12,33), (120,0) => (127,36), (240,0)=>(226,13), etc.

My goal is to get my source image to fit onto the new shape by mapping the source coordinates to the destination and applying distortion.

What are some ways to accomplish this? I'm using .NET but am fine calling out to e.g. ImageMagick.

EDIT: As requested, here's a picture. The left image is a simple photo, evenly subdivided (ignore my crappy MSPaint skills). The right side shows the target shape. I have coordinates for each dot on the right side. The goal is to map (project?) the flat image onto the shape so that the dots line up.

enter image description here

roufamatic
  • 18,187
  • 7
  • 57
  • 86
  • Are you trying to accomplish [this](http://stackoverflow.com/questions/7838487/executing-cvwarpperspective-for-a-fake-deskewing-on-a-set-of-cvpoint)? It's not clear to me, I suggest you upload some images that demonstrate what you are trying to do. – karlphillip May 11 '12 at 19:06
  • Picture added. Not quite the same goal as that one, almost the opposite but with the added challenge of an uneven shape target. – roufamatic May 11 '12 at 20:14

2 Answers2

2

Look into ImageMagick's Freeform Distorts.

japreiss
  • 11,111
  • 2
  • 40
  • 77
2

I have some examples of the distort operators on my site ( http://www.rubblewebs.co.uk/imagemagick/operators/distort.php ) - using php but you will get the idea.

You probably want the shepards method.

$cmd = "$input -matte -virtual-pixel transparent".  
" -distort Shepards \"0,0 0,0  0,100 20,100 0,200".  
" 0,200 133,200 133,180 266,200 266,200 266,100".  
" 246,100 266,0 266,0 133,0 133,20\" ";  
exec("convert $cmd shepards.png");

enter image description here

Bonzo
  • 5,169
  • 1
  • 19
  • 27
  • Big help, thanks. My points are too close together for shepards it appears as the result is a swirly mess. But polynomial distortion seemed to work really well. – roufamatic May 12 '12 at 06:41
  • Is there any way to do it in php? – Alauddin Ahmed Jul 05 '18 at 10:46
  • The code is for php but I assume you mean Imagick. If so yes; but I do not use Imagick: http://php.net/manual/en/imagick.distortimage.php – Bonzo Jul 05 '18 at 17:56