Assignment-1Geometric Transformation and Interpolation
Rotate Image and Perform Interpolation
1. Forward rotate image2. Inverse rotate image3. Rotation with interpolation
1. Nearest neighbour interpolation2. Bilinear interpolation
Rotation
• Initial location = (𝑥𝑥,𝑦𝑦)• After rotation
• 𝑥𝑥′,𝑦𝑦′ = ?
(x, y)𝜃𝜃
(x’, y’)
Rotation
• Initial location = (𝑥𝑥,𝑦𝑦)• After rotation
• 𝑥𝑥′,𝑦𝑦′ = ?𝑥𝑥′ = 𝑥𝑥 cos 𝜃𝜃 − 𝑦𝑦 sin(𝜃𝜃)𝑦𝑦′ = 𝑥𝑥 sin 𝜃𝜃 + 𝑦𝑦 cos 𝜃𝜃
(x, y)𝜃𝜃
(x’, y’)
Rotation
• Initial location = (𝑥𝑥,𝑦𝑦)• After rotation
• 𝑥𝑥′,𝑦𝑦′ = ?𝑥𝑥′ = 𝑥𝑥 cos 𝜃𝜃 − 𝑦𝑦 sin(𝜃𝜃)𝑦𝑦′ = 𝑥𝑥 sin 𝜃𝜃 + 𝑦𝑦 cos 𝜃𝜃Matrix Notation𝑥𝑥𝑥𝑦𝑦𝑥
= 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃 −𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃
𝑥𝑥𝑦𝑦
(x, y)𝜃𝜃
(x’, y’)
Rotation
• Initial location = (𝑥𝑥,𝑦𝑦)• After rotation
• 𝑥𝑥′,𝑦𝑦′ = ?𝑥𝑥′ = 𝑥𝑥 cos 𝜃𝜃 − 𝑦𝑦 sin(𝜃𝜃)𝑦𝑦′ = 𝑥𝑥 sin 𝜃𝜃 + 𝑦𝑦 cos 𝜃𝜃Matrix Notation𝑥𝑥𝑥𝑦𝑦𝑥
= 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃 −𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃
𝑥𝑥𝑦𝑦
(x, y)𝜃𝜃
(x’, y’)
Rotation Matrix
Inverse Rotation
• Given 𝑥𝑥′,𝑦𝑦′ that has• Undergone rotation by 𝜃𝜃
• Find original location (𝑥𝑥,𝑦𝑦)
(x, y)𝜃𝜃
(x’, y’) given
determine
Inverse Rotation
• Given 𝑥𝑥′,𝑦𝑦′ that has• Undergone rotation by 𝜃𝜃
• Find original location (𝑥𝑥,𝑦𝑦)Matrix Notation𝑥𝑥𝑥𝑦𝑦𝑥
= 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃 −𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃
𝑥𝑥𝑦𝑦
(x, y)𝜃𝜃
(x’, y’) given
determine
Inverse Rotation
• Given 𝑥𝑥′,𝑦𝑦′ that has• Undergone rotation by 𝜃𝜃
• Find original location (𝑥𝑥,𝑦𝑦)Matrix Notation𝑥𝑥𝑥𝑦𝑦𝑥
= 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃 −𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃
𝑥𝑥𝑦𝑦
𝑠𝑠𝑖𝑖 𝐴𝐴 = 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃 −𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃
(x, y)𝜃𝜃
(x’, y’) given
determine
Inverse Rotation
• Given 𝑥𝑥′,𝑦𝑦′ that has• Undergone rotation by 𝜃𝜃
• Find original location (𝑥𝑥,𝑦𝑦)Matrix Notation𝑠𝑠𝑖𝑖 𝐴𝐴 = 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃 −𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃
𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃𝑥𝑥𝑥𝑦𝑦𝑥
= 𝐴𝐴𝑥𝑥𝑦𝑦
(x, y)𝜃𝜃
(x’, y’) given
determine
Inverse Rotation
• Given 𝑥𝑥′,𝑦𝑦′ that has• Undergone rotation by 𝜃𝜃
• Find original location (𝑥𝑥,𝑦𝑦)Matrix Notation𝑠𝑠𝑖𝑖 𝐴𝐴 = 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃 −𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃
𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃𝑥𝑥𝑥𝑦𝑦𝑥
= 𝐴𝐴𝑥𝑥𝑦𝑦
𝐴𝐴𝑥𝑥𝑦𝑦
=𝑥𝑥𝑥𝑦𝑦𝑥
(x, y)𝜃𝜃
(x’, y’) given
determine
Inverse Rotation
• Given 𝑥𝑥′,𝑦𝑦′ that has• Undergone rotation by 𝜃𝜃
• Find original location (𝑥𝑥,𝑦𝑦)Matrix Notation𝑠𝑠𝑖𝑖 𝐴𝐴 = 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃 −𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃
𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃𝑥𝑥𝑥𝑦𝑦𝑥
= 𝐴𝐴𝑥𝑥𝑦𝑦
𝐴𝐴𝑥𝑥𝑦𝑦
=𝑥𝑥𝑥𝑦𝑦𝑥
(x, y)𝜃𝜃
(x’, y’) given
determine
Inverse Rotation
• Given 𝑥𝑥′,𝑦𝑦′ that has• Undergone rotation by 𝜃𝜃
• Find original location (𝑥𝑥,𝑦𝑦)Matrix Notation𝑠𝑠𝑖𝑖 𝐴𝐴 = 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃 −𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃
𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃𝑥𝑥𝑥𝑦𝑦𝑥
= 𝐴𝐴𝑥𝑥𝑦𝑦
𝑥𝑥𝑦𝑦
= 𝐴𝐴−1𝑥𝑥𝑥𝑦𝑦𝑥
(x, y)𝜃𝜃
(x’, y’) given
determine
Inverse of (2X2) matrix
𝑠𝑠𝑖𝑖 𝐾𝐾 = 𝑎𝑎 𝑏𝑏𝑐𝑐 𝑑𝑑
Inverse of (2X2) matrix
𝑠𝑠𝑖𝑖𝐾𝐾 = 𝑎𝑎 𝑏𝑏𝑐𝑐 𝑑𝑑
𝐾𝐾 𝑋𝑋 𝐾𝐾−1 = 𝐼𝐼 𝑠𝑠𝑑𝑑𝑖𝑖𝑠𝑠𝑖𝑖𝑠𝑠𝑖𝑖𝑦𝑦 𝑚𝑚𝑎𝑎𝑖𝑖𝑚𝑚𝑠𝑠𝑥𝑥𝐾𝐾−1 =
1𝑎𝑎𝑑𝑑 − 𝑏𝑏𝑐𝑐
𝑑𝑑 −𝑏𝑏−𝑐𝑐 𝑎𝑎
Inverse of (2X2) matrix
𝑠𝑠𝑖𝑖 𝐾𝐾 = 𝑎𝑎 𝑏𝑏𝑐𝑐 𝑑𝑑
𝐾𝐾 𝑋𝑋 𝐾𝐾−1 = 𝐼𝐼 𝑠𝑠𝑑𝑑𝑖𝑖𝑠𝑠𝑖𝑖𝑠𝑠𝑖𝑖𝑦𝑦 𝑚𝑚𝑎𝑎𝑖𝑖𝑚𝑚𝑠𝑠𝑥𝑥𝐾𝐾−1 =
1𝑎𝑎𝑑𝑑 − 𝑏𝑏𝑐𝑐
𝑑𝑑 −𝑏𝑏−𝑐𝑐 𝑎𝑎
𝐴𝐴 = 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃 −𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃
𝐴𝐴−1 = 1𝑐𝑐𝑐𝑐𝑐𝑐2𝜃𝜃+𝑐𝑐𝑠𝑠𝑠𝑠2𝜃𝜃
𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃 𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃−𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃 , 𝑐𝑐𝑜𝑜𝑜𝑜2𝜃𝜃 + 𝑜𝑜𝑠𝑠𝑠𝑠2𝜃𝜃=1
𝐴𝐴−1 = 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃 𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃−𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃
Inverse Rotation
• Given 𝑥𝑥′,𝑦𝑦′ that has• Undergone rotation by 𝜃𝜃
• Find original location (𝑥𝑥,𝑦𝑦)Matrix Notation𝑥𝑥𝑦𝑦
= 𝐴𝐴−1𝑥𝑥𝑥𝑦𝑦𝑥
𝑥𝑥𝑦𝑦
= 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃 𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃−𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃
𝑥𝑥𝑥𝑦𝑦𝑥 (x, y)
𝜃𝜃
(x’, y’) given
determine
Inverse Rotation
• Given 𝑥𝑥′,𝑦𝑦′ that has• Undergone rotation by 𝜃𝜃
• Find original location (𝑥𝑥,𝑦𝑦)Matrix Notation𝑥𝑥𝑦𝑦
= 𝐴𝐴−1𝑥𝑥𝑥𝑦𝑦𝑥
𝑥𝑥𝑦𝑦
= 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃 𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃−𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃
𝑥𝑥𝑥𝑦𝑦𝑥 (x, y)
𝜃𝜃
(x’, y’) given
determine
Inverse rotation matrix
Part 1: Forward Rotation
Forward rotate by𝜃𝜃 = 0.5 radians
(~28 deg)
Input
Output
Forward Rotation
Forward rotate by𝜃𝜃 = 0.5 radians
(~28 deg)
• Input:• Image• Theta (𝜃𝜃) - angle of rotation
Assumption
• To simplify,• We assume the following co-ordinate system
• O-Origin
x
yO
Assumption
• To simplify,• We assume the following co-ordinate
system• Theta denote rotation from x-axis,
towards y-axis
x
y
𝜽𝜽
O
Rotate each pixel
• Let R be the rotated image and I the original image.
x
y
𝜽𝜽
O
(𝑠𝑠, 𝑗𝑗)
𝑠𝑠′ , 𝑗𝑗′
Rotate each pixel
• Let R be the rotated image and I the original image.
• For every pixel 𝑠𝑠, 𝑗𝑗 in I • apply rotation by theta to get 𝑠𝑠′ , 𝑗𝑗′
in new image.
𝑠𝑠𝑥𝑗𝑗𝑥
= 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃 −𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃
𝑠𝑠𝑗𝑗
x
y
𝜽𝜽
O
(𝑠𝑠, 𝑗𝑗)
𝑠𝑠′ , 𝑗𝑗′
Rotate each pixel
• Let R be the rotated image and I the original image.
• For every pixel 𝑠𝑠, 𝑗𝑗 apply rotation by theta to get 𝑠𝑠′ , 𝑗𝑗′ in new image.
𝑠𝑠𝑥𝑗𝑗𝑥
= 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃 −𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃
𝑠𝑠𝑗𝑗
• 𝑅𝑅 𝑠𝑠′, 𝑗𝑗′ = 𝐼𝐼(𝑠𝑠, 𝑗𝑗)
x
y
𝜽𝜽
O
(𝑠𝑠, 𝑗𝑗)
𝑠𝑠′ , 𝑗𝑗′
Determine size of rotated image
Determine size of rotated image
x
y
𝜽𝜽
O
Determine size of rotated image
x
y
𝜽𝜽
O
Determine size of rotated image
x
y𝜽𝜽
O
Determine size of rotated image
x
y
𝜽𝜽
O
Determine size of rotated image
x
y
𝜽𝜽
O
Determine size of rotated image
x
y
𝜽𝜽
1. Compute Rotation matrix2. Rotate corners
O
Determine size of rotated image
x
y
𝜽𝜽
1. Compute Rotation matrix2. Rotate corners
𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥 = minimum of x value amongst the rotated corners
𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦 ,𝑚𝑚𝑎𝑎𝑥𝑥𝑥𝑥 …1. Get (𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥, 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦) (top left of the image)
1. Is also the new origin (N)2. Get (𝑚𝑚𝑎𝑎𝑥𝑥𝑥𝑥, 𝑚𝑚𝑎𝑎𝑥𝑥𝑦𝑦) (bottom right)
O
N
Let I be the original image
Determine size of rotated image
x
y
𝜽𝜽
1. Compute Rotation matrix2. Rotate corners
1. Get (𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥, 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦) (top left of the image)2. Get (𝑚𝑚𝑎𝑎𝑥𝑥𝑥𝑥, 𝑚𝑚𝑎𝑎𝑥𝑥𝑦𝑦) (bottom right)
1. Rows = 𝑚𝑚𝑎𝑎𝑥𝑥𝑥𝑥 – 𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥2. Cols = 𝑚𝑚𝑎𝑎𝑥𝑥𝑦𝑦 - 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦
3. Size of rotated image = (rows, cols)
O
N
Let I be the original image
Create empty image
x
y
𝜽𝜽
1. Compute Rotation matrix2. Rotate corners
1. Get (𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥, 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦) (top left of the image)2. Get (𝑚𝑚𝑎𝑎𝑥𝑥𝑥𝑥, 𝑚𝑚𝑎𝑎𝑥𝑥𝑦𝑦) (bottom right)
1. Rows = 𝑚𝑚𝑎𝑎𝑥𝑥𝑥𝑥 – 𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥2. Cols = 𝑚𝑚𝑎𝑎𝑥𝑥𝑦𝑦 - 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦
3. Size of rotated image = (rows, cols)3. Create rotated image (R) of size (rows, cols)
O
N
Let I be the original image
Rotate each pixel
x
y
𝜽𝜽
1. Compute Rotation matrix2. Rotate corners
1. Get (𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥, 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦) (top left of the image)2. Get (𝑚𝑚𝑎𝑎𝑥𝑥𝑥𝑥, 𝑚𝑚𝑎𝑎𝑥𝑥𝑦𝑦) (bottom right)
1. Rows = 𝑚𝑚𝑎𝑎𝑥𝑥𝑥𝑥 – 𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥2. Cols = 𝑚𝑚𝑎𝑎𝑥𝑥𝑦𝑦 - 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦
3. Size of rotated image = (rows, cols)3. Create rotated image (R) of size (rows, cols)4. For each (i, j) in original image:
1. Compute rotated location (I’, j’)
(i, j)
(i’, j’)
𝜽𝜽O
N
Let I be the original image
Rotate each pixel
x
y
𝜽𝜽
1. Compute Rotation matrix2. Rotate corners
1. Get (𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥, 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦) (top left of the image)2. Get (𝑚𝑚𝑎𝑎𝑥𝑥𝑥𝑥, 𝑚𝑚𝑎𝑎𝑥𝑥𝑦𝑦) (bottom right)
1. Rows = 𝑚𝑚𝑎𝑎𝑥𝑥𝑥𝑥 – 𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥2. Cols = 𝑚𝑚𝑎𝑎𝑥𝑥𝑦𝑦 - 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦
3. Size of rotated image = (rows, cols)3. Create rotated image (R) of size (rows, cols)4. For each (i, j) in original image:
1. Compute rotated location (I’, j’)
(i, j)
(i’, j’)
𝜽𝜽O
N
This is with respect to the origin O
Let I be the original image
Rotate each pixel
x
y
𝜽𝜽
1. Compute Rotation matrix2. Rotate corners
1. Get (𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥, 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦) (top left of the image)2. Get (𝑚𝑚𝑎𝑎𝑥𝑥𝑥𝑥, 𝑚𝑚𝑎𝑎𝑥𝑥𝑦𝑦) (bottom right)
1. Rows = 𝑚𝑚𝑎𝑎𝑥𝑥𝑥𝑥 – 𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥2. Cols = 𝑚𝑚𝑎𝑎𝑥𝑥𝑦𝑦 - 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦
3. Size of rotated image = (rows, cols)3. Create rotated image (R) of size (rows, cols)4. For each (i, j) in original image:
1. Compute rotated location (I’, j’)
(i, j)
(i’, j’)
𝜽𝜽O
N
This is with respect to the origin O
Let I be the original image
Step 3: Compute new location with respect to N
x
y
𝜽𝜽
1. Compute Rotation matrix2. Rotate corners
1. Get (𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥, 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦) (top left of the image)2. Get (𝑚𝑚𝑎𝑎𝑥𝑥𝑥𝑥, 𝑚𝑚𝑎𝑎𝑥𝑥𝑦𝑦) (bottom right)
1. Rows = 𝑚𝑚𝑎𝑎𝑥𝑥𝑥𝑥 – 𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥2. Cols = 𝑚𝑚𝑎𝑎𝑥𝑥𝑦𝑦 - 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦
3. Size of rotated image = (rows, cols)3. Create rotated image (R) of size (rows, cols)4. For each (i, j) in original image:
1. Compute rotated location (i’, j’)2. 𝑠𝑠𝑁𝑁′ = 𝑠𝑠′ − 𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥, 𝑗𝑗𝑁𝑁′ = 𝑗𝑗′ − 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦
(i, j)
(i’, j’)
𝜽𝜽O
N
This is with respect to the origin O
Let I be the original image (𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ )
Step 3: Assign pixel value
x
y
𝜽𝜽
1. Compute Rotation matrix2. Rotate corners
1. Get (𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥, 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦) (top left of the image)2. Get (𝑚𝑚𝑎𝑎𝑥𝑥𝑥𝑥, 𝑚𝑚𝑎𝑎𝑥𝑥𝑦𝑦) (bottom right)
1. Rows = 𝑚𝑚𝑎𝑎𝑥𝑥𝑥𝑥 – 𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥2. Cols = 𝑚𝑚𝑎𝑎𝑥𝑥𝑦𝑦 - 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦
3. Size of rotated image = (rows, cols)3. Create rotated image (R) of size (rows, cols)4. For each (i, j) in original image:
1. Compute rotated location (i’, j’)2. 𝑠𝑠𝑁𝑁′ = 𝑠𝑠′ − 𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥, 𝑗𝑗𝑁𝑁′ = 𝑗𝑗′ − 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦3. R(𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ ) = I(i, j)
(i, j)
(i’, j’)
𝜽𝜽O
N
This is with respect to the origin O
Let I be the original image (𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ )
Step 3: Assign pixel value
x
y
𝜽𝜽
1. Compute Rotation matrix2. Rotate corners
1. Get (𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥, 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦) (top left of the image)2. Get (𝑚𝑚𝑎𝑎𝑥𝑥𝑥𝑥, 𝑚𝑚𝑎𝑎𝑥𝑥𝑦𝑦) (bottom right)
1. Rows = 𝑚𝑚𝑎𝑎𝑥𝑥𝑥𝑥 – 𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥2. Cols = 𝑚𝑚𝑎𝑎𝑥𝑥𝑦𝑦 - 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦
3. Size of rotated image = (rows, cols)3. Create rotated image (R) of size (rows, cols)4. For each (i, j) in original image:
1. Compute rotated location (i’, j’)2. 𝑠𝑠𝑁𝑁′ = 𝑠𝑠′ − 𝑚𝑚𝑠𝑠𝑠𝑠𝑥𝑥, 𝑗𝑗𝑁𝑁′ = 𝑗𝑗′ − 𝑚𝑚𝑠𝑠𝑠𝑠𝑦𝑦3. R(𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ ) = I(i, j)
O
N
This is with respect to the origin O
Let I be the original image
Part 2: Reverse Rotation
Reverse rotate by𝜃𝜃 = 0.5 radians
(~28 deg)
Input
Output
Input:1. Rotated image2. Theta: the angle by which the
image was rotated 3. Origin (𝑂𝑂 = (𝑂𝑂𝑠𝑠 ,𝑂𝑂𝑗𝑗)): the origin
of the original image with respect to the origin of rotated image
4. Original shape: Shape of the original image before rotation
Input:1. Rotated image2. Theta: the angle by which the
image was rotated 3. Origin (𝑂𝑂 = (𝑂𝑂𝑠𝑠 ,𝑂𝑂𝑗𝑗)): the origin
of the original image with respect to the origin of rotated image
4. Original shape: Shape of the original image
1. Compute inverse rotation matrix
O
Let R be rotated imageN
Create empty image with original shape
1. Compute inverse rotation matrix2. Create image (I) of shape (original
shape)O
Let R be rotated imageN
Iterate through each pixel and compute original location1. Compute inverse rotation matrix2. Create image (I) of shape (original
shape)3. For (𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ ) in rotated image
1. Calculate location with respect to O
O
(𝒊𝒊𝑵𝑵′ , 𝒋𝒋𝑵𝑵′ )(𝒊𝒊′ , 𝒋𝒋′)
Let R be rotated image N
Iterate through each pixel and compute original location1. Compute inverse rotation matrix2. Create image (I) of shape (original
shape)3. For (𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ ) in rotated image
1. Calculate location with respect to O𝑠𝑠′ = 𝑠𝑠𝑁𝑁′ − 𝑂𝑂𝑠𝑠 , 𝑗𝑗′ = 𝑗𝑗𝑁𝑁′ − 𝑂𝑂𝑗𝑗
O
(𝒊𝒊𝑵𝑵′ , 𝒋𝒋𝑵𝑵′ )(𝒊𝒊′ , 𝒋𝒋′)
Let R be rotated image N
Iterate through each pixel and compute original location1. Compute inverse rotation matrix2. Create image (I) of shape (original
shape)3. For (𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ ) in rotated image
1. Calculate location with respect to O𝑠𝑠′ = 𝑠𝑠𝑁𝑁′ − 𝑂𝑂𝑠𝑠 , 𝑗𝑗′ = 𝑗𝑗𝑁𝑁′ − 𝑂𝑂𝑗𝑗
2. Compute inverse rotation on 𝑠𝑠′, 𝑗𝑗′ to get (𝑠𝑠, 𝑗𝑗)
O
(𝒊𝒊𝑵𝑵′ , 𝒋𝒋𝑵𝑵′ )(𝒊𝒊′ , 𝒋𝒋′)
(𝒊𝒊, 𝒋𝒋)
Let R be rotated image
𝜃𝜃
N
Iterate through each pixel and compute original location1. Compute inverse rotation matrix2. Create image (I) of shape (original
shape)3. For (𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ ) in rotated image
1. Calculate location with respect to O𝑠𝑠′ = 𝑠𝑠𝑁𝑁′ − 𝑂𝑂𝑠𝑠 , 𝑗𝑗′ = 𝑗𝑗𝑁𝑁′ − 𝑂𝑂𝑗𝑗
2. Compute inverse rotation on 𝑠𝑠′, 𝑗𝑗′ to get (𝑠𝑠, 𝑗𝑗)
𝑠𝑠𝑗𝑗
= 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃 𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃−𝑜𝑜𝑠𝑠𝑠𝑠𝜃𝜃 𝑐𝑐𝑜𝑜𝑜𝑜𝜃𝜃
𝑠𝑠𝑥𝑗𝑗𝑥
O
(𝒊𝒊𝑵𝑵′ , 𝒋𝒋𝑵𝑵′ )(𝒊𝒊′ , 𝒋𝒋′)
(𝒊𝒊, 𝒋𝒋)
Let R be rotated image
𝜃𝜃
N
Assign values
1. Compute inverse rotation matrix2. Create image (I) of shape (original
shape)3. For (𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ ) in rotated image
1. Calculate location with respect to O𝑠𝑠′ = 𝑠𝑠𝑁𝑁′ − 𝑂𝑂𝑠𝑠 , 𝑗𝑗′ = 𝑗𝑗𝑁𝑁′ − 𝑂𝑂𝑗𝑗
2. Compute inverse rotation on 𝑠𝑠′, 𝑗𝑗′ to get (𝑠𝑠, 𝑗𝑗)3. 𝐼𝐼 𝑠𝑠, 𝑗𝑗 = 𝑅𝑅(𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ )
O
(𝒊𝒊𝑵𝑵′ , 𝒋𝒋𝑵𝑵′ )(𝒊𝒊′ , 𝒋𝒋′)
(𝒊𝒊, 𝒋𝒋)
Let R be rotated image N
Output
Forward rotation
• Missing Pixel Values• Perform interpolation to fill in
missing values.
Part 3: Rotation with Interpolation
Rotation with interpolation𝜃𝜃 = 0.5 radians
(~28 deg)
Part 3: Rotation with interpolation
Input
Output
Rotation with interpolation𝜃𝜃 = 0.5 radians
(~28 deg)
Part 3: Forward Rotation
Input
OutputInput:1. Image2. Theta3. Type of interpolation
• Solution: Combination of Part 1 and 2
Part 3: Forward Rotation
Idea
x
y
𝜽𝜽
Assuming R is the image obtained after rotation.O
N
Let I be the original image
Idea
x
y
𝜽𝜽
Assuming R is the image obtained after rotation.
1. For (𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ ) in rotated image R.1. Determine its location 𝑠𝑠, 𝑗𝑗 before
rotation in the original image
O
N
Let I be the original image
(𝒊𝒊𝑵𝑵′ , 𝒋𝒋𝑵𝑵′ )
(𝒊𝒊, 𝒋𝒋)
Idea
x
y
𝜽𝜽
Assuming R is the image obtained after rotation.
1. For (𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ ) in rotated image R.1. Determine its location 𝑠𝑠, 𝑗𝑗 before
rotation in the original image2. Use neighbors of (𝑠𝑠, 𝑗𝑗) to perform either
nearest neighbour or bilinear interpolation.
3. R 𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ = 𝑠𝑠𝑠𝑠𝑖𝑖𝑖𝑖𝑚𝑚𝑖𝑖𝑖𝑖𝑜𝑜𝑖𝑖𝑖𝑖𝑑𝑑(𝑠𝑠, 𝑗𝑗)
O
N
Let I be the original image
(𝒊𝒊𝑵𝑵′ , 𝒋𝒋𝑵𝑵′ )
(𝒊𝒊, 𝒋𝒋)
Compute size of rotate image
x
y
𝜽𝜽
1. Compute Rotation matrix2. Compute Inverse Rotation matrix3. Compute size of the rotated image O
N
Let I be the original image
Create Empty Image
x
y
𝜽𝜽
1. Compute Rotation matrix2. Compute Inverse Rotation matrix3. Compute size of the rotated image4. Create rotated image (R) of size (rows, cols) O
N
Let I be the original image
Calculate O
x
y
𝜽𝜽
1. Compute Rotation matrix2. Compute Inverse Rotation matrix3. Compute size of the rotated image4. Create rotated image (R) of size (rows, cols)5. Calculate location O, with respect to N (𝑂𝑂 =
− min𝑥𝑥, − min𝑦𝑦) (Computed from 4 corners)
O
N
Let I be the original image
Iterate and interpolate
x
y
𝜽𝜽
1. Compute Rotation matrix2. Compute Inverse Rotation matrix3. Compute size of the rotated image4. Create rotated image (R) of size (rows, cols)5. Calculate location O, with respect to N (𝑂𝑂 =
− min𝑥𝑥, − min𝑦𝑦)(Computed from 4 corners)6. For (𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ ) in rotated image
1. Calculate location with respect to O𝑠𝑠′ = 𝑠𝑠𝑁𝑁′ − 𝑂𝑂𝑠𝑠 , 𝑗𝑗′ = 𝑗𝑗𝑁𝑁′ − 𝑂𝑂𝑗𝑗
2.
O
N
Let I be the original image
(𝒊𝒊𝑵𝑵′ , 𝒋𝒋𝑵𝑵′ )(𝒊𝒊′ , 𝒋𝒋′)
Iterate and interpolate
x
y
𝜽𝜽
1. Compute Rotation matrix2. Compute Inverse Rotation matrix3. Compute size of the rotated image4. Create rotated image (R) of size (rows, cols)5. Calculate location O, with respect to N (𝑂𝑂 =
− min𝑥𝑥, − min𝑦𝑦)(Computed from 4 corners)6. For (𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ ) in rotated image
1. Calculate location with respect to O𝑠𝑠′ = 𝑠𝑠𝑁𝑁′ − 𝑂𝑂𝑠𝑠 , 𝑗𝑗′ = 𝑗𝑗𝑁𝑁′ − 𝑂𝑂𝑗𝑗
2. Compute inverse rotation on 𝑠𝑠′, 𝑗𝑗′ to get (𝑠𝑠, 𝑗𝑗)
O
N
Let I be the original image
(𝒊𝒊𝑵𝑵′ , 𝒋𝒋𝑵𝑵′ )(𝒊𝒊′ , 𝒋𝒋′)
𝜃𝜃
(𝒊𝒊, 𝒋𝒋)
Nearest interpolate
x
y
𝜽𝜽
1. Compute Rotation matrix2. Compute Inverse Rotation matrix3. Compute size of the rotated image4. Create rotated image (R) of size (rows, cols)5. Calculate location O, with respect to N (𝑂𝑂 =
− min𝑥𝑥, − min𝑦𝑦)(Computed from 4 corners)6. For (𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ ) in rotated image
1. Calculate location with respect to O𝑠𝑠′ = 𝑠𝑠𝑁𝑁′ − 𝑂𝑂𝑠𝑠 , 𝑗𝑗′ = 𝑗𝑗𝑁𝑁′ − 𝑂𝑂𝑗𝑗
2. Compute inverse rotation on 𝑠𝑠′, 𝑗𝑗′ to get (𝑠𝑠, 𝑗𝑗)3. If using nearest neighbour interpolation
1. nearest neighbour (𝑠𝑠𝑠𝑠𝑠𝑠, 𝑗𝑗𝑠𝑠𝑠𝑠)= (𝑚𝑚𝑜𝑜𝑟𝑟𝑠𝑠𝑑𝑑(𝑠𝑠), 𝑚𝑚𝑜𝑜𝑟𝑟𝑠𝑠𝑑𝑑(𝑗𝑗))2. R 𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ = 𝐼𝐼(𝑠𝑠𝑠𝑠𝑠𝑠, 𝑗𝑗𝑠𝑠𝑠𝑠)
O
N
Let I be the original image
(𝒊𝒊𝑵𝑵′ , 𝒋𝒋𝑵𝑵′ )(𝒊𝒊′ , 𝒋𝒋′)
𝜃𝜃
(𝒊𝒊, 𝒋𝒋)
Bilinear interpolate
x
y
𝜽𝜽
1. Compute Rotation matrix2. Compute Inverse Rotation matrix3. Compute size of the rotated image4. Create rotated image (R) of size (rows, cols)5. Calculate location O, with respect to N (𝑂𝑂 =
− min𝑥𝑥, − min𝑦𝑦)(Computed from 4 corners)6. For (𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ ) in rotated image
1. Calculate location with respect to O𝑠𝑠′ = 𝑠𝑠𝑁𝑁′ − 𝑂𝑂𝑠𝑠 , 𝑗𝑗′ = 𝑗𝑗𝑁𝑁′ − 𝑂𝑂𝑗𝑗
2. Compute inverse rotation on 𝑠𝑠′, 𝑗𝑗′ to get (𝑠𝑠, 𝑗𝑗)3. If using bilinear interpolation
1. Find four nearest neighbors to (𝑠𝑠, 𝑗𝑗)
O
N
Let I be the original image
(𝒊𝒊𝑵𝑵′ , 𝒋𝒋𝑵𝑵′ )(𝒊𝒊′ , 𝒋𝒋′)
𝜃𝜃
(𝒊𝒊, 𝒋𝒋)
Bilinear interpolate
x
y
𝜽𝜽
1. Compute Rotation matrix2. Compute Inverse Rotation matrix3. Compute size of the rotated image4. Create rotated image (R) of size (rows, cols)5. Calculate location O, with respect to N (𝑂𝑂 =
− min𝑥𝑥, − min𝑦𝑦)(Computed from 4 corners)6. For (𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ ) in rotated image
1. Calculate location with respect to O𝑠𝑠′ = 𝑠𝑠𝑁𝑁′ − 𝑂𝑂𝑠𝑠 , 𝑗𝑗′ = 𝑗𝑗𝑁𝑁′ − 𝑂𝑂𝑗𝑗
2. Compute inverse rotation on 𝑠𝑠′, 𝑗𝑗′ to get (𝑠𝑠, 𝑗𝑗)3. If using bilinear interpolation
1. Find four nearest neighbors to (𝑠𝑠, 𝑗𝑗)2. perform bi-linear interpolated value (b)2. R 𝑠𝑠𝑁𝑁′ , 𝑗𝑗𝑁𝑁′ = 𝑏𝑏
O
N
Let I be the original image
(𝒊𝒊𝑵𝑵′ , 𝒋𝒋𝑵𝑵′ )(𝒊𝒊′ , 𝒋𝒋′)
𝜃𝜃
(𝒊𝒊, 𝒋𝒋)
Without Interpolation Nearest neighbor Interpolation
Assignment - 1
1. Forward Rotate (10 Pts.)2. Reverse Rotate (10 Pts)3. Rotate with interpolation (20 Pts)
Total: 40 Pts.Due Date: Oct 1st
Submission Instructions
• Must use the starter code available in Github• Submission allowed only through Github• You will receive an email with invitation to join Github classroom• Start by reading the readme.md file.• Instructions are available here• Github will automatically save the last commit as a submission
before the deadline