Advertisement
dan-masek

Example of simple geometric transformation on a sequence of 2D points.

Apr 2nd, 2021
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4. import matplotlib.lines as mlines
  5.  
  6.  
  7. points = np.array([[4,10], [7,8], [10,5], [9,9], [13,9], [15,10]], dtype=np.float32)
  8.  
  9.  
  10. def plot(points):
  11.     plt.axis([0, 20, 0, 20])
  12.  
  13.     plt.gca().add_line(mlines.Line2D([0, 20], [10, 10]))
  14.     plt.gca().add_line(mlines.Line2D([10, 10], [0, 20]))
  15.     plt.gca().add_line(mlines.Line2D([0, 20], [0, 20]))
  16.     plt.gca().add_line(mlines.Line2D([20, 0], [0, 20]))
  17.     plt.gca().invert_yaxis()
  18.  
  19.     plt.scatter(points[:,0], points[:,1], marker='o', c='r')
  20.     for point in points:
  21.         plt.gca().annotate('(%d,%d)' % tuple(point) , tuple(point + (0.3,-0.3)))
  22.  
  23.  
  24. m_1 = cv2.getRotationMatrix2D((10,10), -45, 1.0)
  25. m_2 = cv2.getRotationMatrix2D((10,10), 45, 1.0)
  26.  
  27. rot_1 = cv2.transform(points.reshape(-1,1,2), m_1).reshape(-1,2)
  28. rot_2 = cv2.transform(points.reshape(-1,1,2), m_2).reshape(-1,2)
  29.  
  30. plt.figure(1)
  31. plt.subplot(131)
  32. plot(points)
  33. plt.subplot(132)
  34. plot(rot_1)
  35. plt.subplot(133)
  36. plot(rot_2)
  37. plt.show()
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement