Page 1 of 3

Help with composite images

Posted: 2009-04-17T01:48:44-07:00
by bob1975
I have a question on creating composite image from two images.

I would like to create a composite image from below two images (Image 1 and Image 2) and create a final image (Image 3). Problem I am facing is creating a curved image from Image 2 so that I can then create composite image. I have explored all pages on ImageMagick usage and read through the forums but will no luck. Anyone can help me with this? Thanks

Bob.

1. Image
http://img2.imageshack.us/img2/2534/mugblank.jpg

2. Image
http://img144.imageshack.us/img144/563/img5967.jpg

3. Image (http://img11.imageshack.us/img11/7426/finalmug.jpg)

Re: Help with composite images

Posted: 2009-04-17T10:57:07-07:00
by fmw42
To my knowledge, IM does not have texture mapping, nor a specific image to cylinder in perspective warp.

The closest you can come is using -wave, but it is sinusoidal and not circular.

The alternate is to use -fx and come up with your own formula for warping into a cyclinder. See some of the examples like the hourglass at http://www.imagemagick.org/Usage/displace/

Re: Help with composite images

Posted: 2009-04-17T17:15:51-07:00
by bob1975
Thanks fmw42.

You are correct. I have tried wave with different variations, but the results are not satisfactory. I will keep reading and try to understand the -fx thing.

I know this might not sound good, but can someone help me create these kind of transform?

Thanks

Re: Help with composite images

Posted: 2009-04-17T18:21:18-07:00
by fmw42
I am working on a script that might do a warp to a cylinder. But I don't know if I can put in the perspective aspect.

Here is what I have so far as a result. But I am not sure it is truly a cylinder, yet. So I will continue with this for fun.

original
zelda3.jpg
Image

Warped image
Image

Here is an attempt with your image:

original
img5967.jpg
Image

Warped image
Image

Shrinking vertically 70%
Image

Re: Help with composite images

Posted: 2009-04-18T00:58:36-07:00
by bob1975
Thanks.

Just a thought : I think that somehow from the center of the image (or about 40% on each side from the center) the a curve is applied to the image.

Re: Help with composite images

Posted: 2009-04-18T10:27:13-07:00
by fmw42
The issue is that in your example, the image texture has been mapped onto a cylinder that is viewed in perspective.

I don't think that I or IM will be able to do that kind of thing very easily if at all.

Warping the image into a cylinder is achievable with IM as I have demonstrated above, but not easily with a cylinder in perspective. You can try perspectively tilting the image first and then warping it, but this still will not be the same as texture mapping it properly onto the cylinder in perspective.

You probably need to look into a true 3D texture mapping application. IM is really only 2D.

Re: Help with composite images

Posted: 2009-04-18T19:11:58-07:00
by bob1975
Thanks for trying and all the help. I will keep on trying and post things here if I find something.

Re: Help with composite images

Posted: 2009-04-23T00:12:33-07:00
by anthony
bob1975 wrote:You are correct. I have tried wave with different variations, but the results are not satisfactory.
If you plan to do a lot of this generating a displacement map would be the way to go. A -fx can generate the map which can then be applied to multiple images without needing to recalculate it. But getting that initial map is still the same problem.

You said you tried -wave in what way. The curve on a can is not precisely a sinusiodal curve, it is a cylindrical curve with BOTH vertical Y displacements, as well as horizontal compression at the edges, or X displacements. That is why 'wave' is not satisfactory. Both displacements will be needed.

The Y displacement can of course be appliy using -wave, but it will definataly not provide you with the X compression displacement needed.

the hourglass displacement will be no good as that is simply a horizonal linear compression, that is the compression is uniform over the whole horizontal line.

Generation is thus going to require some thought. But lets forget about the vertical 'Y' displacement for now, and concentrate on teh X displacements.

You will need to create a displacement map that maps a point on the can, to a point on the original image that you want to wrap around that can. so map a X poition on the can, to a X position on the original image, that is X position on a circle to an angle! X - > angle That the arccos() function!

That is the key, and the rest is much like what is shown in 'Distortion Maps'. Create a absolute map first, then convert that to a 'displacement map' for speed.


PS: and please let us know how you go. I am sure fred can also take it from here as well!

Re: Help with composite images

Posted: 2009-04-23T09:36:04-07:00
by fmw42
I believe it should be arcsin and not arccos, if the angle is measured from the center of the cylinder towards its rim. That is what I used to generate my examples. I am still working on this as I am not quite satisfied with the results. I cannot fully duplicate what I produced with -fx by using compose displace.

Re: Help with composite images

Posted: 2009-04-23T18:05:22-07:00
by anthony
The -fx will of course be using absolute coordinates whcih is applied directly is a ArcCos.

A displacement map however requires relative coordinates, so you need to subtract the current
pixel location from the absolute coordinates to get a relative coordinate.

With Arccos() converted to a displacement you will generate a Arcsin() instead, Though you never gave the actual command you used in the examples you provided.

Once you have the X displacement compression worked out, you can simply add an appropriate 'wave' warping for the Y coordinate. But again it isn't actually a 'sin curve' but a circular curve. The top edge of the image should form a ellipse (flatted circle).

That is the vertical displacement should actually follow the curve -sqrt(1-x^2)
where X goes from -1 to +1 over the width of the cylinder.
Or for a normalized range of 0 to 1 across the cylinder -sqrt(1-4*(x-.5)^2)

So now we just need to settle the X displacement.

Re: Help with composite images

Posted: 2009-04-23T18:49:48-07:00
by fmw42
why should -fx use arcsin and composite displace use arcos? Makes no sense to me. Are they both not doing an inverse transformation? Furthermore I can duplicate most of the effect from arcsin in -fx using arcsin in composite displace.

The horizontal position relative to the center of the cylinder is R*sin(angle) orthographically projecting from the input image wrapped on the half cylinder to the output, where -pi/2 to +pi/2 for angle corresponds to the left and right side of the input image. Thus the inverse transformation goes as (2/pi)*arcsin(xd), where xd goes from -1 to +1 on the left and right sides of the output.

Perhaps I am missing something here or doing something incorrectly???

Absolute Displacement (fx):

Image
infile="img5967.jpg"
width=`identify -format %w $infile`
height=`identify -format %h $infile`
xc=`echo "scale=1; ($width - 1) / 2" | bc`
sxc=`echo "scale=6; 1 / ($osx*$xc)" | bc`
isx=`echo "scale=6; 1 / $isx" | bc`
ipid2=`echo "scale=6; 1/(2*a(1))" | bc -l`
ffx="ffx=$ipid2*asin(xd);"
convert -monitor $infile -virtual-pixel black -fx \
"xd=$sxc*(i-$xc); $ffx xs=$isx*$xc*ffx+$xc; u.p{xs,j}" \
$outfile

where
osx=1
isx=0.35 (before inversion)
Image

Relative Displacement (displace):

Replace the convert above with:

convert -monitor -size ${width}x1 xc: -virtual-pixel black -fx \
"xd=$sxc*(i-$xc); $ffx xs=0.5*$isx*ffx+0.5; xd>1?1:xs" \
-scale ${width}x${height}! \
tmp.png
composite tmp.png $infile -virtual-pixel black -displace ${amp}x0 $outfile

and either
use isx=1 and amp=100
Image

or

use isx=0.5 and amp=50
Image


But I get a better effect from the Absolute Displacement using

isx=1
osx=0.5
Image

Thus I am still puzzled and still working to understand this better. Also the first 3 images above are only slightly different from just resizing the image by about 50% horizontally (apart from the black background).

Image

Re: Help with composite images

Posted: 2009-04-24T10:42:17-07:00
by fmw42
I have now rederived the equations more precisely and formally in terms of the cylinder radius.

If i=horizontal dimension of output, xc=horizontal center, xs is horizontal dimension of input, then the forward equation in width is

i-xc = R*sin((pi/2)*(xs-xc)/xc)

Inverting this, we get

xs = (2*xc/pi)*asin((i-xc)/R) + xc


Thus as an absolute displacement usng fx one has:

infile="img5967.jpg"
Image

radius=40
dx=1
width=`identify -format %w $infile`
height=`identify -format %h $infile`
xc=`echo "scale=1; ($width - 1) / 2" | bc`
ipid2=`echo "scale=6; 1/(2*a(1))" | bc -l`
ffx="ffx=$ipid2*asin(xd);"
[ "$dx" != "1" ] && ffx="ffx=sign(xd)*($ipid2*asin(abs(xd)))^$dx;"
convert -monitor $tmpA -virtual-pixel $vp -fx \
"xd=(i-$xc)/$radius; $ffx xs=$xc*ffx+$xc; u.p{xs,j}" \
$outfile
Image

which looks quite reasonable.


Using a relative displacement from composite displace one gets:

radius=40
amp=40
dx=1
width=`identify -format %w $infile`
height=`identify -format %h $infile`
xc=`echo "scale=1; ($width - 1) / 2" | bc`
ipid2=`echo "scale=6; 1/(2*a(1))" | bc -l`
ffx="ffx=$ipid2*asin(xd);"
[ "$dx" != "1" ] && ffx="ffx=sign(xd)*($ipid2*asin(abs(xd)))^$dx;"
convert -monitor -size ${width}x1 xc: -virtual-pixel $vp -fx \
"xd=(i-$xc)/$radius; $ffx xs=0.5*ffx+0.5; xd>1?1:xs" \
-scale ${width}x${height}! \
tmp0.png
composite tmp0.png $infile -virtual-pixel black -displace ${amp}x0 $outfile
Image

which is not quite as similar as I would have expected or liked.

However, if I apply an exponent dx=2 to the formula above for the relative distortion, I do get something more similar to the absolute distortion version (using dx=1)
Image


So I am still puzzled about converting an absolute displacement to a relative one to gain speed without losing precision.

Is this exaggeration needed in the relative distortion to compensate for the fact that in the absolute distortion the range was -1 to +1 (ie a range of 2) and in the relative distortion the range was 0 to 1 (ie a range of 1). Thus some kind of (non-linear?) amplification of a factor of 2 is needed?

Re: Help with composite images

Posted: 2009-04-26T17:44:05-07:00
by anthony
The speed increase in displacement comes not though the formula but in generating a displacement map that can be re-used.

I like the idea of a xc or center of the cylinder, That with a -virtual-pixel setting of HorizontalTile
and an image that tiles horizontally could allow you to generate animated rotating cylindrical views of the image ;-)

One of the trickier aspects appears to be scaling. That is the final diameter (2 * radius) will actually be directly related to the width of the image segment that will be display.

Of course how much of the input image should be 'visible' depends on what you are doing. Especially if you plan on generating a rotating animation or not.

Re: Help with composite images

Posted: 2009-04-26T17:47:42-07:00
by anthony
fmw42 wrote:Is this exaggeration needed in the relative distortion to compensate for the fact that in the absolute distortion the range was -1 to +1 (ie a range of 2) and in the relative distortion the range was 0 to 1 (ie a range of 1). Thus some kind of (non-linear?) amplification of a factor of 2 is needed?
I am not certain as to why you needed to do that. It should come out the same, Other than a linear mapping of a -1 to +1 range into a 0 to 1 range needed for the displacement map.

I can't see why it is different.

Re: Help with composite images

Posted: 2009-04-26T18:42:55-07:00
by fmw42
Anthony wrote:I can't see why it is different.
Neither can I and the fact that they don't seem to give the same results raises questions about what composite displace is really doing. I have tried reproducing my (fx) bubblewarp script using composite displace in my bump script and after several tries with different functions, I still cannot make it come out the same (although it is complicated by using -distort polar/depolar). However, as bump is more of an effect than an exact transformation, it was not originally so bothering. But I would really like to understand how to make composite displace work equivalent to using -fx.

I was really hoping that you might be able to shed some light on it here with this simpler one-dimensional analog situation with the cylinder.