0
votes

I am taking a BufferedImage named "img" and rotating it Pi/2 radians using the Affine transform function. Here is the code for the above description:

BufferedImage img = ImageIO.read(new File(filePath));

AffineTransform tx = new AffineTransform();
tx.rotate(3.14/2, img.getWidth() / 2, img.getHeight() / 2);

AffineTransformOp op = new AffineTransformOp(tx,
AffineTransformOp.TYPE_BILINEAR);
img = op.filter(img, null);

However, within this buffered image, I have a pixel at position (x,y) that I am trying to keep track of post-rotation. I was wondering how I should follow this pixel's position? I am not sure of how the AffineTransform mathematically rotates the image, so I was hoping that someone could lend some insight into to tracking the pixel's position after rotation.

3

3 Answers

0
votes

First, determine if the AffineTransform moves the image clockwise or counterclockwise (probably counterclockwise based on unit circle angles). If it moves clockwise, point (x,y) correspond to (-y,x), otherwise if it is counter clockwise it correspond to (y, -x).

0
votes

I would use the following

AffineTransform tx = new AffineTransform();
tx.rotate(3.14/5, img.getWidth() / 2, img.getHeight() / 2);

AffineTransformOp op = new AffineTransformOp(tx,
AffineTransformOp.TYPE_BILINEAR);
BufferedImage img2 = op.filter(img, null);

g2.drawImage(img, 0, 0, null);
g2.drawImage(img2, 0, 0, null);
g2.setColor(Color.magenta);
Point2D.Double p2=(Point2D.Double)op.getPoint2D(new Point2D.Double(10, hc/2), null);
g2.fillOval(10, hc/2, 5, 5);
g2.setColor(Color.red);
g2.fillOval((int)p2.x, (int)p2.y, 5, 5);
System.out.println(p2.toString());

although its mathematical precision is doubted but can be used in a "more or less" sense. You can confirm with your own experiments, where the method in question is

  Point2D.Double p2=(Point2D.Double)op.getPoint2D(new Point2D.Double(x, y), null);
0
votes

AffineTransform has a transform() method that takes a Point2D. Try this:

Point pt = new Point(10, 10);  // replace 10, 10 with your pixel location
Point transformedPt = tx.transform(pt, null);  // this is the new post-tx location