warp image onto another image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
sssddd
Posts: 14
Joined: 2011-02-15T03:16:59-07:00
Authentication code: 8675308

warp image onto another image

Post by sssddd »

Basically the goal is to put an arbitrary image onto a picture of a t-shirt, so that it looks as if the image is printed on the shirt. See below for an example.
Image
An example result:
Image

What I'm doing now, is first using shepards distortion to distort the image, then use -compose displace using a region of tshirt (grayscaled) to add some surface displacement, then add highlights/shadows by -compose multiply, finally adding this to the shirt at the same region. This kinda works, but the major problem is I have to manually supply control points to shepards distortion in order to distort the image so that it follows the flow of the shirt. Try to imagine there is a grid of control points on the shirt that are straight lines if the shirt is pictured perfectly flat and straight, then imagine where the corresponding point should land on the shirt since the shirt is distorted. This gives me a bunch of coordinates that I used to do shepards distortion. But this will not work if the shirt picture is changed. Basically I want something that works for warping arbitrary image onto arbitrary shirt pictures. I wonder if there is a way to automatically find the control point pairs using the grayscale image? For an arbitrary point on the shirt in the distorted shirt picture, maybe use its grayscale as the elevation value, and add the rotation of the shirt to compute the coordinate of the source point as if the shirt was straight? Or what other ways can I achieve the same goal?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: warp image onto another image

Post by fmw42 »

see my example at viewtopic.php?f=1&t=16921#p62696

I don't know if this is a generic solution or not.
sssddd
Posts: 14
Joined: 2011-02-15T03:16:59-07:00
Authentication code: 8675308

Re: warp image onto another image

Post by sssddd »

Thanks I have read that thread before. There are a few problems with that approach. Simple displace only works when the shirt is almost facing straight front. When the shirt is rotated away from front, perspective must be added to the picture so the image on the shirt have a perspective to it. However, perspective distortion seems only work for straight area, but the shirt surface is arbitrarily deformed, that's why I tried with multi-point shepards distortion so the whole area is distorted according to the way the shirt is rotated.

The other problem is how to determine the coordinates where the image should be composed onto the shirt. I only know the image should be at the center of the shirt if the shirt is straight forward facing and flat. But the shirt is rotated and distorted, how do I translate the virtual coordinate of the "flat picture" to the actual shirt coordinates where the image should be anchored? Also because the shirt has perspective, a rectangular image on the "flat" shirt is no longer rectangular on this shirt. I still crop a rectangular region from the shirt to be used for displacement of the image, but how to determine the appropriate region to be cropped is tricky, similar to how to determine the shepards control points. Basically I need a way to translate the coordinates on a "flat" shirt to the coordinates on this shirt.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: warp image onto another image

Post by fmw42 »

You can guess at the rotation of the person in the shirt and try to do a perspective rotation before the displacement map. See my 3Drotate script.

But getting the coordinates in the rotated shirt is very hard as you have no 3D model. So you probably need to just use trial and error to estimate where you want to put the coordinates in the shirt.

None of this is going to work well for any arbitrary oriented shirt.

Sorry I don't have any other ideas to offer. You have a very hard problem that is not easy to solve in a general way.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: warp image onto another image

Post by anthony »

Displacement maps are a general distortion method for ALL types of distortions (though without a scaling filter, only interpolation as such more that 50% compression can produce aliasing artefacts). Basically if you design your displacement map right you can distort any image to any position.

I am of course talking about two dimensional displacement maps, not a single displacement along just one X,Y vector.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
sssddd
Posts: 14
Joined: 2011-02-15T03:16:59-07:00
Authentication code: 8675308

Re: warp image onto another image

Post by sssddd »

Thanks. How do I get two-dimensional displacement maps? I can only think of using grayscale shirt for simple displacement. What are the ways to get appropriate 2d displacement maps?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: warp image onto another image

Post by anthony »

That is the hard part, and one where examples are incomplete
http://www.imagemagick.org/Usage/mapping/
(url fixed)

One method is warping two gradients one vertical (Y) and one horizontal (X) onto the shirt in the final position you want.
The gradients now generate a map of where every source pixel is to be mapped in the destination. All that is left is to invert that map to map destination pixels to source pixels.

This is what a Absolute (Distortion) Map and Relative (Displacement) Map actually represents. That is for each destination pixel, where should it get the color from the source image, (if any).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
sssddd
Posts: 14
Joined: 2011-02-15T03:16:59-07:00
Authentication code: 8675308

Re: warp image onto another image

Post by sssddd »

Sorry but the link in your post is broken. I don't really understand what you meant. Could you please explain it a bit detailed? Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: warp image onto another image

Post by fmw42 »

Post Reply