How to do it

You need to complete the following steps:

  1. Import the necessary modules:
import cv2import numpy as np
  1. Load the precomputed homography and camera parameters:
data = np.load('../data/rotational_homography.npy').item()H, K = data['H'], data['K']
  1. Factor out the camera parameters from the homography transformation:
H_ = np.linalg.inv(K) @ H @ K
  1. Compute the approximate rotation matrix:
w, u, vt = cv2.SVDecomp(H_)R = u @ vtif cv2.determinant(R) < 0:    R *= 1
  1. Convert the rotation matrix to the rotation vector:
rvec = cv2.Rodrigues(R)[0]
  1. Print the results:
print('Rotation vector:')print(rvec)

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.