polar to rectangular cordinates

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?".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: polar to rectangular cordinates

Post by fmw42 »

Anthony has built the arc distortion into -distort, to go from rectangular to polar, but he needs to build the reverse. (Hint :wink: )

However, I have a script, defish, that will convert the fisheye into a normal perspective view (not panoramic, though). See http://www.fmwconcepts.com/imagemagick/index.html
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: polar to rectangular cordinates

Post by anthony »

Basically we have not added a built-in distortion to do this.
A polar to cartesian distortion is planned, it just has not happened yet.

However you could probably modify Fred's fish eye script to do it using the slow
FX DIY solution.

The technique is called Reverse or Texture Mapping see Im examples
http://www.imagemagick.org/Usage/distorts/#summary

In your case for each coordinate in the destination image the i,j position is converted to a an angle and radius, then to a u,v coordinate in the source image, and the color is looked up.

The only thing that a -distort built-in would do is do it faster, and with area resampling to try to improved the resulting image (at this time area resampling is a little blurry, but that will be fixed).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: polar to rectangular cordinates

Post by fmw42 »

I took the 360 degree photo from http://www.0-360.com/elements.asp and trimmed it to the circle area

Image

Then I followed the instructions and used Photoshop to generate the following polar to rectangular image:

Image

Then I implemented a formula from first principles using IM -fx to try to match that of Photoshop.

This implements:

y=r*cos(theta)-yc
x=r*sin(theta)-xc

where x and y are reversed due to the fact that y increases downward and theta increases from 0 (both + and -) away from the y axis rather than the x axis. Also the result has r going downward and theta horizontally with the middle of the source image at the top center of the output.

Note that this is actually a rectangular to polar transformation as the input is rectangular (x,y) and the output is polar (r,theta) when implemented as an inverse transformation so that one can sequence through the output and find the location in the input, get that color and transfer to the output. Seems like Photoshop has their notation reversed.

Nevertheless, this works fairly closely (but not exactly) to reproduce the results from photoshop for their polar to rectangular option, but only for a square image. I am not sure what tweaking Photoshop is doing otherwise for normalizing the radius. When a square image is used, r=j/2 works likely due to the radius being half the height of the image. Here is the IM function using -fx or as Anthony calls it, a DIY implementation for Do It Yourself.

convert circle_trim.png -monitor \
-fx "yy=(j/2)*cos((2*pi)*(i/(w-1))-(pi))+h/2; xx=(j/2)*sin((2*pi)*(i/(w-1))-(pi))+w/2; u.p{xx,yy}" \
circle_trim_im_polar2rect.png


Here is the image:

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

Re: polar to rectangular cordinates

Post by anthony »

Fred - your 'center' looks a little off center! But it is a very good example, thank you.
The very first step in any 'new' distortion is usually generating a '-fx' equivalent.

Note that you can generate an output image that is a different size to the input.
The trick is to generate a 'output canvas' and make that the first image for -fx
to loop over (i,j) coordinates. But then convert those into coordinates for the second 'v'
source image.

When the next IM (v6.2.4-6) comes out you can use -verbose to see the equivelent options and internal coefficients IM uses for the Arc distort, to see this type of thing put into good use. Arc always generates a new image that is a different size to the original as rarely does the original image size make sense. This distortion will probably be the same.

Of course you will need to adjust the offsets and arguments appropriately.

The biggest problem I found it attempting to preserve the mid-arc aspect ratio of the image as much as possible. That may be a tricky matter in this case, and may need to be one of the 'user input arguments' (with a reasonable internal default).

Suggested input arguments for a -distort version of this will be welcome at this point, as I am currently doing Distort Upgrades, now is a good time for me to add new distortions.
Look at the various optional arguments for 'arc' for an example.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
GeorgeStone

Re: polar to rectangular cordinates

Post by GeorgeStone »

I too am looking for the exact same thing as the OP.
A replacement for that software.

I eventually found imagemagick but -360 didn't work in the arc param ;)

Just signed up to show you that there is some demand for this feature.

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

Re: polar to rectangular cordinates

Post by fmw42 »

I am currently working on a script to do this (polar to rect and rect to polar). Should have it done in a day or so.

Anthony and I am working on new distorts. Hopefully he can program this once I get the equations pinned down in my script and do some testing for proof of concept.

But feel free to use the -fx equation I supplied above. It works. I am not sure what exactly PS is doing, but it does not appear to be correct in the nominal sense of the mathematics.
GeorgeStone

Re: polar to rectangular cordinates

Post by GeorgeStone »

Great work!

But it's too slow for the implementation I want it for. Although I could do them in batch I guess at the end of each day... Hmmm.
Will your new one be faster?

I might end up hiring someone to program for me... I looked a little into it my self and the math is way over my head.
GeorgeStone

Re: polar to rectangular cordinates

Post by GeorgeStone »

Hmm, it just finished running and it didn't work :(
It just kind of put half on one side, and half on the other.

Like this:

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

Re: polar to rectangular cordinates

Post by anthony »

I'll see if I can get a Polar to Cartesian version in -distort over the weekend. At least as good as the -fx versions.

suggestiong for the distortion name?
Cartesian, Depolar, UnPolar?


Later I'll try to get the scaling vectors worked out for area-resampling for improved (non -filter point output).

Side Note: I am getting behind on the documentation for the other additions, such as Barrel. the other 'Polynomial' is not finalized.

Things are happening NOW! so stay tuned!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: polar to rectangular cordinates

Post by fmw42 »

GeorgeStone wrote:
Will your new one be faster?
No, the script has the same limitations as running it in command line. The only advantage is that I take care of all the parameter input to make it easier.
Hmm, it just finished running and it didn't work
It just kind of put half on one side, and half on the other.
What environment are you using and what version of IM. This is a Unix command line. Perhaps if you are running Windows, there may be some syntax changes needed? What was your command line and your input image.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: polar to rectangular cordinates

Post by anthony »

The -distort version will very very fast my comparision, but will have to wait for a IM release cycle when I have it added.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: polar to rectangular cordinates

Post by fmw42 »

I have now uploaded my script called polar. It does rect to polar and polar to rect. The names have to do with the mathematical description of the input and output images and not the way the images look, which one might think of as being reversed. Thus my names are just the opposite from what Photoshop calls them. Sorry!

See http://www.fmwconcepts.com/imagemagick/index.html


Anthony can reverse them if he wants.
GeorgeStone

Re: polar to rectangular cordinates

Post by GeorgeStone »

Ah, I'm using the windows version since I don't have access to a linux install at the moment.
I guess that's the problem.

I think DePolar would be a good name for it.

I can't believe how fast development happens over here! A 4 day turnaround!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: polar to rectangular cordinates

Post by anthony »

For Fred, he'll get things up and running fast.

For me you are lucky I am in a Distort programming cycle. Last one was last year when I first added distort.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
GeorgeStone

Re: polar to rectangular cordinates

Post by GeorgeStone »

It is a much appreciated feature.

Thanks.
Post Reply