def __init__( self, models, angs=30, wmax=10, verbose=False, ): model, self.target, self.match = mkMatcher(angs, wmax) self.mod = [model(m) for m in readModels(models)] for k, m in enumerate(self.mod): m.id = k try: locs = np.loadtxt(models + '/locations.txt').reshape(-1, 7) if verbose: print(locs) T = [] for ax, ay, az, dx, dy, dz, s in locs: t = rotation(vec(1, 0, 0), np.radians(ax), homog=True) t = rotation(vec(0, 1, 0), np.radians(ay), homog=True) @ t t = rotation(vec(0, 0, 1), np.radians(az), homog=True) @ t t = scale(vec(s, s, s)) @ t t = desp(vec(dx, dy, dz)) @ t T.append(t) except: T = [np.eye(4) for _ in self.mod] self.p3d = [ move(t, (m.original * [[1, -1]])) for t, m in zip(T, self.mod) ] if verbose: print(self.p3d)
def mymoments(c): cf = c.astype(float) cr = np.roll(cf, -1, 0) x1 = cf[:, 0] y1 = cf[:, 1] x2 = cr[:, 0] y2 = cr[:, 1] dx = x2 - x1 dy = y2 - y1 x12 = x1**2 x22 = x2**2 y12 = y1**2 y22 = y2**2 q1 = 2 * y1 + y2 q2 = y1 + 2 * y2 s0 = x1 * y2 - x2 * y1 sx = np.sum(2 * x1 * x2 * dy - x22 * q1 + x12 * q2) / 12 sy = np.sum(-2 * y1 * y2 * dx + y22 * (2 * x1 + x2) - y12 * (2 * x2 + x1)) / 12 sx2 = np.sum((x12 * x2 + x1 * x22) * dy + (x1**3 - x2**3) * (y1 + y2)) / 12 sy2 = np.sum(-(y12 * y2 + y1 * y22) * dx - (y1**3 - y2**3) * (x1 + x2)) / 12 sxy = np.sum(s0 * (x1 * q1 + x2 * q2)) / 24 s = np.sum(s0) / 2 mx = sx / s my = sy / s cx = sx2 / s - mx**2 cy = sy2 / s - my**2 cxy = sxy / s - mx * my (l1, l2, ad) = eig22(cx, cy, cxy) return (vec(mx, my), (np.sqrt(l1), np.sqrt(l2), ad))
def mymoments(c): m = cv.moments(c.astype(np.float32)) # int32, float32, but not float64! s = m['m00'] mx = m['m10'] / s my = m['m01'] / s cx = m['mu20'] / s cy = m['mu02'] / s cxy = m['mu11'] / s (l1, l2, ad) = eig22(cx, cy, cxy) return (vec(mx, my), (np.sqrt(l1), np.sqrt(l2), ad))
def __init__(self, x, lab='?', full=False, eps_w=10, **args): self.orig = x (A, self.s1, self.s2, ad), H, W = whitenedMoments(x, th=eps_w) self.H = H self.W = W self.SBar, self.S, self.K = KSsignature(W) self.Ts = [ np.dot(t, H) for t in canonicTransforms((self.SBar, self.K), **args) ] self.can = [warpW(x, T, sz=(100, 100), sigma=3) for T in self.Ts] self.dtc = [dt(c) for c in self.can] self.auxh1 = [ np.hstack([self.can[k], self.dtc[k]]) for k in range(len(self.can)) ] self.auxh2 = [ np.hstack([self.dtc[k], self.can[k]]) for k in range(len(self.can)) ] pl = min(x.shape) self.pad = np.pad(x, pl, 'constant') if full: self.dto = dt(self.pad) / self.s1 self.dtco = [ warpW(self.dto, T, sz=(100, 100), sigma=3, offset=pl, borderMode=cv.BORDER_REPLICATE) for T in self.Ts ] self.auxh3 = [ np.hstack([self.can[k], self.dtco[k]]) for k in range(len(self.can)) ] self.auxh4 = [ np.hstack([self.dtco[k], self.can[k]]) for k in range(len(self.can)) ] conts = extractContours(255 - 255 * self.pad.astype(np.uint8), minarea=0, minredon=0, reduprec=0.5) assert len(conts) > 0, (len(conts), lab) self.wcont = sorted(conts, key=len)[-1] self.invar = invar(spectralFeat(self.wcont)) r, s, t, g = orientability((self.SBar, self.S, self.K)) self.skf = vec(self.S.max(), s, (self.K.min() + self.K.max()) / 2, r, t / r, g / r) self.lab = lab
def autoscale(cont): (x1, y1), (x2, y2) = cont.min(0), cont.max(0) s = max(x2 - x1, y2 - y1) c = vec(x1 + x2, y1 + y2) / 2 h = np.dot(scale(1 / vec(s, s)), desp(-c)) return htrans(h, cont)
def whitener(cont): (c, (s1, s2, a)) = mymoments(cont) return np.dot(np.dot(rot3(a), scale(1 / vec(s1, s2))), np.dot(rot3(-a), desp(-c)))