Cartesian-to-polar in the pixel array
in
Programming Questions
•
2 years ago
Hey everyone,
I want to do a cartesian-to-polar transformation on an image (sequence) using the pixel array. I've created a first implementation of this idea, which solves some of the challenges: cartesian nature of the pixel array itself, low density in the outer polar rings at stepSize of 1 pixel. My solution is the inputBasedPolar. It starts from the input xy and via qr ends up at the output xy.
It works, but it's inefficient. There may be ways to improve it, but what I would really like is the outputBasedPolar. This should start from the output xy and via qr end up at the input xy. However my math is a little rusty. I would appreciate some help with 'inverting' the formulas. Or hear other suggestions on cartesian-to-polar transformations of course.
Thanks! This is what I got...
Code-in-progress
I want to do a cartesian-to-polar transformation on an image (sequence) using the pixel array. I've created a first implementation of this idea, which solves some of the challenges: cartesian nature of the pixel array itself, low density in the outer polar rings at stepSize of 1 pixel. My solution is the inputBasedPolar. It starts from the input xy and via qr ends up at the output xy.
It works, but it's inefficient. There may be ways to improve it, but what I would really like is the outputBasedPolar. This should start from the output xy and via qr end up at the input xy. However my math is a little rusty. I would appreciate some help with 'inverting' the formulas. Or hear other suggestions on cartesian-to-polar transformations of course.
Thanks! This is what I got...
Code-in-progress
- String url = "http://newtech.aurum3.com/images/polar-bear.jpg";
- PImage input, output;
- boolean original;
- void setup() {
- input = loadImage(url);
- size(input.width, input.height, P2D);
- inputBasedPolar(input, 0.25, 0.25, 0.5); // try stepSizes of 1 to see the density issue
- }
- void draw() {
- if (original) { image(input, 0, 0); }
- else { image(output, 0, 0); }
- }
- void keyPressed() {
- if (key == ' ') { original = !original; }
- }
- void inputBasedPolar(PImage input, float stepSizeX, float stepSizeY, float factor) {
- output = createImage(input.width, input.height, RGB);
- for (float y=0; y<input.height; y += stepSizeY) {
- float r = y * factor;
- for (float x=0; x<input.width; x += stepSizeX) {
- float q = map(x, output.width, 0, 0, TWO_PI);
- int polarX = int(r * cos(q)) + output.width/2;
- int polarY = int(r * sin(q)) + output.height/2;
- polarX = constrain(polarX, 0, output.width-1);
- polarY = constrain(polarY, 0, output.height-1);
- int outputIndex = polarX + polarY * output.width;
- int inputIndex = int(x) + int(y) * input.width;
- output.pixels[outputIndex] = input.pixels[inputIndex];
- }
- }
- }
- void outputBasedPolar(PImage input, float factor) {
- output = createImage(input.width, input.height, RGB);
- for (int y=0; y<output.height; y++) {
- for (int x=0; x<output.width; x++) {
- // ? HOW? ;-)
- int inputIndex = 0;
- int outputIndex = x + y * output.width;
- output.pixels[outputIndex] = input.pixels[inputIndex];
- }
- }
- }
1