Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Page 1

Lab 3: Geometric transformation with OpenCV

Electrical Engineering Department


DEC40092-Computer Vision Programming

Lab 3: Geometric transformation with


OpenCV
Lecturer: Gauri Birasamy

Title : Geometric transformation with OpenCV

Learning Outcomes
At the end of this lab session, the student should be able to create a simple program:

1. Learn to apply different geometric transformation to images like translation, rotation, affine transformation
etc

2. Learn to Capture video and display.

1. Material and Apparatus


Personal Computer with Python, OpenCV.

Theory
 Scaling is just resizing of the image. OpenCV comes with a function cv2.resize() for this purpose. The
size of the image can be specified manually, or can specify the scaling factor.
 Translation is the shifting of object’s location.
 Rotation of an image for an angle θ is achieved by the transformation matrix
 In affine transformation, all parallel lines in the original image will still be parallel in the output
image.

Page 2
Lab 3: Geometric transformation with OpenCV

Procedure:
EXERCISES 1

Scaling
Different interpolation methods are used. Preferable interpolation methods are cv2.INTER_AREA for
shrinking and cv2.INTER_CUBIC (slow) & cv2.INTER_LINEAR for zooming. By default, interpolation
method used is cv2.INTER_LINEAR for all resizing purposes. You can resize an input image either of
following methods:

1. Type the program in figure 1 in notepad++/ python shell.

import cv2
import numpy as np

res = cv2.resize(img,None,fx=2, fy=2, interpolation = cv2.INTER_CUBIC)

#OR

height, width = img.shape[:2]


res = cv2.resize(img,(2*width, 2*height), interpolation = cv2.INTER_CUBIC)
Figure 1: Scaling programming

2. Save as xxxx.py.

3. Run the program.

Page 3
Lab 3: Geometric transformation with OpenCV

4. Save the result and explain the program and results.

Page 4
Lab 3: Geometric transformation with OpenCV

EXERCISES 2

Translation
Translation is the shifting of object’s location. If you know the shift in (x,y) direction, let it be (t_x,t_y), you
can create the transformation matrix, M as follows:

Numpy array of type np.float32 and pass it into cv2.warpAffine() function. See below example for a shift of
(100,50)

img = cv2.imread('messi5.jpg',0)
rows,cols = img.shape

M = np.float32([[1,0,100],[0,1,50]])
dst = cv2.warpAffine(img,M,(cols,rows))
Figure 2: Translation programming

1. Type the command in Figure 2 in xxxx.py

2. Save as xxxx.py.

3. Run the program.

Page 5
Lab 3: Geometric transformation with OpenCV

4. Save the result and explain the program and results.

Page 6
Lab 3: Geometric transformation with OpenCV

EXERCISES 3

Rotation
Rotation of an image for an angle θ is achieved by the transformation matrix of the form

But OpenCV provides scaled rotation with adjustable center of rotation so that you can rotate at any location
you prefer. Modified transformation matrix is given by

where:

To find this transformation matrix, OpenCV provides a function, cv2.getRotationMatrix2D. Check below
example which rotates the image by 90 degree with respect to center without any scaling.

rows,cols = img.shape

M = cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
dst = cv2.warpAffine(img,M,(cols,rows))
Figure 3: Rotation programming

1. Type the command in Figure 3 in yyyy.py

2. Save as yyyy.py.

3. Run the program.

Page 7
Lab 3: Geometric transformation with OpenCV

4. Save the result and explain the program and results.

Page 8
Lab 3: Geometric transformation with OpenCV

EXERCISES 4

Figure 4: Flipping programming

Page 9
Lab 3: Geometric transformation with OpenCV

EXERCISES 5

Page 10
Lab 3: Geometric transformation with OpenCV

Page 11
Lab 3: Geometric transformation with OpenCV

Figure 5: crop programming

EXERCISES 6

Write a Python program to capture video from camera

1. Type the program in figure 2 in notepad++/python shell.

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
# Capture frame-by-frame
ret, frame = cap.read()

# Our operations on the frame come here


gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Display the resulting frame


cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# When everything done, release the capture


cap.release()
cv2.destroyAllWindows()

Figure 6: Programming to capture the video

2. Save as xxx.py.

3. Run the program.

4. Save the result and explain the program and results. Write a comment for each line of the program.

Task1
Write a program to reduce the size of the given image to half and rotate it 180 and shift it by 100 to the right and 100
down. Next crop a large drop of water. Lastly save the image in PNG format. The image link is given :

https://static.packt-cdn.com/products/9781800201774/graphics/image/B16118_02_20.jpg . (20 marks)

Page 12
Lab 3: Geometric transformation with OpenCV

Page 13
Lab 3: Geometric transformation with OpenCV

Discussion

Conclusion

Page 14

You might also like