Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

__init__.py 2.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  1. import math
  2. import cv2
  3. import numpy as np
  4. import numpy.linalg as npla
  5. from .umeyama import umeyama
  6. def get_power_of_two(x):
  7. i = 0
  8. while (1 << i) < x:
  9. i += 1
  10. return i
  11. def rotationMatrixToEulerAngles(R) :
  12. sy = math.sqrt(R[0,0] * R[0,0] + R[1,0] * R[1,0])
  13. singular = sy < 1e-6
  14. if not singular :
  15. x = math.atan2(R[2,1] , R[2,2])
  16. y = math.atan2(-R[2,0], sy)
  17. z = math.atan2(R[1,0], R[0,0])
  18. else :
  19. x = math.atan2(-R[1,2], R[1,1])
  20. y = math.atan2(-R[2,0], sy)
  21. z = 0
  22. return np.array([x, y, z])
  23. def polygon_area(x,y):
  24. return 0.5*np.abs(np.dot(x,np.roll(y,1))-np.dot(y,np.roll(x,1)))
  25. def rotate_point(origin, point, deg):
  26. """
  27. Rotate a point counterclockwise by a given angle around a given origin.
  28. The angle should be given in radians.
  29. """
  30. ox, oy = origin
  31. px, py = point
  32. rad = deg * math.pi / 180.0
  33. qx = ox + math.cos(rad) * (px - ox) - math.sin(rad) * (py - oy)
  34. qy = oy + math.sin(rad) * (px - ox) + math.cos(rad) * (py - oy)
  35. return np.float32([qx, qy])
  36. def transform_points(points, mat, invert=False):
  37. if invert:
  38. mat = cv2.invertAffineTransform (mat)
  39. points = np.expand_dims(points, axis=1)
  40. points = cv2.transform(points, mat, points.shape)
  41. points = np.squeeze(points)
  42. return points
  43. def transform_mat(mat, res, tx, ty, rotation, scale):
  44. """
  45. transform mat in local space of res
  46. scale -> translate -> rotate
  47. tx,ty float
  48. rotation int degrees
  49. scale float
  50. """
  51. lt, rt, lb, ct = transform_points ( np.float32([(0,0),(res,0),(0,res),(res / 2, res/2) ]),mat, True)
  52. hor_v = (rt-lt).astype(np.float32)
  53. hor_size = npla.norm(hor_v)
  54. hor_v /= hor_size
  55. ver_v = (lb-lt).astype(np.float32)
  56. ver_size = npla.norm(ver_v)
  57. ver_v /= ver_size
  58. bt_diag_vec = (rt-ct).astype(np.float32)
  59. half_diag_len = npla.norm(bt_diag_vec)
  60. bt_diag_vec /= half_diag_len
  61. tb_diag_vec = np.float32( [ -bt_diag_vec[1], bt_diag_vec[0] ] )
  62. rt = ct + bt_diag_vec*half_diag_len*scale
  63. lb = ct - bt_diag_vec*half_diag_len*scale
  64. lt = ct - tb_diag_vec*half_diag_len*scale
  65. rt[0] += tx*hor_size
  66. lb[0] += tx*hor_size
  67. lt[0] += tx*hor_size
  68. rt[1] += ty*ver_size
  69. lb[1] += ty*ver_size
  70. lt[1] += ty*ver_size
  71. rt = rotate_point(ct, rt, rotation)
  72. lb = rotate_point(ct, lb, rotation)
  73. lt = rotate_point(ct, lt, rotation)
  74. return cv2.getAffineTransform( np.float32([lt, rt, lb]), np.float32([ [0,0], [res,0], [0,res] ]) )
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...