Distortion: circle (or any shape) to square

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
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Distortion: circle (or any shape) to square

Post by dognose »

So, I'm trying to distort a shape, say a triangle or circle or any shape really into a square or rectangle... sorta like a rolling pin flattening dough into a square pizza.
(and for the moment, I'm only interested in stretching the pixels horizontally.)

For the circle, the top and bottom of the circle would get stretched horizontally the most..and the middle would stay about the same.

I've seen the 4 point -distort Perspective method, but that won't work for any shape.

Any ideas on how I would do this? I'm guessing it's an fx: operation..
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Distortion: circle (or any shape) to square

Post by anthony »

More likely a ploynomial, though there are problems with it, and no good examples.

Polynomial curve fits a ploynomial distortion equation to multiple points, though requires and extra 'order' parameter, which determines the minimum number of control points needed. If you don't have enough control points IM will generate an error telling you the number of control point pairs needed.

http://www.imagemagick.org/Usage/distorts/#polynomial
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Re: Distortion: circle (or any shape) to square

Post by dognose »

Hmm. Well, that's not working too well... it's not providing near enough of an exact edge... it just seems to zoom in on the image a bit

convert circle.jpg -matte -virtual-pixel black -distort Polynomial '1.5 285,6 0,6 277,6 0,6 270,7 0,7 263,7 0,7 255,13 0,13 246,16 0,16 239,17 0,17 229,20 0,20 220,22 0,22 207,25 0,25 200,25 0,25 193,30 0,30 187,32 0,32 181,39 0,39 175,42 0,42 169,47 0,47 163,52 0,52 157,57 0,57 150,62 0,62 143,68 0,68 137,74 0,74 123,86 0,86 117,91 0,91 110,97 0,97 103,104 0,104 98,112 0,112 93,119 0,119 90,125 0,125 88,132 0,132 82,144 0,144 79,152 0,152 78,159 0,159 76,167 0,167 75,179 0,179 74,185 0,185 74,195 0,195 74,202 0,202 74,213 0,213 74,219 0,219 74,226 0,226 74,236 0,236 76,245 0,245 77,252 0,252 78,265 0,265 81,272 0,272 94,297 0,297 96,305 0,305 100,314 0,314 104,324 0,324 107,332 0,332 114,344 0,344 120,352 0,352 130,365 0,365 142,382 0,382 150,391 0,391 168,407 0,407 174,414 0,414 186,423 0,423 196,428 0,428 212,432 0,432 218,435 0,435 232,442 0,442 237,448 0,448 246,456 0,456 600,451 600,451 600,0 600,0 0,451' square.jpg

Secondly, I don't like having to produce the polynomial... if it could just use the edge of the shape (based on transparency)
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Re: Distortion: circle (or any shape) to square

Post by dognose »

I wrote this pseudo code that shows what I'm thinking I want done:


split image into horizontal lines

foreach line {
autocrop line (based on transparency)
scale line to width of original image
}
join lines
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Distortion: circle (or any shape) to square

Post by fmw42 »

Look up conformal mappings.

see example circle to square at http://www.flickr.com/photos/sbprzd/362529354/
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Re: Distortion: circle (or any shape) to square

Post by dognose »

Interesting .. a search for that yields map projection software.. which might be just what I need..

http://www.users.globalnet.co.uk/~arcus ... uropa.html
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Distortion: circle (or any shape) to square

Post by fmw42 »

Many map projections are conformal maps as well.

see http://www.progonos.com/furuti/MapProj/ ... jConf.html

The problem you have with any shape to square is very hard to solve. Each shape needs a different transformation (if solvable at all) and each such transformation is quite complex. The usual way it is solved is mapping the the corners of a grid using the conformal equations and then filling in each grid quadrilateral using a bilinear or perspective transformation.

Anthony has such on his list, but has not had time to deal with the piecewise bilinear or perspective issue, yet.

P.S. I have an old algorithm that uses distortion maps to speed it up for warping a square into a circle, but have not scripted that into IM yet. (F. Weinhaus and M. Walterman, A Flexible Approach to Image Warping, Proceedings SPIE, Vol. 1244, 108 - 122, February, 1990). One day perhaps I will implement it.

Going the other way is harder.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Distortion: circle (or any shape) to square

Post by anthony »

fmw42 wrote:Anthony has such on his list, but has not had time to deal with the piecewise bilinear or perspective issue, yet.
The major problem with the implemention of a piece wise distortion is that I need to re-write the main distortion loop, to call functions to do coordinate mapping, rather than just calculate directory.
That is call an affine function with the appropriate distortion coordinates to map destination coordinates to source coordinates. That is you need to be able to switch between different sets of distortion coefficents.

That is need as each 'piece' needs a separate set of distortion coefficients, as well as 'bounds' information. And thus a remapping of the current use of a simple floating point array of coefficients to a structure of some kind holding more complex information. Something that is more object oriented than is easilly 'C' coded.

Input of the distortion also needs some modification, grid input for examples is relationally straight forward, but a triangular mesh needs two sections (mapping the relation of triangles to coodinates), requiring some consistent structure in the input.

Basically the next step needs non-simple changes in internal distortion processing, data handling and mapping. Something that is almost a big as the original effort to add a generalised distortion algorithm.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Re: Distortion: circle (or any shape) to square

Post by dognose »

Well, here's what I've done so far with my pseudo code..

circle to square stretch...
Image

somewhat useful, though you can clearly see the pixel jitteryness.

I was using -trim .. which doesn't seem to like 1 pixel wide images.. otherwise pretty simple.

only thing I haven't been able to do is join the lines back together.. montage?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Distortion: circle (or any shape) to square

Post by anthony »

Looks like it needs some 'filtered' resampling of some kind!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Re: Distortion: circle (or any shape) to square

Post by dognose »

Hmm. The more I look at it, the more I need an improved trim.. it doesn't seem to detect the edge well enough.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Distortion: circle (or any shape) to square

Post by anthony »

Improve Trim In what way, and wouldn't this a separate topic?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply