0
$\begingroup$

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?

$\endgroup$

1 Answer 1

1
$\begingroup$

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 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);
}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.