From 3fa93da5e7a7cfda502ce167eded7a288cb2b1c7 Mon Sep 17 00:00:00 2001 From: Colombo Date: Fri, 20 Mar 2020 11:37:42 +0400 Subject: [PATCH] upd --- core/imagelib/filters.py | 2 +- core/imagelib/sd/__init__.py | 3 ++- core/imagelib/sd/calc.py | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 core/imagelib/sd/calc.py diff --git a/core/imagelib/filters.py b/core/imagelib/filters.py index 2a59069..961f882 100644 --- a/core/imagelib/filters.py +++ b/core/imagelib/filters.py @@ -30,7 +30,7 @@ def apply_random_hsv_shift(img, mask=None, rnd_state=None): h, s, v = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV)) h = ( h + rnd_state.randint(360) ) % 360 s = np.clip ( s + rnd_state.random()-0.5, 0, 1 ) - v = np.clip ( v + rnd_state.random()/2-0.25, 0, 1 ) + v = np.clip ( v + rnd_state.random()-0.5, 0, 1 ) result = np.clip( cv2.cvtColor(cv2.merge([h, s, v]), cv2.COLOR_HSV2BGR) , 0, 1 ) if mask is not None: diff --git a/core/imagelib/sd/__init__.py b/core/imagelib/sd/__init__.py index 7c50477..2eafd4c 100644 --- a/core/imagelib/sd/__init__.py +++ b/core/imagelib/sd/__init__.py @@ -1 +1,2 @@ -from .draw import * \ No newline at end of file +from .draw import * +from .calc import * \ No newline at end of file diff --git a/core/imagelib/sd/calc.py b/core/imagelib/sd/calc.py new file mode 100644 index 0000000..ebc4916 --- /dev/null +++ b/core/imagelib/sd/calc.py @@ -0,0 +1,20 @@ +import numpy as np +import numpy.linalg as npla + +def dist_to_edges(pts, p): + a = pts[:-1,:] + b = pts[1:,:] + edges = np.concatenate( ( pts[:-1,None,:], pts[1:,None,:] ), axis=-2) + + pa = p-a + ba = b-a + + h = np.clip( np.einsum('ij,ij->i', pa, ba) / np.einsum('ij,ij->i', ba, ba), 0, 1 ) + + return npla.norm ( pa - ba*h[...,None], axis=1 ) + +def nearest_edge_id_and_dist(pts, p): + x = dist_to_edges(pts, p) + if len(x) != 0: + return np.argmin(x), np.min(x) + return None, None \ No newline at end of file