2D transformation using built-in tool
2 steps:
Overview
1. Define parameters (transformation matrix) for spatial transformation.
Input Image
Transformation matrix
2. Perform the transformation by passing the input image and the transformation matrix to cv2.warpAffine().
cv2.warpAffine
Transformed image
37
Step 1
• All transformation we dealt with can be expressed as an affine transformation.
• The affine transformation can be written in the matrix
form as:
= 1 = 1
1 001
• In Python, only the first two rows are needed:
Input coordinates Output coordinates
(y’, x’) (y, x)
(i, j) (i’, j’)
=
MATLAB notation
Our previous notation
38
Example: Rotation about a point
• In Python, , → (,)
39
Step 1
Operation
Coordinate equations
*Affine matrix, T
Translation
′=+ ′ = +
1 0 0 1 001
Scaling about origin
′= ′=
0 0 0 0 001
Scaling about apoint( , )
′ =0+(−0)=+(0−0) ′ =0 +(−0)=+(0 −0)
0 −0
Rotation about origin for clockwise
′ = − ′ = +
− 0 0
Rotation about apoint( , )
0 0 0 ′ =(− )+(− )+
−
00
001
00
000001
′ =(−0)−(−0)+0
0 0 1
=−+( − + )
=++( − − )
*Note that only the first two rows of T are needed in Python.
0 0 0
0
0 0−0
40
Step 2
• Step 2: Perform the transformation.
im2 = cv2.warpAffine(im, T, (width, height)) – im is the input image; im2 is the output
image
– T is the transformation matrix. We can create it as a Numpy array of type np.float32
– width and height specify the size of the output image
41
Putting it all together …
import numpy as np
import math
import cv2
import matplotlib.pyplot as plt
Rotate about
centre of image counterclockwise for 450
im = cv2.imread(‘pout.tif’, cv2.IMREAD_UNCHANGED) plt.imshow(im, cmap=’gray’, vmin = 0, vmax = 255) plt.axis(‘off’)
plt.show()
height, width = im.shape
# Compute centre of image
x0 = np.round(0.5*(width-1)) y0 = np.round(0.5*(height-1))
# Step 1: Define affine matrix, T
theta = 45 # angle
theta = math.radians(-1*theta) # radian
tx = x0-x0*math.cos(theta)+y0*math.sin(theta) ty = y0-x0*math.sin(theta)-y0*math.cos(theta) T = np.float32([[math.cos(theta), – 1*math.sin(theta), tx], [math.sin(theta), math.cos(theta), ty]])
# Step 2: Perform the transformation
im2 = cv2.warpAffine(im, T, (width, height)) plt.imshow(im2, cmap=’gray’, vmin = 0, vmax = 255) plt.axis(‘off’)
plt.show()
42
Putting it all together …
import numpy as np
import math
import cv2
import matplotlib.pyplot as plt
• We can also create the transformation matrix in
im = cv2.imread(‘pout.tif’, cv2.IMREAD_UNCHANGED) plt.imshow(im, cmap=’gray’, vmin = 0, vmax = 255) plt.axis(‘off’)
plt.show()
Step 1 with the build-in function
height, width = im.shape
cv2.getRotationMa
# Compute centre of image
x0 = np.round(0.5*(width-1)) y0 = np.round(0.5*(height-1))
trix2D()
# Step 1: Define affine matrix, T
theta = 45 # angle
theta = math.radians(-1*theta) # radian
• T= cv2.getRotationMatrix2D (center=(x0, y0), angle=45, scale=1)
tx = x0-x0*math.cos(theta)+y0*math.sin(theta) ty = y0-x0*math.sin(theta)-y0*math.cos(theta) T = np.float32([[math.cos(theta), – 1*math.sin(theta), tx], [math.sin(theta), math.cos(theta), ty]])
# Step 2: Perform the transformation
im2 = cv2.warpAffine(im, T, (width, height)) plt.imshow(im2, cmap=’gray’, vmin = 0, vmax = 255) plt.axis(‘off’)
plt.show()
43