Coordinate Rotation
-
28-09-2022 - |
Question
I need to rotate the coordinates of the pixels of an image (or a matrix). My aim is to calculate, for each point, where it will be positioned after a rotation, and vice versa. I'm currently using this procedure:
private void setRot(int i) {
this.rot = -i / (180.0 / Math.PI);
this.coseno = Math.cos(rot);
this.seno = Math.sin(rot);
}
private Point ruota(int x, int y){
int centerY=h/2;
int centerX=w/2;
int j = (int) (coseno * (x-centerX) - seno * (y-centerY) + centerX);
int i = (int) (seno * (x-centerX) + coseno * (y-centerY) + centerY);
return new Point(j,i);
}
However, if you apply a test, using the method for each pixel, the result is not rotated about the center.
private void esegui() {
for(int i=0;i<w;i++)
for(int j=0;j<h;j++){
Point p = ruota(i,j); //ip : origina image | ip2 destination image
ip2.putPixel(p.x, p.y, ip.getPixel(i, j));
}
nu.setProcessor(ip); // destination image
nu.repaintWindow();
}
Would someone help me make a proper rotation in one direction and in the other?
this is my Test Class:
private int getH(){ //height of destination image
return (int) (this.h*Math.abs(coseno) + this.w*Math.abs(seno));
}
private int getW(){
return (int) (this.plus.getHeight()*Math.abs(seno) + this.plus.getWidth()*Math.abs(coseno));
}
public Test(){ // class test
IJ.open();
this.plus = IJ.getImage();
ip = plus.getProcessor();
this.h = this.plus.getHeight(); // height of start image
this.w = this.plus.getWidth();
setRot(14); // rotation of 14°
dHeight = this.getH(); // dimension of the target image
dWidth = this.getW();
System.out.println("h "+h+" gh " + getH());
System.out.println("w "+w+" gw " + getW());
// create an image target
this.nu = NewImage.createRGBImage("rotate", getW(), getH(), 1, NewImage.FILL_BLACK);
ip2 = nu.getProcessor();
esegui();
nu.setProcessor(ip2);
nu.show();
}
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow