Sunteți pe pagina 1din 55

National University of Sciences and

Technology (NUST)
School of Electrical Engineering and Computer Science
(SEECS)

Digital Image Processing


(EE-333)

1 Khawar Khurshid, 2012


Image
Transformations

2 Khawar Khurshid, 2012


Tranformations
• Scaling

• Rotation

• Translation

• Shear

• Combination of transformations

• Matrix representation

• Matrix composition
2D Modeling Transformations
Modeling Coordinates
Scale
y Translate

Scale
Rotate
Translate

World Coordinates
2D Modeling Transformations
Modeling Coordinates
y

Let’s look
at this in
detail…

World Coordinates
2D Modeling Transformations
Modeling Coordinates
y

Initial location
at (0, 0) with
x- and y-axes
aligned
2D Modeling Transformations
Modeling Coordinates
y

Scale .3, .3
Rotate -90
Translate 5, 3
2D Modeling Transformations
Modeling Coordinates
y

Scale .3, .3
Rotate -90
Translate 5, 3
2D Modeling Transformations
Modeling Coordinates
y

Scale .3, .3
Rotate -90
Translate

World Coordinates
Scaling
• Scaling a coordinate means multiplying each of its
components by a scalar
• Uniform scaling means this scalar is the same for all
components:

2
Scaling
• Non-uniform scaling: different scalars per
component:

X  2,
Y  0.5

• How can we represent this in matrix form?


Scaling

• Scaling operation:  x' ax


 y '  by 
   

• Or, in matrix form:  x'  a 0  x 


 y '  0 b  y 
    

scaling matrix
Rotation
(x’, y’)

(x, y)

x’ = x cos() - y sin()
 y’ = x sin() + y cos()
Rotation
x = r cos (f)
y = r sin (f)
x’ = r cos (f + )
y’ = r sin (f + )
(x’, y’)
Trig Identity…
(x, y) x’ = r cos(f) cos() – r sin(f) sin()
y’ = r cos(f) sin() + r sin(f) cos()

 f
Substitute…
x’ = x cos() - y sin()
y’ = x sin() + y cos()
Rotation
• This is easy to capture in matrix form:

 x' cos    sin    x 


 y '   sin   cos     y 
    
Shear

Shear about y axis


Basic 2D Transformations

• Translation:
– x’ = x + tx
– y’ = y + ty
• Scale:
– x’ = x * sx
– y’ = y * sy
• Shear:
– x’ = x + hx*y
– y’ = y + hy*x
• Rotation: Transformations
– x’ = x*cosQ - y*sinQ
– y’ = x*sinQ + y*cosQ can be combined
(with simple algebra)
Basic 2D Transformations
• Translation:
– x’ = x + tx
– y’ = y + ty
• Scale:
– x’ = x * sx
– y’ = y * sy
• Shear:
– x’ = x + hx*y
– y’ = y + hy*x
• Rotation:
– x’ = x*cosQ - y*sinQ
– y’ = x*sinQ + y*cosQ
Basic 2D Transformations
• Translation:
– x’ = x + tx
– y’ = y + ty
• Scale:
– x’ = x * sx (x,y)
– y’ = y * sy
(x’,y’)
• Shear:
– x’ = x + hx*y
– y’ = y + hy*x
• Rotation:
– x’ = x*cosQ - y*sinQ x’ = x*sx
– y’ = x*sinQ + y*cosQ y’ = y*sy
Basic 2D Transformations
• Translation:
– x’ = x + tx
– y’ = y + ty
• Scale:
– x’ = x * sx
– y’ = y * sy
• Shear:
– x’ = x + hx*y
– y’ = y + hy*x
• Rotation: (x’,y’)
– x’ = x*cosQ - y*sinQ
– y’ = x*sinQ + y*cosQ x’ = (x*sx)*cosQ - (y*sy)*sinQ
y’ = (x*sx)*sinQ + (y*sy)*cosQ
Basic 2D Transformations
• Translation:
– x’ = x + tx
– y’ = y + ty
• Scale:
– x’ = x * sx
– y’ = y * sy (x’,y’)
• Shear:
– x’ = x + hx*y
– y’ = y + hy*x
• Rotation:
– x’ = x*cosQ - y*sinQ x’ = ((x*sx)*cosQ - (y*sy)*sinQ) + tx
– y’ = x*sinQ + y*cosQ y’ = ((x*sx)*sinQ + (y*sy)*cosQ) + ty
Basic 2D Transformations
• Translation:
– x’ = x + tx
– y’ = y + ty
• Scale:
– x’ = x * sx
– y’ = y * sy
• Shear:
– x’ = x + hx*y
– y’ = y + hy*x
• Rotation:
– x’ = x*cosQ - y*sinQ x’ = ((x*sx)*cosQ - (y*sy)*sinQ) + tx
– y’ = x*sinQ + y*cosQ y’ = ((x*sx)*sinQ + (y*sy)*cosQ) + ty
Matrix Representation
• Represent 2D transformation by a
matrix
a b 
 c d 
• Multiply matrix by column vector
 apply transformation to point

 x'   a b   x  x'  ax  by
 y'  c d   y  y '  cx  dy
Matrix Representation
• Transformations combined by
multiplication
 x'  a b   e f  i j x
 y '  c d   g h  k l   y 

Matrices are a convenient and efficient way


to represent a sequence of transformations!
2x2 Matrices
• What types of transformations can be
represented with a 2x2 matrix?
2D Identity?
x'  x  x'  1 0  x 
y'  y  y ' 0 1  y 

2D Scale around (0,0)?


x'  s x * x  x '  s x 0  x
y'  s y * y  y '   0 s y   y 
  
2x2 Matrices
• What types of transformations can be
represented with a 2x2 matrix?
2D Rotate around (0,0)?
x'  cos Q * x  sin Q * y  x ' cos Q  sin Q  x 
y '  sin Q * x  cos Q * y  y '   sin Q cos Q   y 
    

2D Shear?
x '  x  shx * y  x '  1 shx   x 
y '  shy * x  y  y '   sh 1   y 
   y
2x2 Matrices
• What types of transformations can be
represented with a 2x2 matrix?
2D Mirror about Y axis?
x'   x  x '     1 0  x 
y'  y  y'  0 1  y 

2D Mirror over (0,0)?


x'   x  x'    1 0   x 
y'   y  y'  0  1  y 
2x2 Matrices
• What types of transformations can be
represented with a 2x2 matrix?

2D Translation?

x'  x  t x
NO!
y'  y  t y
Homogeneous Coordinates

• Q: How can we represent translation as


a 3x3 matrix?

x'  x  t x
y'  y  t y
Homogeneous Coordinates

•Homogeneous coordinates
 x
 x  homogeneous coords  
– represent coordinates in 2  y      y 
dimensions with a 3-vector    1 

• Homogeneous coordinates seem unintuitive, but


they make graphics operations much easier
Homogeneous Coordinates
• Q: How can we represent translation as a 3x3
matrix? x'  x  t x

y'  y  t y

• A: Using the rightmost column:

1 0 t x 
 
Translation  0 1 t y 
0 0 1 
 
Translation
Example of translation •Homogeneous Coordinates

 x ' 1 0 t x   x   x  t x 
 y '  0 1 t   y    y  t 
   y    y

 1  0 0 1   1   1 

tx = 2
ty = 1
Basic 2D Transformations
• Basic 2D transformations as 3x3 matrices
 x '  s x 0 0  x 
 x ' 1 0 t x   x   y '   0 sy 0  y 
 y '  0 1 t   y    
   y    1   0 0 1  1 
 1  0 0 1   1 

Translate Scale

 x' cos Q  sin Q 0  x   x '  1 shx 0  x 


 y '   sin Q 0  y   y '   sh
   cos Q
   y 1 0  y 
 1   0 0 1  1   1   0 0 1  1 

Rotate Shear
Reflection
• Reflection with respect to the axis
• x • y • xy
1 0 0    1 0 0   1 0 0
0  1 0   0 1 0  0  1 0
     
0 0 1  0 0 1  0 0 1

y 1 y y
1 1’ 1’

2 3 2 3 3’ 2 3’ 2
x x 3 x
2’ 3’ 1

1’ 2
Reflection
 Reflection with respect to a Line

y
0 1 0 
1 0 0 
  x
0 0 1
y=x

 Clockwise rotation of 45  Reflection about the


x axis  Counterclockwise rotation of45
y y 1
y
2 3
x x
2’ 3’ x

1’
Matrix Composition
• Transformations can be combined by matrix
multiplication

 x'   1 0 tx  cos Q  sin Q 0  sx 0 0   x 


 y '    0 1 ty   sin Q cos Q 0  0 sy 0   y 
 w'  0 0 1   0 0     
1  0 0 1   w

p’ = T(tx,ty) R(Q) S(sx,sy) p
Matrix Composition
• Matrices are a convenient and efficient way
to represent a sequence of transformations

– General purpose representation

p’ = (T * (R * (S*p) ) )
p’ = (T*R*S) * p
Matrix Composition
• Be aware: order of transformations
matters
• Matrix multiplication is not commutative

p’ = T * R * S * p
Matrix Composition
•What if we want to rotate?
– Ex: Rotate line segment by 45 degrees about
endpoint ‘a’

a a
Multiplication Order – Wrong Way

• Our line is defined by two endpoints

– Applying a rotation of 45 degrees, R(45), affects both points


– We could try to translate both endpoints to return endpoint
a to its original position, but by how much?

a
a a
Wrong Correct
R(45) T(-3) R(45) T(3)
3D Concept
• Besides the 2D xy plane, a third dimension of depth is
added Y

• Geometric transformations in three dimensions


– Same idea
3D Transformations
• Same idea as 2D transformations
– Homogeneous coordinates: (x,y,z,w)
– 4x4 transformation matrices

 x'   a b c d  x 
 y'  e f g h  y 
 z'    i j k l  z 
 w' m p   w
 n o
3D Translation
• Translation of a Point
x'  x  t x , y'  y  t y , z'  z  t z

x
z
3D Translation
• Translation of a Point

 x' 1 0 0 tx  x
 y ' 0 1 0   
t y  y
 
 z '  0 0 1 tz  z 
    
 1  0 0 0 1  1 
3D Scaling
• Uniform Scaling
x'  x  s x , y'  y  s y , z'  z  sz

x
z
3D Scaling

 x'  s x 0 0 0  x 
 y '  0 sy 0 0  y 
 
 z'  0 0 sz 0  z 
    
1  0 0 0 1  1 

• How to scale if one of the object point


needs to stay at its position?
Relative Scaling
• Scaling with a Selected Fixed Position

y y y y

z x z x z x z x

Original position Translate Scaling Inverse Translate


Relative Scaling
• Scaling with a Selected Fixed Position
y y y y

z x z x z x z x

Original position Translate Scaling Inverse Translate

 x' 1 0 0 x f  sx 0 0 0 1 0 0  x f  x
 y ' 0 1 0 y f   0 s y 0 0 0 1 0  y f   y 
T ( x f , y f , z f )  S ( s x , s y , s z )  T (  x f , y f , z f )     
 z '  0 0 1 z f   0 0 sz 0  0 0 1  z f  z 
      
 1  0 0 0 1  0 0 0 1  0 0 0 1  1 
3D Rotation
• Coordinate-Axes Rotations
– X-axis rotation
– Y-axis rotation
– Z-axis rotation

• General 3D Rotations
– Rotation about an axis that is parallel to one of the
coordinate axes
– Rotation about an arbitrary axis
Coordinate-Axes Rotations
• X-Axis Rotation

 x' 1 0 0 0  x 
 y ' 0 cos   sin  0  y 
 
 z '  0 sin  cos  0  z 
    
 1  0 0 0 1  1 

x
z
Coordinate-Axes Rotations
• Y-Axis Rotation

 x'  cos  0 sin  0  x 


 y '  0 1 0 0  y 
 
 z '   sin  0 cos  0  z 
    
1  0 0 0 1  1 

x
z
Coordinate-Axes Rotations
• Z-Axis Rotation

 x' cos   sin  0 0  x 


 y '  sin  cos  0 0  y 
 
 z'  0 0 1 0  z 
    
1  0 0 0 1  1 

x
z
Coordinate-Axes Rotations
• X-Axis Rotation • Y-Axis Rotation • Z-Axis Rotation

 x' 1 0 0 0  x   x'  cos  0 sin  0  x   x' cos   sin  0 0  x 


 y ' 0 cos   y '  0 0  y   y '  sin  0  y 
   sin  0  y    1 0   cos  0
 z '  0 sin  cos  0  z   z '   sin  0 cos  0  z   z'  0 0 1 0  z 
              
 1  0 0 0 1  1  1  0 0 0 1  1  1  0 0 0 1  1 

y y y

x x x
z z z
Order of Rotations

Order of Rotation effects Final Position

X-axis  Z-axis

Z-axis  X-axis
End
Image
Transformations
Khawar Khurshid, 2012

S-ar putea să vă placă și