Page 1 of 1

Distorting an image to fit a mask

Posted: 2013-07-19T11:52:47-07:00
by antun
Hi there,

I'm trying to use ImageMagick to distort an image of some text into a mask image. I've been reading through the examples on mapping and distorting, but without much luck.

In short, I'm aiming to take an image like this:

Image

... and distort it to match the area shown in black on this image:

Image

... resulting in an image something like this:

Image

The images I'm using are all PNGs with transparent backgrounds. I was able to create the resulting distored image shown above using Forward Bilinear Distortion, but the problem there is that I have to provide a bunch of control points, which is a manual process. I'm looking for an automated way to distort the text.

Is this possible?

Thanks in advance!

Re: Distorting an image to fit a mask

Posted: 2013-07-19T12:52:36-07:00
by snibgo
I've done something similar for video frames using displacement maps (http://www.imagemagick.org/Usage/mapping/#distort). The problem can be expressed like this:

1. There is vertical displacement only, not horizontal.

2. The bottom of each letter is displaced by a different amount to the top. Put another way: the heights are stretched, then the letters are displaced upwards.

3. The height of each letter is given by the depth of the black mark.

4. The displacement of the bottom is given by the vertical height of the bottom (white) triangle.

We can get (3) the depth of the black mark by resizeing to a single row. At each resulting x-value, darker pixels represent a thicker black mark at that location.

Similarly for (4): fill the top triangle with black, resize to a single row, and this gives the height of the bottom triangle.

Re: Distorting an image to fit a mask

Posted: 2013-07-19T15:15:48-07:00
by antun
Thanks. I think that would work. In my distortion it wasn't only vertical, but I think the result in vertical-only would be OK for my purposes. Are there specific examples that I should look at?

Re: Distorting an image to fit a mask

Posted: 2013-07-19T15:56:21-07:00
by snibgo
The method I outlined assumes the mark is at least as wide as the text. If it is narrower, or you also want horizontal displacement, you'll have to adapt it.

The page I linked to is a good source.

An alternative approach would be to find control points from the black mark. For example: crop the black mark to a one-pixel-wide column and find the first and last black pixel. Repeat for four columns, evenly spaced. This gives you eight control points.

Re: Distorting an image to fit a mask

Posted: 2013-07-25T16:25:06-07:00
by anthony
You can use a polynomial fit. Convert the mask into a array of points (corners, mid-edges, middle, etc) and map the source image to those coordinates, using
http://www.imagemagick.org/Usage/distorts/#polynomial