Basically I want to convert this:
To this:
I used NumPy and cv2 to generate the images.
The following:
import cv2
import numpy as np
img = np.ones((1080,1080,3), dtype=np.uint8)*255
for i in range(12):
img[i*45:(i+1)*45, :, 0] = round(i*64/3)
img[1079-(i+1)*45:1079-i*45, :, 0] = round(i*64/3)
minv = max(0, i*45-2)
img[minv:i*45+2, :] = (0, 0, 0)
img[:, minv:i*45+2] = (0, 0, 0)
maxv = min(1079, i*45+542)
img[i*45+538:maxv, :] = (0, 0, 0)
img[:, i*45+538:maxv] = (0, 0, 0)
img[1078:1080, :] = (0, 0, 0)
img[:, 1078:1080] = (0, 0, 0)
img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR_FULL)
Creates the test image.
I then converted the test image from "polar coordinates" to "Cartesian coordinates".
polar_img = cv2.warpPolar(img, (1080, 1080), (540, 540), 540, cv2.WARP_POLAR_LINEAR+cv2.INTER_CUBIC)
To get this image:
And then flipped the image left to right and converted "Cartesian coordinates" to "polar coordinates" to get the toroidal shape.
I am not a native English speaker and I don't know what is the term for that shape. But anyway the shape reminds me of how the magnetic field lines of a bar magnet is typically depicted, and I imagine this is what the magnetic field of a bar magnet with zero dimensions would be.
Google searching "what is the shape of the magnetic field of bar magnets called" (without quotes) yields few relevant results, the only relevant result I found is "toroidal", and since bar magnets have two poles I guess that could be called "bipolar" but I don't know if it makes sense.
Anyways I would like to know the mathematical equations to turn any rectangular image to "toroidal", the above code uses a rather mathematically redundant process to convert the images and it only works on square images, I want one set of equations that can convert any point in a plane from Cartesian coordinate to "toroidal" coordinate, so that if the width of the plane is w and its height h, the boundary of the resultant image would be an ellipse whose horizontal axis is w and vertical axis h, and it will only be a circle if w = h.
Given the shape of the image $(w, h)$, the center of transformation should be $(\frac{w}{2}, \frac{h}{2})$, and since y-axis of images is downwards instead of upwards, we need to flip its sign, so the coordinate for point $(x, y)$ after translation and flipping is:
$(x_1, y_1) = (x - \frac{w}{2}, \frac{h}{2} - y)$
To convert from polar to Cartesian:
$(x, y) = (r \cdot cos(\theta), r \cdot sin(\theta))$
Interpret the Cartesian coordinate as polar coordinate and plug in the values:
$(x_2, y_2) = (-x_1 \cdot cos(y_1), x_1 \cdot sin(y_1))$
(The x-axis needs to be flipped)
Interpret $(x_2, y_2)$ as Cartesian and convert it to polar:
$$ \left\{ \begin{aligned} &r = \sqrt{x_2^2 + y_2^2}, \\ &\theta = \begin{cases} arctan(\frac{y_2}{x_2}), &\text{if $x_2 > 0$} \\ arctan(\frac{y_2}{x_2}) + \pi, &\text{if $x_2 < 0$} \\ \frac{\pi}{2}, &\text{if $x_2 = 0 \ and \ y_2 >= 0$} \\ -\frac{\pi}{2} &\text{if $x_2 = 0 \ and \ y_2 < 0$} \end{cases} \end{aligned} \right. $$
And finally translate and flip the coordinate back to image coordinates:
$(x_3, y_3) = (x_2 + \frac{w}{2}, \frac{h}{2} - y_2)$
My mathematics is not good enough to simplify the equations and I want to know how to combine all the equations into one and simplify it.
And I want to generalize it, the above process works only if the input image is a perfect square, that is, the aspect ratio of the image is 1:1, if it is any other number, the intermediate polar coordinate is circular, but it should be elliptical, so the final image would not contain all information in the original image.
I want the intermediate polar coordinate system to be an ellipse whose horizontal axis is the width of the image and whose vertical axis is the height of the image, in other words, the circular polar coordinate system needs to be scaled.
I recognize the equation to convert from polar to Cartesian as the parametric equation of circles, and I know the parametric equation of ellipses is:
$(x, y) = (a \cdot cos(\theta), b \cdot sin(\theta))$
So I think the equation to convert from elliptical polar coordinates to Cartesian should be:
$(x, y) = (\frac{r \cdot cos(\theta) \cdot a} {R}, \frac{r \cdot sin(\theta) \cdot b} {R})$
(R is the semi-major axis, in other words the longer one of a and b, a is w/2 and b is h/2 regardless of the orientation of the image, r is the distance between the point and center of polar coordinate system)
But I don't know for sure if it is true.
And I have no idea how to convert from Cartesian to elliptical polar coordinates.
Edit
Examples created using PhotoShop CS6 to better illustrate what I intend to do:
Input:
Output:
I need to programmatically apply the transformation process to all frames of some videos.