def global_rigid_transformation(pose, J, kintree_table): def _rodrigues(x): return Rodrigues(x) results = {} pose = pose.reshape((-1, 3)) id_to_col = {kintree_table[1, i]: i for i in range(kintree_table.shape[1])} parent = {i: id_to_col[kintree_table[0, i]] for i in range(1, kintree_table.shape[1])} results[0] = with_zeros( ch.hstack((_rodrigues(pose[0, :]), J[0, :].reshape((3, 1))))) for i in range(1, kintree_table.shape[1]): results[i] = results[parent[i]].dot( with_zeros(ch.hstack((_rodrigues(pose[i, :]), ((J[i, :] - J[parent[i], :]).reshape((3, 1))) )))) results = [results[i] for i in sorted(results.keys())] results_global = results results2 = [results[i] - (pack( results[i].dot(ch.concatenate(((J[i, :]), 0)))) ) for i in range(len(results))] results = results2 result = ch.dstack(results) return result, results_global
def _global_rigid_transformation(self): results = {} pose = self.pose.reshape((-1, 3)) parent = { i: self.kintree_table[0, i] for i in range(1, self.kintree_table.shape[1]) } with_zeros = lambda x: ch.vstack((x, ch.array([[0.0, 0.0, 0.0, 1.0]]))) pack = lambda x: ch.hstack([ch.zeros((4, 3)), x.reshape((4, 1))]) results[0] = with_zeros( ch.hstack((Rodrigues(pose[0, :]), self.J[0, :].reshape((3, 1))))) for i in range(1, self.kintree_table.shape[1]): results[i] = results[parent[i]].dot( with_zeros( ch.hstack(( Rodrigues(pose[i, :]), # rotation around bone endpoint (self.J[i, :] - self.J[parent[i], :]).reshape( (3, 1)) # bone )))) results = [results[i] for i in sorted(results.keys())] results_global = results # subtract rotated J position results2 = [ results[i] - (pack(results[i].dot(ch.concatenate((self.J[i, :], [0]))))) for i in range(len(results)) ] result = ch.dstack(results2) return result, results_global
def _set_up(self): self.v_shaped = self.shapedirs.dot(self.betas) + self.v_template self.v_shaped_personal = self.v_shaped + self.v_personal if sp.issparse(self.J_regressor): self.J = sp_dot(self.J_regressor, self.v_shaped) else: self.J = ch.sum(self.J_regressor.T.reshape(-1, 1, 24) * self.v_shaped.reshape(-1, 3, 1), axis=0).T self.v_posevariation = self.posedirs.dot( posemap(self.bs_type)(self.pose)) self.v_poseshaped = self.v_shaped_personal + self.v_posevariation self.A, A_global = self._global_rigid_transformation() self.Jtr = ch.vstack([g[:3, 3] for g in A_global]) self.J_transformed = self.Jtr + self.trans.reshape((1, 3)) self.V = self.A.dot(self.weights.T) rest_shape_h = ch.hstack( (self.v_poseshaped, ch.ones((self.v_poseshaped.shape[0], 1)))) self.v_posed = ch.sum(self.V.T * rest_shape_h.reshape(-1, 4, 1), axis=1)[:, :3] self.v = self.v_posed + self.trans
def _set_up(self): self.v_shaped = self.shapedirs.dot(self.betas) + self.v_template body_height = (self.v_shaped[2802, 1] + self.v_shaped[6262, 1]) - ( self.v_shaped[2237, 1] + self.v_shaped[6728, 1]) # print('111111111111111111111111111111111111111') # print(np.array(body_height)[0]) # print(type(body_height)) # print('22222222222222222222222222222222222222222') self.scale = 1.66 / np.array(body_height)[0] self.v_shaped_personal = self.scale * self.v_shaped + self.v_personal if sp.issparse(self.J_regressor): self.J = self.scale * sp_dot(self.J_regressor, self.v_shaped) else: self.J = self.scale * ch.sum(self.J_regressor.T.reshape(-1, 1, 24) * self.v_shaped.reshape(-1, 3, 1), axis=0).T self.v_posevariation = self.posedirs.dot( posemap(self.bs_type)(self.pose)) self.v_poseshaped = self.v_shaped_personal + self.v_posevariation self.A, A_global = self._global_rigid_transformation() self.Jtr = ch.vstack([g[:3, 3] for g in A_global]) self.J_transformed = self.Jtr + self.trans.reshape((1, 3)) self.V = self.A.dot(self.weights.T) rest_shape_h = ch.hstack( (self.v_poseshaped, ch.ones((self.v_poseshaped.shape[0], 1)))) self.v_posed = ch.sum(self.V.T * rest_shape_h.reshape(-1, 4, 1), axis=1)[:, :3] self.v = self.v_posed + self.trans
def r_and_derivatives(self): tmp = self.v.dot(Rodrigues(self.rt)) + self.t return ch.hstack(( col(2. / (self.right - self.left) * tmp[:, 0] - (self.right + self.left) / (self.right - self.left) + 1.) * self.width / 2., col(2. / (self.bottom - self.top) * tmp[:, 1] - (self.bottom + self.top) / (self.bottom - self.top) + 1.) * self.height / 2., ))
def setup_silhouette_obj(silhs, rends, f): n_model = [ch.sum(rend[:, :, 0] > 0) for rend in rends] dist_tsf = [cv2.distanceTransform(np.uint8(1 - silh), cv2.DIST_L2, cv2.DIST_MASK_PRECISE) for silh in silhs] # Make sigma proportional to image area. # This gives radius 400 for 960 x 1920 image. sigma_ratio = 0.2727 sigma = [np.sqrt(sigma_ratio * (silh.shape[0] * silh.shape[1]) / np.pi) for silh in silhs] # Model2silhouette ==> Consistency (contraction) def obj_m2s(w, i): return w * (GMOf(rends[i][:, :, 0] * dist_tsf[i], sigma[i]) / np.sqrt(n_model[i])) # silhouette error term (scan-to-model) ==> Coverage (expansion) coords = [np.fliplr(np.array(np.where(silh > 0)).T) + 0.5 for silh in silhs]# is this offset needed? scan_flat_v = [np.hstack((coord, ch.zeros(len(coord)).reshape((-1, 1)))) for coord in coords] scan_flat = [Mesh(v=sflat_v, f=[]) for sflat_v in scan_flat_v] # 2d + all 0. sv_flat = [ch.hstack((rend.camera, ch.zeros(len(rend.v)).reshape((-1, 1)))) for rend in rends] for i in range(len(rends)): sv_flat[i].f = f def obj_s2m(w, i): from sbody.mesh_distance import ScanToMesh return w * ch.sqrt(GMOf(ScanToMesh(scan_flat[i], sv_flat[i], f), sigma[i])) # For vis for i in range(len(rends)): scan_flat[i].vc = np.tile(np.array([0, 0, 1]), (len(scan_flat[i].v), 1)) return obj_m2s, obj_s2m, dist_tsf
def SecondFundamentalForm(v, f): from chumpy import hstack, vstack from chumpy.linalg import Pinv nbrs = MatVecMult(FirstEdgesMtx(v, f, want_big=True), v.ravel()).reshape( (-1, 3)) b0 = VertNormals(f=f, v=v) b1 = NormalizedNx3(CrossProduct(b0, nbrs - v)).reshape((-1, 3)) b2 = NormalizedNx3(CrossProduct(b0, b1)).reshape((-1, 3)) cnct = get_vert_connectivity(np.asarray(v), f) ffs = [] for i in range(v.size / 3): nbrs = v[np.nonzero(np.asarray(cnct[i].todense()).ravel())[0]] - row( v[i]) us = nbrs.dot(b2[i]) vs = nbrs.dot(b1[i]) hs = nbrs.dot(b0[i]) coeffs = Pinv( hstack((col((us * .5)**2), col(us * vs), col( (vs * .5)**2)))).dot(hs) ffs.append(row(coeffs)) # if i == 3586: # import pdb; pdb.set_trace() ffs = vstack(ffs) return ffs
def run(self, beta=None, theta=None, garment_d=None, garment_class=None): """Outputs body and garment of specified garment class given theta, beta and displacements.""" if beta is not None: self.smpl_base.betas[:beta.shape[0]] = beta else: self.smpl_base.betas[:] = 0 if theta is not None: self.smpl_base.pose[:] = theta else: self.smpl_base.pose[:] = 0 self.smpl_base.v_personal[:] = 0 if garment_d is not None and garment_class is not None: if 'skirt' not in garment_class: vert_indices = self.class_info[garment_class]['vert_indices'] f = self.class_info[garment_class]['f'] self.smpl_base.v_personal[vert_indices] = garment_d garment_m = Mesh(v=self.smpl_base.r[vert_indices], f=f) else: # vert_indices = self.class_info[garment_class]['vert_indices'] f = self.class_info[garment_class]['f'] A = self.smpl_base.A.reshape((16, 24)).T skirt_V = self.skirt_skinning.dot(A).reshape((-1, 4, 4)) verts = self.skirt_weight.dot(self.smpl_base.v_poseshaped) verts = verts + garment_d verts_h = ch.hstack((verts, ch.ones((verts.shape[0], 1)))) verts = ch.sum(skirt_V * verts_h.reshape(-1, 1, 4), axis=-1)[:, :3] garment_m = Mesh(v=verts, f=f) else: garment_m = None self.smpl_base.v_personal[:] = 0 body_m = Mesh(v=self.smpl_base.r, f=self.smpl_base.f) return body_m, garment_m
def __init__(self, t, rod, rad, length): self.t = t # translation of the axis self.rod = rod # rotation of the axis in Rodrigues form self.rad = rad # radious of the capsule self.length = length # length of the axis axis0 = ch.vstack([0, ch.abs(self.length), 0]) self.axis = ch.vstack((t.T, (t + Rodrigues(rod).dot(axis0)).T)) v0 = ch.hstack([v[:26].T*rad, (v[26:].T*rad)+ axis0]) self.v = ((t + Rodrigues(rod).dot(v0)).T) self.set_sphere_centers()
def face_bases(v, f): t1 = TriEdges(f, 1, 0, v).reshape((-1,3)) t2 = TriEdges(f, 2, 0, v).reshape((-1,3)) #t3 = NormalizedNx3(CrossProduct(t1, t2)).reshape((-1,3)) #t3 = CrossProduct(t1, t2).reshape((-1,3)) # Problem: cross-product is proportional in length to len(t1)*len(t2) # Solution: divide by sqrt(sqrt(len(cross-product))) t3 = CrossProduct(t1, t2).reshape((-1,3)); t3 = t3 / col(ch.sum(t3**2., axis=1)**.25) result = ch.hstack((t1, t2, t3)).reshape((-1,3,3)) return result
def test_stacking(self): a1 = ch.Ch(np.arange(10).reshape(2, 5)) b1 = ch.Ch(np.arange(20).reshape(4, 5)) c1 = ch.vstack((a1, b1)) c1_check = np.vstack((a1.r, b1.r)) residuals1 = (c1_check - c1.r).ravel() a2 = ch.Ch(np.arange(10).reshape(5, 2)) b2 = ch.Ch(np.arange(20).reshape(5, 4)) c2 = ch.hstack((a2, b2)) c2_check = np.hstack((a2.r, b2.r)) residuals2 = (c2_check - c2.r).ravel() self.assertFalse(np.any(residuals1)) self.assertFalse(np.any(residuals2)) d0 = ch.array(np.arange(60).reshape((10, 6))) d1 = ch.vstack((d0[:4], d0[4:])) d2 = ch.hstack((d1[:, :3], d1[:, 3:])) tmp = d2.dr_wrt(d0).todense() diff = tmp - np.eye(tmp.shape[0]) self.assertFalse(np.any(diff.ravel()))
def global_rigid_transformation(pose, J, kintree_table): results = {} pose = pose.reshape((-1, 3)) id_to_col = {kintree_table[1, i]: i for i in range(kintree_table.shape[1])} parent = { i: id_to_col[kintree_table[0, i]] for i in range(1, kintree_table.shape[1]) } def with_zeros(x): return ch.vstack((x, ch.array([[0.0, 0.0, 0.0, 1.0]]))) results[0] = with_zeros( ch.hstack((Rodrigues(pose[0, :]), J[0, :].reshape((3, 1))))) for i in range(1, kintree_table.shape[1]): results[i] = results[parent[i]].dot( with_zeros( ch.hstack((Rodrigues(pose[i, :]), ((J[i, :] - J[parent[i], :]).reshape((3, 1))))))) def pack(x): return ch.hstack([np.zeros((4, 3)), x.reshape((4, 1))]) results = [results[i] for i in sorted(results.keys())] results_global = results if True: results2 = [ results[i] - pack(results[i].dot(ch.concatenate(((J[i, :]), 0)))) for i in range(len(results)) ] results = results2 result = ch.dstack(results) return result, results_global
def __init__(self, t, rod, rad, length): assert (hasattr(t, 'dterms')) # the translation should be a chumpy object (differentiable wrt shape) self.t = t # translation of the axis self.rod = rod # rotation of the axis in Rodrigues form # the radius should be a chumpy object (differentiable wrt shape) assert (hasattr(rad, 'dterms')) self.rad = rad # radius of the capsule # the length should be a chumpy object (differentiable wrt shape) assert (hasattr(length, 'dterms')) self.length = length # length of the axis axis0 = ch.vstack([0, ch.abs(self.length), 0]) self.axis = ch.vstack((t.T, (t + Rodrigues(rod).dot(axis0)).T)) v0 = ch.hstack([v[:26].T * rad, (v[26:].T * rad) + axis0]) self.v = ((t + Rodrigues(rod).dot(v0)).T) self.set_sphere_centers()
def SecondFundamentalForm(v, f): from chumpy import hstack, vstack from chumpy.linalg import Pinv nbrs = MatVecMult(FirstEdgesMtx(v, f, want_big=True), v.ravel()).reshape((-1,3)) b0 = NormalizedNx3(VertNormalsScaled(f=f, v=v)).reshape((-1,3)) b1 = NormalizedNx3(CrossProduct(b0, nbrs-v)).reshape((-1,3)) b2 = NormalizedNx3(CrossProduct(b0, b1)).reshape((-1,3)) cnct = get_vert_connectivity(v.r, f) ffs = [] for i in range(v.size/3): nbrs = v[np.nonzero(np.asarray(cnct[i].todense()).ravel())[0]] - row(v[i]) us = nbrs.dot(b2[i]) vs = nbrs.dot(b1[i]) hs = nbrs.dot(b0[i]) coeffs = Pinv(hstack((col((us*.5)**2), col(us*vs), col((vs*.5)**2)))).dot(hs) ffs.append(row(coeffs)) # if i == 3586: # import pdb; pdb.set_trace() ffs = vstack(ffs) return ffs
def run(self, beta=None, theta=None, garment_d=None, garment_class=None): """Outputs body and garment of specified garment class given theta, beta and displacements.""" if beta is not None: self.smpl_base.betas[:beta.shape[0]] = beta else: self.smpl_base.betas[:] = 0 if theta is not None: self.smpl_base.pose[:] = theta else: self.smpl_base.pose[:] = 0 self.smpl_base.v_personal[:] = 0 if garment_d is not None and garment_class is not None: if 'skirt' not in garment_class: vert_indices = self.class_info[garment_class]['vert_indices'] f = self.class_info[garment_class]['f'] self.smpl_base.v_personal[vert_indices] = garment_d garment_m = Mesh(v=self.smpl_base.r[vert_indices], f=f) else: vert_indices = self.class_info[garment_class]['vert_indices'] f = self.class_info[garment_class]['f'] verts = self.smpl_base.v_poseshaped[vert_indices] + garment_d verts_h = ch.hstack((verts, ch.ones((verts.shape[0], 1)))) verts = ch.sum(self.smpl_base.V.T[vert_indices] * verts_h.reshape(-1, 4, 1), axis=1)[:, :3] # if theta is not None: # rotmat = self.smpl_base.A.r[:, :, 0] # verts_homo = np.hstack( # (verts, np.ones((verts.shape[0], 1)))) # verts = verts_homo.dot(rotmat.T)[:, :3] garment_m = Mesh(v=verts, f=f) else: garment_m = None self.smpl_base.v_personal[:] = 0 body_m = Mesh(v=self.smpl_base.r, f=self.smpl_base.f) return body_m, garment_m
def pack(x): return ch.hstack([ch.zeros((4, 3)), x.reshape((4, 1))])
def pack(x): return ch.hstack([ch.zeros((4, 3)), x.reshape((4, 1))]) results[0] = with_zeros(
def height_predictor(b2m, betas): return ch.hstack((betas.reshape(1, -1), [[1]])).dot(b2m)
def diffHog(image, drconv=None, numOrient=9, cwidth=8, cheight=8): imagegray = 0.3 * image[:, :, 0] + 0.59 * image[:, :, 1] + 0.11 * image[:, :, 2] sy, sx = imagegray.shape # gx = ch.empty(imagegray.shape, dtype=np.double) gx = imagegray[:, 2:] - imagegray[:, :-2] gx = ch.hstack([np.zeros([sy, 1]), gx, np.zeros([sy, 1])]) gy = imagegray[2:, :] - imagegray[:-2, :] # gy = imagegray[:, 2:] - imagegray[:, :-2] gy = ch.vstack([np.zeros([1, sx]), gy, np.zeros([1, sx])]) gx += 1e-5 # gy = imagegray[:-2,1:-1] - imagegray[2:,1:-1] + 0.00001 # gx = imagegray[1:-1,:-2] - imagegray[1:-1, 2:] + 0.00001 distFilter = np.ones([2 * cheight, 2 * cwidth], dtype=np.uint8) distFilter[np.int(2 * cheight / 2), np.int(2 * cwidth / 2)] = 0 distFilter = (cv2.distanceTransform(distFilter, cv2.DIST_L2, 3) - np.max( cv2.distanceTransform(distFilter, cv2.DIST_L2, 3))) / ( -np.max(cv2.distanceTransform(distFilter, cv2.DIST_L2, 3))) magn = ch.sqrt(gy**2 + gx**2) * 180 / np.sqrt(2) angles = ch.arctan(gy / gx) * 180 / np.pi + 90 # meanOrient = np.linspace(0, 180, numOrient) orientations_arr = np.arange(numOrient) meanOrient = orientations_arr / numOrient * 180 fb_resttmp = 1 - ch.abs( ch.expand_dims(angles[:, :], 2) - meanOrient[1:].reshape([1, 1, numOrient - 1])) * numOrient / 180 zeros_rest = np.zeros([sy, sx, numOrient - 1, 1]) fb_rest = ch.max(ch.concatenate([fb_resttmp[:, :, :, None], zeros_rest], axis=3), axis=3) chMinOrient0 = ch.min(ch.concatenate([ ch.abs( ch.expand_dims(angles[:, :], 2) - meanOrient[0].reshape([1, 1, 1]))[:, :, :, None], ch.abs(180 - ch.expand_dims(angles[:, :], 2) - meanOrient[0].reshape([1, 1, 1]))[:, :, :, None] ], axis=3), axis=3) zeros_fb0 = np.zeros([sy, sx, 1]) fb0_tmp = ch.concatenate( [1 - chMinOrient0[:, :] * numOrient / 180, zeros_fb0], axis=2) fb_0 = ch.max(fb0_tmp, axis=2) fb = ch.concatenate([fb_0[:, :, None], fb_rest], axis=2) # fb[:,:,0] = ch.max(1 - ch.abs(ch.expand_dims(angles,2) - meanOrient.reshape([1,1,numOrient]))*numOrient/180,0) # fb = 1./(1. + ch.exp(1 - ch.abs(ch.expand_dims(angles,2) - meanOrient.reshape([1,1,numOrient]))*numOrient/180)) Fb = ch.expand_dims(magn, 2) * fb if drconv is None: drconv = dr_wrt_convolution(Fb[:, :, 0], distFilter) Fs_list = [ convolve2D(x=Fb[:, :, Fbi], filter=distFilter, convolve2DDr=drconv).reshape([Fb.shape[0], Fb.shape[1], 1]) for Fbi in range(numOrient) ] # Fs_list = [scipy.signal.convolve2d(Fb[:,:,Fbi], distFilter).reshape([Fb.shape[0], Fb.shape[1],1]) for Fbi in range(numOrient)] Fs = ch.concatenate(Fs_list, axis=2) # cellCols = np.arange(start=cwidth/2, stop=Fs.shape[1]-cwidth/2 , step=cwidth) # cellRows = np.arange(start=cheight/2, stop=Fs.shape[0]-cheight/2 , step=cheight) Fcells = Fs[0:Fs.shape[0]:cheight, 0:Fs.shape[1]:cwidth, :] epsilon = 1e-5 v = Fcells / ch.sqrt(ch.sum(Fcells**2) + epsilon) # v = Fcells # hog, hogim = skimage.feature.hog(imagegray, orientations=numOrient, pixels_per_cell=(cheight, cwidth), visualise=True) hog_image = HogImage(image=image, hog=Fcells, numOrient=numOrient, cwidth=cwidth, cheight=cheight) # plt.imshow(hog_image) # plt.figure() # plt.imshow(hogim) # ipdb.set_trace() return v, hog_image, drconv