Joe
Joe

Reputation: 1

[Java-Opencv]: How convert an image from Cartesian space to Polar space?

Hi guys I have to convert this image:

enter image description here

in this:

enter image description here

in Java.

This is my code:

     double Cx =original_img.width()/2;
     double Cy =original_img.height()/2;
     int rho,theta;

     for (int i=0;i<img.getHeight();i++){
         for(int j=0;j<img.getWidth();j++){


             rho = (int)(Math.sqrt(Math.pow(i-Cx,2) + Math.pow(j-Cy,2)));  
             theta = (int)(Math.atan2((j-Cy),(i-Cx)));

             int color; 

             try{

                color = img.getRGB((int)rho, (int)theta);
             }catch(Exception e){
                color = 0;
             }

             int  alpha = (color>>24) & 0xff;
             int  red = (color & 0x00ff0000) >> 16;
             int  green = (color & 0x0000ff00) >> 8;
             int  blue = color & 0x000000ff;
             int pixel = (alpha << 24) | (red << 16) | (green << 8) | blue;
             img2.setRGB(rho, theta, pixel);


             System.out.println("point: "+rho+" "+theta);
         }
     }

What's wrong? I haven't found a simple and good Log-Polar transform in java.

My steps are:

1) take an original image (original_img)

2) cycling on the rows and cols of image

3) calculate rho and theta (are the new X and Y coordinates for the new pixel, right?)

4) get color pixel at coords (rho,theta)

5) create new pixel and set at the new coords.

What miss or wrong?

Thank you.

Upvotes: 0

Views: 1488

Answers (1)

Eypros
Eypros

Reputation: 5723

Now I get it. You want to apply to pixel coordinates. Sorry.

 rho = (int)(Math.sqrt(Math.pow(i-Cx,2) + Math.pow(j-Cy,2)));  
 theta = (int)(Math.atan2((j-Cy),(i-Cx)));

Why would you want int instead of double on the above code? If not required I would suggest use double. Also the code is wrong, you are subtracting the dimension each time. Do not do this:

 rho = Math.sqrt(Math.pow(i,2) + Math.pow(j,2));  
 theta = Math.atan2((j),(i));

That looks fine to me. But why you want to convert to polar anyway?

P.S. The above code has noting to do with Opencv of course.

Edit: If I am interpreting correctly the algorithm you the Cartesian coordinates should be in the center of the image so use your code:

I cannot tell you about the rotation part though but from your statement get color pixel at coords (rho,theta) I am guessing that you don't have to rotate the image. The effect does not require this.

Upvotes: 1

Related Questions