Lab 02
Lab 02
Lab 02
4 Exercises
Exercise 1: Write a function to transform an input image based on the following transformation:
1. Rotation with the different degrees (45, -45, 120, -120, 180, 270)
2. Scaling images with scale = 2, 1/2
3. Translation image with tx = 50, ty = 100
For each case, the image result is saved with jpg format
Hint:
• cv2.warpAffine(img,M,(w, h))
• cv2.getRotationMatrix2D(center, angle, scale)
Exercise 2: Write a function to perform perspective transformation for images on Google Drive of lab2.
1. With book.png, P = {(159.0, 125), (263, 183), (160, 336), (38, 234)}
2. With paper1.png, P = {(33, 97), (292, 111), (436, 457), (162, 577)}
3. With ex2.png, P = {(36, 62), (196, 19), (238, 102), (59, 163)}
Hint:
• cv2.getPerspectiveTransform(point1, point2)
• cv2.warpPerspective(I, M, (width, height))
Exercise 3: Write functions to convert an image from RGB color-space to gray with the following cases:
1. The lightness method averages the most prominent and least prominent colors: (max(R, G, B) +
min(R, G, B))/2
2. The average method simply averages the values: (R + G + B)/3.
3. The luminosity method is a more sophisticated version of the average method:
(a) 0.21R + 0.72G + 0.07B
(b) 0.299R + 0.7587G + 0.114B
Exercise 4: Write functions to convert an image from RGB color-space to CMY color-space and reverse
with the following equation:
C 255 R
M = 255 − G
Y 255 B
Exercise 5: Write functions to convert an image from RGB color-space to YCbCr and reverse which based
on the equation below:
219(0.299R + 0.587G + 0.114B)
16 +
Y 255
Cb = 128 + 224(−0.169R − 0.331G + 0.5B)
255
Cr
224(0.5R − 0.419G − 0.081B)
128 +
255
Anh H. Vo - vohoanganh@tdtu.edu.vn 8
Ton Duc Thang University
Faculty of Information Technology
256(Y − 0.00093Cb + 0.114Cr)
−16 +
R 219
G = −128 + 256(Y − 0.3437Cb − 0.71417Cr)
224
B
256(Y − 1.77216Cb − 0.00099Cr)
−128 +
224
Exercise 6: Write function to conversion between RGB and YIQ with the following equations
Y 0.299 0.587 0.114 R
I = 0.596 −0.274 −0.322 G
Q 0.211 −0.523 0.311 B
R 1.0 0.956 0.621 Y
G = 1.0 −0.272 −0.649 I
B 1.0 −1.106 1.703 Q
Exercise 7: Write function to conversion between RGB and YUV with the following equations
Y 0.299 0.587 0.114 R
U = −0.147 −0.289 0.436 G ,
V 0.615 −0.515 −0.100 B
R 1.0 0.0 1.140 Y
G = 1.0 −0.395 −0.581 U
B 1.0 2.032 0.00 V
Hint:
import argparse
parser = argparse.ArgumentParser()
parser.add argument(’square’, help=’display a square of a given number’, type=int)
args = parser.parse args()
print(args.square**2)
Anh H. Vo - vohoanganh@tdtu.edu.vn 9