Photoshop warp alorithm

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
Locked
mirek
Posts: 8
Joined: 2013-04-08T12:46:09-07:00
Authentication code: 6789

Photoshop warp alorithm

Post by mirek »

Hey guys,

I'm building a rendering tool which uses PSDs as source templates and IM as a rendering engine. I've succeeded reverse engineering PSD format and now get 16 control points for warp transformation of each layer in the PSD. However, when I try to apply it to source data I get very different results, mainly because I have no idea about what rendering algorithm does PS use. I'm quite sure about the numbers though.

Initially I thought this could be Shepards distortion but the order "3" polynomial transformation gives me much better results, however it's still quite far from perfect. See for yourself (PSD on the left, IM transformed rectangle on the right)

https://dl.dropbox.com/u/3204447/Screen ... 3%20PM.png

My question is: Does anyone know what transformation algorithm does PS use? Can it be replicated in IM with the same mesh? Any ideas?

Many thanks for your help.

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

Re: Photoshop warp alorithm

Post by anthony »

Yes

As you supply 16 points, and order 3 only needs 10. it does a 'best fit of the 16 points.

Note an order 4 polynomial requires a minimum of 15 points, as such in that algorithm the 'best fit' will be a lot closer.
By which I mean that the source point will more closely match the destination point.

The PS result however look like the result is a less closely fitting the distortion of of the control points you requested. perhaps it is quadratic (order 2) or even (reversed) bi-linear (order 1.5). I think it is the latter.

however as you only give a output image and not source data and image in a usable form, I can not experiment myself, without a lot of extra work.


To see exactly what distort is doing set -verbose

See IM Examples, Distorting Images
http://www.imagemagick.org/Usage/distort/#distort
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

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

Re: Photoshop warp alorithm

Post by fmw42 »

My guess is that PS is using a 2D spline warping technique.

mirek
Posts: 8
Joined: 2013-04-08T12:46:09-07:00
Authentication code: 6789

Re: Photoshop warp alorithm

Post by mirek »

Thanks! I'm going to experiment a bit further now, in the meantime...

My original CPs:

0: 0 x 0
1: 122 x 0
2: 244 x 0
3: 404 x -100.00000000000001
4: 0 x 84.33333333333333
5: 122 x 84.33333333333333
6: 244 x 84.33333333333333
7: 366 x 84.33333333333333
8: 0 x 168.66666666666666
9: 122 x 168.66666666666666
10: 244 x 168.66666666666666
11: 366 x 168.66666666666666
12: -80 x 354
13: 122 x 253
14: 244 x 253
15: 366 x 253

Test IM command:
convert warpmeh.png -gravity Center -extent 566x453 +repage +distort Polynomial '3 100,100,100,100 222,100,222,100 344,100,344,100 466,100,504,-1.4210854715202004e-14 100,184.33333333333331,100,184.33333333333331 222,184.33333333333331,222,184.33333333333331 344,184.33333333333331,344,184.33333333333331 466,184.33333333333331,466,184.33333333333331 100,268.66666666666663,100,268.66666666666663 222,268.66666666666663,222,268.66666666666663 344,268.66666666666663,344,268.66666666666663 466,268.66666666666663,466,268.66666666666663 100,353,20,454 222,353,222,353 344,353,344,353 466,353,466,353' +repage out.png

Src undistorted: https://dl.dropbox.com/u/3204447/warpmeh.png
PS render: https://dl.dropbox.com/u/3204447/ps-layers-1.png
IM output: https://dl.dropbox.com/u/3204447/out.png

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

Re: Photoshop warp alorithm

Post by anthony »

Fred is right...

If it is fixed to 16 values, no more no less, than it is much more likely to be a 2D spline patch

We do not have a spline distort in IM at this time. Nor do we have other piece wise mesh distortion, though some people have done some work doing this as a IM wrapper, they have not published there technique/result where we have seen it.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

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

Re: Photoshop warp alorithm

Post by fmw42 »

You could try my meshwarp at http://www.fmwconcepts.com/imagemagick/ ... /index.php if you are on Linux/Mac

It needs sets of triangles, but you can break the quadrilaterals into two triangles.

mirek
Posts: 8
Joined: 2013-04-08T12:46:09-07:00
Authentication code: 6789

Re: Photoshop warp alorithm

Post by mirek »

Thanks Fred, I just gave it a try and the result seems to be rather too triangulish :)

https://dl.dropbox.com/u/3204447/triangular-out.png

Do you guys have any ideas for other tools/libs that may be able to render this? I'm processing the whole job in a chain anyway and will use IM to decompose PSDs and stitch everything back together. This transformation is so far the only thing I have no idea how to do, and it's quite difficult for me as I'm pretty new to such low level image processing.

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

Re: Photoshop warp alorithm

Post by fmw42 »


mirek
Posts: 8
Joined: 2013-04-08T12:46:09-07:00
Authentication code: 6789

Re: Photoshop warp alorithm

Post by mirek »

I have some progress, however, still no results. I did some more experiments and read a bit only to learn this probably isn't just a spline mesh or a simple polynomial transformation. When I play a bit with the CPs in Photoshop I get this:

https://dl.dropboxusercontent.com/u/320 ... 8%20PM.png

Some of the CPs are far away from the splines and they easily get far out from the layer. You can see it nicely on the test render of my detected coordinates:

https://dl.dropboxusercontent.com/u/320 ... 3%20PM.png

The point in the bottom left is the one you'd expect somewhere below W and A. After studying a bit I believe this is some sort of b-spline or NURBS mesh done in 2D. I know this a bit beyond IM scope, however, you may have some ideas how to render this thing. I don't think many 3D engines store data this way, the bézier / whatever approximation is usually done on top of a triangular mesh as it is much cheaper. Only thinking aloud though.

Any other ideas? Rendering this transformation is crucial to my whole project and this is already far beyond my math skills.

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

Re: Photoshop warp alorithm

Post by fmw42 »

If it has tangent points, it could be a bezier spline mesh. But it is really hard to know what they did in terms of reverse engineering. And certainly IM does not have that. I guess with enough control points you might get the polynomial distort to come close.

mirek
Posts: 8
Joined: 2013-04-08T12:46:09-07:00
Authentication code: 6789

Re: Photoshop warp alorithm

Post by mirek »

Thanks!

If there's any image processing genius reading this who finds the problem interesting, would be able to puzzle it down and turn the resulting transformation into a single purpose program (written in language of your choice), please drop me a line at mirek@openbrand.com

Locked