Browse Source

add points related utils

quarrying 3 years ago
parent
commit
243fdc16a3

+ 1 - 0
khandy/__init__.py

@@ -10,5 +10,6 @@ from .utils_others import *
 
 from .boxes import *
 from .image import *
+from .points import *
 
 from .version import *

+ 2 - 0
khandy/points/__init__.py

@@ -0,0 +1,2 @@
+from .pts_letterbox import *
+from .pts_transform_scale import *

+ 19 - 0
khandy/points/pts_letterbox.py

@@ -0,0 +1,19 @@
+__all__ = ['letterbox_2d_points', 'unletterbox_2d_points']
+
+
+def letterbox_2d_points(points, scale=1.0, pad_left=0, pad_top=0, copy=True):
+    if copy:
+        points = points.copy()
+    points[..., 0::2] = points[..., 0::2] * scale + pad_left
+    points[..., 1::2] = points[..., 1::2] * scale + pad_top
+    return points
+    
+    
+def unletterbox_2d_points(points, scale=1.0, pad_left=0, pad_top=0, copy=True):
+    if copy:
+        points = points.copy()
+        
+    points[..., 0::2] = (points[..., 0::2] - pad_left) / scale
+    points[..., 1::2] = (points[..., 1::2] - pad_top) / scale
+    return points
+    

+ 33 - 0
khandy/points/pts_transform_scale.py

@@ -0,0 +1,33 @@
+import numpy as np
+
+__all__ = ['scale_2d_points']
+
+
+def scale_2d_points(points, x_scale=1, y_scale=1, x_center=0, y_center=0, copy=True):
+    """Scale 2d points.
+    
+    Args:
+        points: (..., 2N)
+        x_scale: scale factor in x dimension
+        y_scale: scale factor in y dimension
+        x_center: scale center in x dimension
+        y_center: scale center in y dimension
+    """
+    points = np.array(points, dtype=np.float32, copy=copy)
+    x_scale = np.asarray(x_scale, np.float32)
+    y_scale = np.asarray(y_scale, np.float32)
+    x_center = np.asarray(x_center, np.float32)
+    y_center = np.asarray(y_center, np.float32)
+    
+    x_shift = 1 - x_scale
+    y_shift = 1 - y_scale
+    x_shift *= x_center
+    y_shift *= y_center
+    
+    points[..., 0::2] *= x_scale
+    points[..., 1::2] *= y_scale
+    points[..., 0::2] += x_shift
+    points[..., 1::2] += y_shift
+    return points
+    
+