Commit 7f117973 authored by Virginie Uhlmann's avatar Virginie Uhlmann
Browse files

fixed bugs in open curves

parent 1853d65b
Pipeline #178085 failed with stage
in 11 minutes and 19 seconds
......@@ -187,16 +187,31 @@ class SplineCurve:
if even:
ts = self._get_even_sampling_t_range(range_length)
else:
ts = np.linspace(0, float(self.M), num=range_length, endpoint=False)
wrapped_indices = np.array(
[
if self.closed:
ts = np.linspace(0, float(self.M), num=range_length, endpoint=False)
else:
ts = np.linspace(0, float(self.M - 1), num=range_length, endpoint=True)
if self.closed:
wrapped_indices = np.array(
[
self._wrapIndex(t, k)
for k in range(self.M)
[
self._wrapIndex(t, k)
for k in range(self.M)
]
for t in ts
]
for t in ts
]
)
)
else:
wrapped_indices = np.array(
[
[
t - (k - self.halfSupport)
for k in range(self.M+int(self.basis.support))
]
for t in ts
]
)
vfunc = np.vectorize(self.basis.firstDerivativeValue
if derivative else self.basis.value)
phi = vfunc(wrapped_indices)
......@@ -211,6 +226,11 @@ class SplineCurve:
@param even: if True, points on the curve are sampled evenly.
"""
contour_size = self.M * sampling_rate
if self.closed:
contour_size = self.M * sampling_rate
else:
contour_size = (sampling_rate * (self.M - 1)) + 1
phi = self._get_phi(range_length=contour_size, even=even)
contour_points = phi @ self.coefs
return contour_points
......@@ -274,6 +294,10 @@ class SplineCurve:
if self.closed:
filter = self.basis.filterPeriodic
else:
knots = np.vstack((knots,knots[-1]))
knots = np.vstack((knots,knots[-1]))
knots = np.vstack((knots[0],knots))
knots = np.vstack((knots[0],knots))
filter = self.basis.filterSymmetric
if len(knots.shape) > 2:
......@@ -295,7 +319,12 @@ class SplineCurve:
def setCoefsFromDenseContour(self, contourPoints):
N = len(contourPoints)
phi = np.zeros((N, self.M))
if self.closed:
phi = np.zeros((N, self.M))
else:
phi = np.zeros((N, self.M + int(self.basis.support)))
if len(contourPoints.shape) == 1:
r = np.zeros((N))
elif len(contourPoints.shape) == 2:
......@@ -312,36 +341,53 @@ class SplineCurve:
for i in range(0, N):
r[i] = contourPoints[i]
if i == 0:
t = 0
elif t < extraPoints:
t += 1.0 / (samplingRate + 1.0)
if i/samplingRate < extraPoints:
t = i/(samplingRate + 1.0)
else:
t += 1.0 / samplingRate
for k in range(0, self.M):
tval = self._wrapIndex(t, k)
if -self.halfSupport < tval < self.halfSupport:
basisFactor = self.basis.value(tval)
else:
basisFactor = 0.0
t = i/samplingRate
phi[i, k] += basisFactor
if self.closed:
for k in range(0, self.M):
tval = self._wrapIndex(t, k)
if -self.halfSupport < tval < self.halfSupport:
basisFactor = self.basis.value(tval)
else:
basisFactor = 0.0
phi[i, k] += basisFactor
else:
for k in range(0, self.M + int(self.basis.support)):
tval = t - (k - self.halfSupport)
if -self.halfSupport < tval < self.halfSupport:
basisFactor = self.basis.value(tval)
else:
basisFactor = 0.0
phi[i, k] += basisFactor
if len(contourPoints.shape) == 1:
c = np.linalg.lstsq(phi, r, rcond=None)
self._coefs = np.zeros([self.M])
for k in range(0, self.M):
self._coefs[k] = c[0][k]
if self.closed:
self._coefs = np.zeros([self.M])
for k in range(0, self.M):
self._coefs[k] = c[0][k]
else:
self._coefs = np.zeros([self.M + int(self.basis.support)])
for k in range(0, self.M + int(self.basis.support)):
self._coefs[k] = c[0][k]
elif len(contourPoints.shape) == 2:
if contourPoints.shape[1] == 2:
cX = np.linalg.lstsq(phi, r[:, 0], rcond=None)
cY = np.linalg.lstsq(phi, r[:, 1], rcond=None)
self._coefs = np.zeros([self.M, 2])
for k in range(0, self.M):
self._coefs[k] = np.array([cX[0][k], cY[0][k]])
if self.closed:
self._coefs = np.zeros([self.M, 2])
for k in range(0, self.M):
self._coefs[k] = np.array([cX[0][k], cY[0][k]])
else:
self._coefs = np.zeros([self.M + int(self.basis.support), 2])
for k in range(0, self.M + int(self.basis.support)):
self._coefs[k] = np.array([cX[0][k], cY[0][k]])
def setCoefsFromBinaryMask(self, binaryMask):
from skimage import measure
......@@ -457,7 +503,10 @@ class SplineCurve:
if self.coefs is None:
raise RuntimeError(SplineCurve._no_coefs_message)
ts = np.array([self._wrapIndex(t, k) for k in range(self.M)])
if self.closed:
ts = np.array([self._wrapIndex(t, k) for k in range(self.M)])
else:
ts = np.array([t - (k - self.halfSupport) for k in range(self.M + int(self.basis.support))])
f = self.basis.firstDerivativeValue if dt else self.basis.value
vec_f = np.vectorize(f)
phi = vec_f(ts)
......@@ -465,8 +514,6 @@ class SplineCurve:
return value
def _wrapIndex(self, t, k):
if not self.closed:
return t - k
t_left = t - self.halfSupport
t_right = t + self.halfSupport
if k < t_left <= k + self.M <= t_right:
......@@ -544,7 +591,10 @@ class HermiteSplineCurve(SplineCurve):
value = 0.0
for k in range(0, self.M):
tval = self._wrapIndex(t, k)
if self.closed:
tval = self._wrapIndex(t, k)
else:
tval = t - k
if -self.halfSupport < tval < self.halfSupport:
if dt:
splineValue = self.basis.firstDerivativeValue(tval)
......@@ -614,16 +664,23 @@ class SplineCurveDebug(SplineCurve):
raise RuntimeError(SplineCurve._no_coefs_message)
value = 0.0
for k in range(0, self.M):
if self.closed:
if self.closed:
for k in range(0, self.M):
tval = self._wrapIndex(t, k)
else:
tval = t - k
if -self.halfSupport < tval < self.halfSupport:
if dt:
splineValue = self._basis.firstDerivativeValue(tval)
else:
splineValue = self._basis.value(tval)
value += self.coefs[k] * splineValue
if -self.halfSupport < tval < self.halfSupport:
if dt:
splineValue = self.basis.firstDerivativeValue(tval)
else:
splineValue = self.basis.value(tval)
value += self.coefs[k] * splineValue
else:
for k in range(0, self.M + int(self.basis.support)):
tval = t - (k - self.halfSupport)
if -self.halfSupport < tval < self.halfSupport:
if dt:
splineValue = self.basis.firstDerivativeValue(tval)
else:
splineValue = self.basis.value(tval)
value += self.coefs[k] * splineValue
return value
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment