Mutate to Polar coordinates #2724
Answered
by
JimBobSquarePants
LuisAlfredo92
asked this question in
Ideas
-
It'd be nice to have a filter in |
Beta Was this translation helpful? Give feedback.
Answered by
JimBobSquarePants
Apr 23, 2024
Replies: 2 comments 3 replies
-
What's generating that output in your sample? |
Beta Was this translation helpful? Give feedback.
1 reply
-
The GIMP plugin has a bunch of options which I haven't worked out but here's a basic implementation. using (Image<Rgba32> image = Image.Load<Rgba32>(@"C:\Users\james\Downloads\polar-in.jpg")) // Load your image
{
int width = image.Width;
int height = image.Height;
var center = new PointF(width / 2f, height / 2f);
float maxRadius = Math.Min(center.X, center.Y);
using (Image<Rgba32> polarImage = new(width, height))
{
polarImage.Mutate(ctx =>
{
ctx.ProcessPixelRowsAsVector4((pixelRow, offset) =>
{
for (int x = 0; x < pixelRow.Length; x++)
{
// Map to polar coordinates
float dx = x - center.X;
float dy = offset.Y - center.Y;
float radius = MathF.Sqrt(dx * dx + dy * dy) / maxRadius;
if (radius <= 1f) // Only process pixels within the circle
{
float theta = MathF.Atan2(dy, dx) - MathF.PI / 2; // Subtract π/2 for 90-degree clockwise rotation
// Normalize theta to range from 0 to 1
theta = (theta + MathF.PI) / (2 * MathF.PI);
while (theta < 0)
{
theta += 1; // Wrap around if negative
}
// Map radius and theta back to source coordinates
int sourceX = (int)(theta * width);
int sourceY = (int)(radius * height);
sourceX = Math.Clamp(sourceX, 0, width - 1);
sourceY = Math.Clamp(sourceY, 0, height - 1);
pixelRow[x] = image[sourceX, sourceY].ToScaledVector4();
}
}
}, PixelConversionModifiers.Scale);
});
polarImage.Save(@"C:\Users\james\Downloads\polar-out.jpg"); // Save the transformed image
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
LuisAlfredo92
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The GIMP plugin has a bunch of options which I haven't worked out but here's a basic implementation.