How to convert Cartesian coordinates to "Toroidal" coordinates? 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.
 A: The essence of your transformation appears to be something like this:
$$
r\begin{pmatrix}\cos\theta\\\sin\theta\end{pmatrix}
\mapsto
(R-r)\begin{pmatrix}\cos\theta\\\sin\theta\end{pmatrix}
$$
In polar coordinates you keep the direction $\theta$ but replace the radius $r$ with an offset negated version $R-r$ which corresponds to the flipping of the image in the intermediate representation you have.
The constant $R$ is the radius of the resulting circle. It matches half the size of your square image. It also occurs as the fourth parameter in your warpPolar call.
You can avoid computing the angle by just using the direction of the input coordinates:
$$\begin{pmatrix}x\\y\end{pmatrix}\mapsto
\frac{R-r}r \begin{pmatrix}x\\y\end{pmatrix}
\quad\text{where }r=\sqrt{x^2+y^2}$$
The division by $r$ normalizes the vector to unit length, same as what you get from cosine and sine.

I want one set of equations that can convert any point in a plane …

It is unclear to me what the extension of that map would be to points outside your circle. Would you want the term to become negative, so that you get negative radius on the right hand polar coordinates and essentially flip to the other side of the image? Or are you content to just map the inside of the disk, or later ellipse?

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$

You could compute $R$ depending on the direction, but that feels like a more complicated approach. So instead I'd suggest you divide $x$ coordinates by $a=\frac12w$ and $y$ coordinates by $b=\frac12h$. This turns your input ellipse into a circle of radius $R=1$ where you can perform the above operation. Then you multiply $x$ by $a$ and $y$ by $b$ to get back to the ellipse with the same dimensions. All of this is still in a coordinate system with the origin at the center.

I want to know how to combine all the equations into one and simplify it.

I haven't done an that for you. If I find the time I might extend my answer to combine the individual steps. But I hope that avoiding the conversion to angles will already be enough of an improvement to warrant a post here.
Essentially the steps are

*

*Shift origin to center of image

*Scale to turn ellipse into unit circle

*$r\mapsto 1-r$ as discussed above

*Scale circle back to ellipse

*Shift origin back to corner

Let me first combine steps 2, 3 and 4. Using $\frac{1-r}r=\frac1r-1$ avoids repeating the expression for $r$.
$$
\begin{pmatrix}x\\y\end{pmatrix}
\overset{\text{2.}}\mapsto
\begin{pmatrix}x/a\\y/b\end{pmatrix}
\overset{\text{3.}}\mapsto
\left(\frac1{\sqrt{\frac{x^2}{a^2}+\frac{y^2}{b^2}}}-1\right)\begin{pmatrix}x/a\\y/b\end{pmatrix}
\overset{\text{4.}}\mapsto
\left(\frac1{\sqrt{\frac{x^2}{a^2}+\frac{y^2}{b^2}}}-1\right)\begin{pmatrix}x\\y\end{pmatrix}
$$
