Questions tagged [affinetransform]

An affine transform is a special 3x3 matrix used to apply translation, rotation, shearing or skew, and scaling to coordinate systems in two dimensional graphic contexts.

An affine transform is a special 3x3 matrix used to apply translation, rotation, shearing or skew, and scaling to coordinate systems in two dimensional graphic contexts. Parallel lines will remain parallel so perspective cannot be applied. The angle between lines that are not parallel may be changed by skew operations.

a c x
b d y
0 0 1

The bottom row of an affine transform matrix is always 0 0 1, so an affine transform is described by only six values. Transforming a point is the basis of all operations with affine transforms.

new.x = old.x * at.a + old.y * at.c + at.x
new.y = old.x * at.b + old.y * at.d + at.y

The identity transform will transform a point to itself.

1 0 0
0 1 0
0 0 1

The x and y values (often referred to as tx and ty) will translate a point. The a b c d values apply rotation, shear and scale to a point. The following affine transform will rotate a point by r radians around the origin of the coordinate space.

 cos(r)  sin(r)  0
-sin(r)  cos(r)  0
 0       0       1

To apply shear or skew by rx and ry radians use the following matrix. With a positive rx value vertical lines will appear to lean to the left and with a positive ry value horizontal lines will appear to slide down towards the right.

 1       tan(rx) 0
 tan(ry) 1       0
 0       0       1

Scaling is applied more directly. To scale by sx and sy use the following matrix. Using a negative scale will flip or reflect the coordinate system around an axis. For example, using -1 for sy in this matrix will flip a coordinate system upside down.

sx 0  0
0  sy 0
0  0  1

To apply several effects at once, several affine transform matrices can be concatenated together. The order of concatenation matters. The order of least surprise is translate, rotate, skew then scale. That is also how the transform will be perceived, regardless of the original order of operations.

In order to rotate around a point other than the origin, a rotate operation is bracketed by translate operations. The second translate will be in the direction of the rotate, since the translate will be concatenated after the rotate.

translate(-x,-y) rotate(r) translate(x,y)

In contexts where the transform is related to a specific visual element, such as css or CALayer, there can be a transform origin that implicitly translates the affine transform. Be aware of this if you must translate between coordinate spaces.

To concatenate two affine transforms u and v into a new transform do the following.

new.a = u.a * v.a + u.c * v.b
new.b = u.b * v.a + u.d * v.b
new.c = u.a * v.c + u.c * v.d
new.d = u.b * v.c + u.d * v.d
new.x = u.a * v.x + u.c * v.y + u.x
new.y = u.b * v.x + u.d * v.y + u.y

The inverse of a matrix can be calculated if the determinant is not zero. Transforming a point by an affine transform then by the inverse of the affine transform will restore the original point.

determinant = a*d - b*c
inverse.a = d/determinant
inverse.b = -b/determinant
inverse.c = -c/determinant
inverse.d = a/determinant
inverse.x = (c*y - d*x)/determinant
inverse.y = (b*x - a*y)/determinant

The translation of an affine transform is always available as the x and y values. Other inputs can also be extracted. For an affine transform that is known to have no skew, the rotation and scale can be calculated as follows.

r = atan2( c , d )
sx = sqrt( a*a + b*b )
sy = sqrt( d*d + c*c )

The rotation, skew and scale may be calculated for an arbitrary affine transform. These will be the perceptual values in the order of least surprise, not the input values. When an affine transform is rotated and skewed, or skewed along both axis, there will be two rotation values. The least of these will be the perceptual rotation and the difference is the skew.

if ( a*d < 0 ) {
    if ( a < 0 ) { flip = 1; a = -a; b = -b; }
    else { flip = 2; d = -d; c = -c; }
} else {
    flip = 0
}

rx = atan2( -b , a )
ry = atan2( c , d )

if ( abs( rx ) < abs( ry ) ) {
    r = rx
    skew_rx = 0
    skew_ry = ry - rx
    scale_x = sqrt( a*a + b*b )
    scale_y = d / (cos(r) - sin(r)*tan(skew_ry))
} else {
    r = ry
    skew_ry = 0
    skew_rx = ry - rx
    scale_y = sqrt( d*d + c*c )
    scale_x = a / (cos(r) + sin(r)*tan(skew_rx))
}

if ( 1 == flip ) scale_x = -scale_x;
if ( 2 == flip ) scale_y = -scale_y;

The original matrix will equal the concatenation of the following effects.

translate(x,y) rotate(r) skew(skew_rx,skew_ry) scale(scale_x,scale_y)

This can be used to verify that a transform will be perceived as intended. When rotate, skew and scale operations are all concatenated, the scaling may be distorted if the skew and rotation are not complementary.

Affine transform documentation for some popular platforms:

560 questions
40
votes
5 answers

findHomography, getPerspectiveTransform, & getAffineTransform

This question is on the OpenCV functions findHomography, getPerspectiveTransform & getAffineTransform What is the difference between findHomography and getPerspectiveTransform?. My understanding from the documentation is that…
tetradeca7tope
  • 491
  • 1
  • 5
  • 7
22
votes
2 answers

Homography and Affine Transformation

Hi i am a beginner in computer vision and i wish to know what exactly is the difference between a homography and affine tranformation, if you want to find the translation between two images which one would you use and why?. From papers and…
16
votes
2 answers

Match set of x,y points to another set that is scaled, rotated, translated, and with missing elements

(Why am I doing this? See explanation below) Consider two sets of points, A and B as shown below It might not look like it, but set A is "hidden" within set B. It can not be easily seen because points in B are scaled, rotated, and translated in (x,…
Gabriel
  • 40,504
  • 73
  • 230
  • 404
15
votes
1 answer

Graphics Context misaligned on first paint

I've been working up an answer for another question and came across a bizare issue that I've not seen before... Basically, the program uses a AffineTransform to provide translation, scaling and rotating of a Graphics element, simple enough stuff,…
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
15
votes
1 answer

how to perform coordinates affine transformation using python? part 2

I have same problem as described here: how to perform coordinates affine transformation using python? I was trying to use method described but some reason I will get error messages. Changes I made to code was to replace primary system and secondary…
user3095658
  • 151
  • 1
  • 1
  • 3
14
votes
4 answers

How to directly rotate CVImageBuffer image in IOS 4 without converting to UIImage?

I am using OpenCV 2.2 on the iPhone to detect faces. I'm using the IOS 4's AVCaptureSession to get access to the camera stream, as seen in the code that follows. My challenge is that the video frames come in as CVBufferRef (pointers to…
Ian Charnas
  • 366
  • 1
  • 3
  • 8
14
votes
4 answers

Transform a triangle to another triangle

Hi i am trying to create the affine transform that will allow me to transform a triangle into another one. What i have are the coordinates for the 2 triangles. Can you help me? Following the answer by Adam Rosenfield i came up with this code in case…
Savvas Dalkitsis
  • 11,476
  • 16
  • 65
  • 104
13
votes
3 answers

AffineTransform without transforming Stroke?

When using the Graphics2D scale() function with two different parameters (scaling by different ratios in x- and y-direction), everything drawn later on this Graphics2D object is scaled too. This has the strange effect that lines drawn in one…
Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
13
votes
4 answers

How can I reliably rotate an image around a point?

I've read the following, from One step affine transform for rotation around a point?: CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y); transform = CGAffineTransformRotate(transform, a); transform =…
Christos Hayward
  • 5,777
  • 17
  • 58
  • 113
11
votes
2 answers

trouble getting cv.transform to work

I'd like to use the same affine matrix M on some individual (x,y) points as I use on images with cv2.warpAffine. It seems cv2.transform is the way to go . When I try send an Nx2 matrix of points I get negged ( src = np.array([ …
jeremy_rutman
  • 3,552
  • 4
  • 28
  • 47
11
votes
3 answers

Java error on bilinear interpolation of 16 bit data

I'm having an issue using bilinear interpolation for 16 bit data. I have two images, origImage and displayImage. I want to use AffineTransformOp to filter origImage through an AffineTransform into displayImage which is the size of the display…
Jon
  • 3,985
  • 7
  • 48
  • 80
10
votes
1 answer

Are there any UV Coordinates (or similar) for UIImageView?

I have a simple UIImageView in my view, but I can't seem to find any feature in Apple's documentation to change the UV Coordinates of this UIImageView, to convey my idea to you, this GIF file should preview how changing 4 vertices coordinates can…
DeyaEldeen
  • 10,847
  • 10
  • 42
  • 75
10
votes
1 answer

Can I simply add affine or perspective (homography) matrices of transformation?

As known, in OpenCV I can get affine or perspective transformation between 2 images: M - affine transformation - by using estimateRigidTransform() H - perspective (homography) transformation - by using FeatureDetector (SIFT, SURF, BRISK, FREAK,…
Alex
  • 12,578
  • 15
  • 99
  • 195
10
votes
4 answers

Problems rotating BufferedImage

I have some problems with rotating images in Java using the AffineTransform class. I have the following method for creating a rotated (90 degrees) copy of an image: private BufferedImage createRotatedCopy(BufferedImage img, Rotation rotation) { …
perp
  • 3,883
  • 4
  • 31
  • 29
10
votes
4 answers

Rotating Image with AffineTransform

I have got class called Airplane. Inside this class i have got variable img which is a BufferedImage type. What is more i have got class WorldMap which overrides function paintComponent(Graphics g): @Override public void paintComponent(Graphics g)…
user2410128
1
2 3
37 38