<p>You'd be doing a change of coordinates from polar to Cartesian; at output point <span class="math-container">$(x,y)$</span> draw polar coordinate <span class="math-container">$(\theta = 2 \pi x, r = y)$</span>. The coordinates in a unit circle drawn in the input image are <span class="math-container">$(r \cos \theta, r \sin \theta)$</span>. In OpenGL this'd be drawn from a sampler, so the input image <span class="math-container">$u$</span> and <span class="math-container">$v$</span> coordinates would be in <span class="math-container">$[0,1]$</span> and you'd have to sample instead from a circle of radius 0.5 centered at <span class="math-container">$(0.5,0.5)$</span>, resulting in <span class="math-container">$\mathrm{out}(x,y) = \mathrm{in}(0.5 + 0.5 * r * \cos \theta, 0.5 + 0.5 * r * \sin \theta)$</span>.</p>
<p>Here's a fragment shader function I tested at <a href="https://www.shadertoy.com" rel="nofollow noreferrer">Shadertoy</a> that performs this transformation:</p>
<pre><code>void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    // Normalized pixel coordinates (from 0 to 1)
    vec2 xy = fragCoord/iResolution.xy;

    // map x to theta and y to r on unit circle
    float theta = 2.0 * 3.14159265368979 * xy.x;
    float r = xy.y;
    
    // map unit circle to circle of radius 0.5 centered at (0.5,0.5)
    vec2 uv = vec2(0.5 + 0.5*r*cos(theta), 0.5 + 0.5*r*sin(theta));

    // texture lookup
    float v = texture(iChannel0,uv).x;

    // Output to screen
    fragColor = vec4(v,v,v,1.0);
}
</code></pre>

CC BY-SA 4.0
2022-05-03T10:01:50.657Z
2022-05-03T10:01:50.657Z
gilgamec
<p>Is it possible to convert Polar to Cartesian coordinates and map result to an image?</p>
<p>I want to get the output same as the <code>distort depolar 0</code> from image magick.</p>
<p><a href="https://legacy.imagemagick.org/Usage/distorts/#depolar" rel="nofollow noreferrer">https://legacy.imagemagick.org/Usage/distorts/#depolar</a></p>
<p>Is it possible to get the same output using opengl?</p>

<opengl><image-processing><raster-image>
Is it possible to convert Polar to Cartesian coordinates and map result to an image using opengl?
code0x00
# Is it possible to convert Polar to Cartesian coordinates and map result to an image using opengl?

(Asked by: username_0 on 2022-05-02)

Is it possible to convert Polar to Cartesian coordinates and map result to an image?

I want to get the output same as the `distort depolar 0` from image magick.

<https://legacy.imagemagick.org/Usage/distorts/#depolar>

Is it possible to get the same output using opengl?

**Tags:** opengl, image-processing, raster-image


---

## 1 answers

### Answer by username_1 on 2022-05-03. Score: 1

You'd be doing a change of coordinates from polar to Cartesian; at output point $(x,y)$ draw polar coordinate $(\theta = 2 \pi x, r = y)$. The coordinates in a unit circle drawn in the input image are $(r \cos \theta, r \sin \theta)$. In OpenGL this'd be drawn from a sampler, so the input image $u$ and $v$ coordinates would be in $[0,1]$ and you'd have to sample instead from a circle of radius 0.5 centered at $(0.5,0.5)$, resulting in $\mathrm{out}(x,y) = \mathrm{in}(0.5 + 0.5 \* r \* \cos \theta, 0.5 + 0.5 \* r \* \sin \theta)$.

Here's a fragment shader function I tested at [Shadertoy](https://www.shadertoy.com) that performs this transformation:

```
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    // Normalized pixel coordinates (from 0 to 1)
    vec2 xy = fragCoord/iResolution.xy;

    // map x to theta and y to r on unit circle
    float theta = 2.0 * 3.14159265368979 * xy.x;
    float r = xy.y;
    
    // map unit circle to circle of radius 0.5 centered at (0.5,0.5)
    vec2 uv = vec2(0.5 + 0.5*r*cos(theta), 0.5 + 0.5*r*sin(theta));

    // texture lookup
    float v = texture(iChannel0,uv).x;

    // Output to screen
    fragColor = vec4(v,v,v,1.0);
}

```


---

