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:
... and distort it to match the area shown in black on this image:
... resulting in an image something like this:
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!
Distorting an image to fit a mask
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Distorting an image to fit a mask
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.
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.
snibgo's IM pages: im.snibgo.com
Re: Distorting an image to fit a mask
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?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Distorting an image to fit a mask
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.
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.
snibgo's IM pages: im.snibgo.com
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Distorting an image to fit a mask
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
http://www.imagemagick.org/Usage/distorts/#polynomial
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/