[SOLVED] Image displace

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
suciuvlad
Posts: 9
Joined: 2010-08-25T03:48:48-07:00
Authentication code: 8675308

[SOLVED] Image displace

Post by suciuvlad » 2010-08-25T04:03:15-07:00

Hello everyone,

I have 2 images. One is bigger and one is smaller. I want to blend the smaller image into the bigger one giving the impression that its actually on it. In photoshop this can be done with the displace filter.
Example: http://dl.dropbox.com/u/5423830/eg.jpg (from zazzle .com)
So, i want the pixels to move based on the brightness. Can anyone tell me how to achive that either using the unix command line or as php module.

Thanks in advance,
Vlad.
Last edited by suciuvlad on 2010-08-26T23:06:23-07:00, edited 1 time in total.

User avatar
fmw42
Posts: 23880
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Image displace

Post by fmw42 » 2010-08-25T08:43:12-07:00

Post a link to your two input images and what you want it to look like afterwards. Or see http://www.imagemagick.org/Usage/mappin ... ement_maps

suciuvlad
Posts: 9
Joined: 2010-08-25T03:48:48-07:00
Authentication code: 8675308

Re: Image displace

Post by suciuvlad » 2010-08-25T09:42:19-07:00

Thanks for your reply.
Image 1:
http://dl.dropbox.com/u/5423830/design.jpg
Image 2:
http://dl.dropbox.com/u/5423830/product.jpg

Here is how it would look displaced(the desired effect):
http://dl.dropbox.com/u/5423830/displaced.jpg

...and here how it looks if i just join them
http://dl.dropbox.com/u/5423830/not_displaced.jpg

User avatar
fmw42
Posts: 23880
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Image displace

Post by fmw42 » 2010-08-25T11:13:27-07:00

I will look into it as I get time in the next day or two. I will have to see what PS is doing.

But you can do something very simple, by distorting the produce.jpg image using -distort barrel, for example and make it look somewhat like it looks on the shirt. Then just composite it over the shirt. IM -distort barrel was a redesign of my script pincushion that has quite a few examples that you can see from my script web site linked below. Note however, that the coefficient ordering is reversed between my script and the IM function and the IM function has fewer coefficients. But at least you will see what a simple distortion can do.

I just don't know (and may not know) exactly what PS is doing to get the displacement coordinates, unless it is just using the graylevel of the shirt for offsets. But I don't know if this is really what they do, nor just yet how to do that in IM with -displace, though I suspect something similar can be done. It has been a while since I last used IM -displace.

You can experiment. Crop out a section of the shirt that is the same size as the product.jpg and in about the area you want. Then use the IM displace to distort it with that section of the image. Then overlay the result back in the area you want.

see

http://www.imagemagick.org/Usage/distorts/#barrel
http://www.imagemagick.org/Usage/mappin ... e_displace
http://www.imagemagick.org/script/compose.php

P.S. I looked quickly at my PS CS (kind of old) and see only Filter->Distort->Displace

But this requires a displacement map. So where did you get the displacement map in your example. Or was it done otherwise. In any case what are your exact PS steps that you used to make your displaced example?
Last edited by fmw42 on 2010-08-25T19:24:36-07:00, edited 1 time in total.

User avatar
fmw42
Posts: 23880
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Image displace

Post by fmw42 » 2010-08-25T17:42:10-07:00

See my post above. But here is a rough go at it.

1) cropped a section of the shirt the same size as the picture, then converted to grayscale, blurred it to smooth out variations, then stretched it to full dynamic range

Image


convert shirt_design.jpg[192x144+90+105] -colorspace gray \
-blur 20x65000 -auto-level shirt_design_crop_b20_al.png

Image

2) Used the cropped, grayscale image as the displacement map to warp the picture. Note the negative arguments. I could have negated the displacement map instead. Note also that the stretch to full dynamic range is needed so that some parts get positive shifts and some parts get negative shifts.

Image

convert shirt_product.jpg shirt_design_crop_b20_al.png -alpha set \
-virtual-pixel transparent -compose displace \
-set option:compose:args -5x-5 -composite shirt_displace_m5b.png

Image


3) Composited the result over the shirt
convert shirt_design.jpg shirt_displace_m5b.png -geometry +90+105 \
-compose over -composite shirt_product_displace_m5.png

Image


It does not reproduce your example, because I don't have your exact displacement map.

But it gives you the idea how to go about it. You can play with the amount of smoothing or some other displacement map and the amount of displacement to apply and its direction (sign).


Here is another approach.

1) Get four grayscale colors from the corners of the cropped grayscale shirt image. Though it does not have to be the corners and can be more than 4. Use those points and colors to interpolate a displacement map using -sparse-color bilinear (but you could try shepards as well). see http://www.imagemagick.org/Usage/canvas/#sparse_summery

color1=`convert shirt_design_crop_b20_al.png -format "%[pixel:s.p{0,0}]" info:`
color2=`convert shirt_design_crop_b20_al.png -format "%[pixel:s.p{191,0}]" info:`
color3=`convert shirt_design_crop_b20_al.png -format "%[pixel:s.p{191,143}]" info:`
color4=`convert shirt_design_crop_b20_al.png -format "%[pixel:s.p{0,143}]" info:`
convert -size 192x144 xc: -sparse-color Bilinear \
"0,0 $color1 191,0 $color2 191,143 $color3 0,143 $color4" -auto-level shirt_displace_map.png

Image

2) apply the displacement map to the picture as before
convert shirt_product.jpg shirt_displace_map.png -alpha set \
-virtual-pixel transparent -compose displace \
-set option:compose:args -10x-10 -composite shirt_displace_map_m10.png

Image

3) composite the displaced image over the shirt.
convert shirt_design.jpg shirt_displace_map_m10.png -geometry +90+105 \
-compose over -composite shirt_product_displace_map_m10.png


Image


Here is another easier approach:

1) distort the picture using -distort arc

convert shirt_product.jpg -alpha set -virtual-pixel transparent \
-distort Arc 10 -resize 95% shirt_product_arc10.png

Image

2) composite over the shirt
convert shirt_design.jpg shirt_product_arc10.png -geometry +90+105 \
-compose over -composite shirt_product_arc10_compose.png


Image


Lots of ways to approach this problem!
Last edited by fmw42 on 2011-03-04T20:50:39-07:00, edited 2 times in total.

User avatar
anthony
Posts: 8871
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Image displace

Post by anthony » 2010-08-26T19:38:53-07:00

You may also like to 'dirty' the image slightly before overlaying.

That is add shading highlights and darken to make it more 'realistic' and better match the
surrounding environment. You should be able to get a shade map, directly from the original
'white t-shirt' image and adjusting appropriately.

I talk about this in IM examples, image displacements, but I have not done a complete job in the examples, mostly due to lack of examples.
http://www.imagemagick.org/Usage/mapping/#spherical

NOTE this is for 'absolute' distortion maps, rather than 'relative' displacement maps,
but the idea is essentially the same. and the shading part will definably be the same.

If you can provide the displacement and shading map, then I can add a practical the example to IM examples.

Please note you may need two displacement maps to do a good job. One X displacement one Y displacement. One way of generating these is using 'Shepards Distortion', and smoothing the result slightly.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

suciuvlad
Posts: 9
Joined: 2010-08-25T03:48:48-07:00
Authentication code: 8675308

Re: [SOLVED] Image displace

Post by suciuvlad » 2010-08-26T23:09:55-07:00

Thanks guys. You've been great.

@fmw42: I'll use the first solution but instead of resizing the tshirt to match the size of the product i will use as displacement map the part of the design where the product will be. That way the highlights and shadows will be correct. I can't use the last solution(with the arc) because the model will be photographed from multiple angles.

Regards.

User avatar
fmw42
Posts: 23880
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [SOLVED] Image displace

Post by fmw42 » 2010-08-27T09:13:24-07:00

suciuvlad wrote:Thanks guys. You've been great.

@fmw42: I'll use the first solution but instead of resizing the tshirt to match the size of the product i will use as displacement map the part of the design where the product will be. That way the highlights and shadows will be correct. I can't use the last solution(with the arc) because the model will be photographed from multiple angles.

Regards.

In the first two methods, I did not resize the tshirt to the size of the product. I cropped out a section of the tshirt of the size of the product image and put the result back in the same place as the cropped section of the tshirt.

You still have not identified how you processed this in PS, nor where you got the displacement map to use in PS. So we are all still in the dark about that and cannot even try to reproduce that result.

Post Reply