def _instance(self, shape_instance, template): landmarks = template.landmarks["source"].lms reference_frame = build_reference_frame(shape_instance) transform = self.transform(reference_frame.landmarks["source"].lms, landmarks) return template.as_unmasked(copy=False).warp_to_mask(reference_frame.mask, transform, warp_landmarks=True)
def _build_shape_model(self, shapes, scale_index): mean_aligned_shape = mean_pointcloud(align_shapes(shapes)) self.n_landmarks = mean_aligned_shape.n_points self.reference_frame = build_reference_frame(mean_aligned_shape) dense_shapes = densify_shapes(shapes, self.reference_frame, self.transform) # build dense shape model shape_model = build_shape_model(dense_shapes) return shape_model
def _warp_images(self, images, shapes, reference_shape, scale_index, prefix, verbose): reference_frame = build_reference_frame(reference_shape) return warp_images(images, shapes, reference_frame, self.transform, prefix=prefix, verbose=verbose)
def _build_shape_model(self, shapes, scale_index): mean_aligned_shape = mean_pointcloud(align_shapes(shapes)) self.n_landmarks = mean_aligned_shape.n_points self.reference_frame = build_reference_frame(mean_aligned_shape) dense_shapes = densify_shapes(shapes, self.reference_frame, self.transform) # Build dense shape model max_sc = self.max_shape_components[scale_index] return self._shape_model_cls[scale_index](dense_shapes, max_n_components=max_sc)
def _instance(self, scale_index, shape_instance, appearance_instance): template = self.appearance_models[scale_index].mean() landmarks = template.landmarks['source'].lms reference_frame = build_reference_frame(shape_instance) transform = pwa(reference_frame.landmarks['source'].lms, landmarks) return appearance_instance.as_unmasked(copy=False).warp_to_mask( reference_frame.mask, transform, warp_landmarks=True)
def _instance(self, shape_instance, template): landmarks = template.landmarks['source'] reference_frame = build_reference_frame(shape_instance) transform = self.transform( reference_frame.landmarks['source'], landmarks) return template.as_unmasked(copy=False).warp_to_mask( reference_frame.mask, transform, warp_landmarks=True)
def _instance(self, scale_index, shape_instance, appearance_instance): template = self.appearance_models[scale_index].mean() landmarks = template.landmarks['source'].lms reference_frame = build_reference_frame(shape_instance) transform = self.transform( reference_frame.landmarks['source'].lms, landmarks) return appearance_instance.as_unmasked(copy=False).warp_to_mask( reference_frame.mask, transform, warp_landmarks=True)
def grid_view(daams, fitter, final_shape, image=None, angle=0, indexes=None, trilist=None): t = fitter.algorithms[-1] t = t.transform t.set_target(final_shape) sub, sampled = subsample_dense_pointclouds([ PointCloud(daams.reference_frame.landmarks['source'].lms. points[daams.n_landmarks:]) ], daams.reference_frame.shape, mask=daams.reference_frame.mask) index = [] for p in sub[0].points: for i, pp in enumerate( daams.reference_frame.landmarks['source'].lms.points): if np.all(p == pp): index.append(i) if indexes: index = indexes + index else: index = range(daams.n_landmarks) + index tm = TriMesh(daams.reference_frame.landmarks['source'].lms.points[index]) if not trilist is None: tm_fit = TriMesh(t.target.points[index], trilist) else: tm_fit = TriMesh(t.target.points[index], tm.trilist) if image: image = build_reference_frame(final_shape) image.landmarks['DENSE'] = t.target img = image img.landmarks['trim'] = tm_fit img = img.rotate_ccw_about_centre(angle - 360) img = img.crop_to_landmarks_proportion(1, group='trim') img.view_widget() else: tm_fit.view() return tm_fit, t.target
def _warp_template(self, template, group, reference_shape, scale_index, prefix, verbose): reference_frame = build_reference_frame(reference_shape) shape = template.landmarks[group].lms return warp_images([template], [shape], reference_frame, self.transform, prefix=prefix, verbose=verbose)
def rescale_images_to_reference_shape(images, group, reference_shape, tight_mask=True, sd=svs_shape, target_group=None, verbose=False): r""" """ _has_lms_align = False _n_align_points = None _is_mc = False group_align = group _db_path = images[0].path.parent reference_align_shape = reference_shape n_landmarks = reference_shape.n_points # Normalize the scaling of all images wrt the reference_shape size for i in images: if 'LMS' in i.landmarks.keys(): _has_lms_align = True i.landmarks['align'] = i.landmarks['LMS'] if not _n_align_points: _n_align_points = i.landmarks['align'].lms.n_points if _has_lms_align: group_align = 'align' reference_align_shape = PointCloud( reference_shape.points[:_n_align_points]) reference_shape = PointCloud(reference_shape.points[_n_align_points:]) else: group_align = '_nicp' for i in images: source_shape = TriMesh(reference_shape.points) _, points_corr = nicp(source_shape, i.landmarks[group].lms) i.landmarks[group_align] = PointCloud( i.landmarks[group].lms.points[points_corr]) print(' - Normalising') normalized_images = [ i.rescale_to_pointcloud(reference_align_shape, group=group_align) for i in images ] # Global Parameters alpha = 30 pdm = 0 lms_shapes = [i.landmarks[group_align].lms for i in normalized_images] shapes = [i.landmarks[group].lms for i in normalized_images] n_shapes = len(shapes) # Align Shapes Using ICP aligned_shapes, target_shape, _removed_transform, _icp_transform, _icp\ = align_shapes(shapes, reference_shape, lms_shapes=lms_shapes, align_target=reference_align_shape) # Build Reference Frame from Aligned Shapes bound_list = [] for s in [reference_shape] + aligned_shapes.tolist(): bmin, bmax = s.bounds() bound_list.append(bmin) bound_list.append(bmax) bound_list.append(np.array([bmin[0], bmax[1]])) bound_list.append(np.array([bmax[0], bmin[1]])) bound_list = PointCloud(np.array(bound_list)) scales = np.max(bound_list.points, axis=0) - np.min(bound_list.points, axis=0) max_scale = np.max(scales) bound_list = PointCloud( np.array([[max_scale, max_scale], [max_scale, 0], [0, max_scale], [0, 0]])) reference_frame = build_reference_frame(bound_list, boundary=15) # Translation between reference shape and aliened shapes align_centre = target_shape.centre_of_bounds() align_t = Translation(reference_frame.centre() - align_centre) _rf_align = Translation(align_centre - reference_frame.centre()) # Set All True Pixels for Mask reference_frame.mask.pixels = np.ones(reference_frame.mask.pixels.shape, dtype=np.bool) # Create Cache Directory home_dir = os.getcwd() dir_hex = uuid.uuid1() sd_path_in = '{}/shape_discriptor'.format( _db_path) if _db_path else '{}/.cache/{}/sd_training'.format( home_dir, dir_hex) sd_path_out = sd_path_in matE = MatlabExecuter() mat_code_path = '/vol/atlas/homes/yz4009/gitdev/mfsfdev' # Skip building svs is path specified _build_shape_desc(sd_path_in, normalized_images, reference_shape, aligned_shapes, align_t, reference_frame, _icp_transform, _is_mc=_is_mc, group=group, target_align_shape=reference_align_shape, _shape_desc=sd, align_group=group_align, target_group=target_group) # self._build_trajectory_basis(sample_groups, target_shape, # aligned_shapes, dense_reference_shape, align_t) # Call Matlab to Build Flows if not isfile('{}/result.mat'.format(sd_path_in)): print(' - Building Shape Flow') matE.cd(mat_code_path) ext = 'gif' isLms = _has_lms_align + 0 isBasis = 0 fstr = 'gpuDevice(1);' \ 'addpath(\'{0}/{1}\');' \ 'addpath(\'{0}/{2}\');' \ 'build_flow(\'{3}\', \'{4}\', \'{5}\', {6}, {7}, ' \ '{8}, \'{3}/{9}\', {10}, {11}, {14}, {15}, {12}, \'{13}\')'.format( mat_code_path, 'cudafiles', 'tools', sd_path_in, sd_path_out, 'sd_%04d.{}'.format(ext), 0, 1, n_shapes, 'bas.mat', alpha, pdm, 30, 'sd_%04d_lms.pts', isBasis, isLms ) sys.stderr.write(fstr) sys.stderr.write(fstr.replace('build_flow', 'build_flow_test')) p = matE.run_function(fstr) p.wait() else: sd_path_out = sd_path_in # Retrieve Results mat = sio.loadmat('{}/result.mat'.format(sd_path_out)) _u, _v = mat['u'], mat['v'] # Build Transforms print(" - Build Transform") transforms = [] for i in range(n_shapes): transforms.append(OpticalFlowTransform(_u[:, :, i], _v[:, :, i])) # build dense shapes print(" - Build Dense Shapes") testing_points = reference_frame.mask.true_indices() ref_sparse_lms = align_t.apply(reference_shape) close_mask = BooleanImage( matpath(ref_sparse_lms.points).contains_points(testing_points).reshape( reference_frame.mask.mask.shape)) if tight_mask: reference_frame.mask = close_mask else: reference_frame.landmarks['sparse'] = ref_sparse_lms reference_frame.constrain_mask_to_landmarks(group='sparse') # Get Dense Shape from Masked Image dense_reference_shape = PointCloud( np.vstack((align_t.apply(reference_align_shape).points, align_t.apply(reference_shape).points, reference_frame.mask.true_indices()))) # Set Dense Shape as Reference Landmarks reference_frame.landmarks['source'] = dense_reference_shape dense_shapes = [] for i, t in enumerate(transforms): warped_points = t.apply(dense_reference_shape) dense_shape = warped_points dense_shapes.append(dense_shape) ni = [] for i, ds, t in zip(normalized_images, dense_shapes, _removed_transform): img = i.warp_to_shape(reference_frame.shape, _rf_align.compose_before(t), warp_landmarks=True) img.landmarks[group] = ds ni.append(img) return ni, transforms, reference_frame, n_landmarks, _n_align_points, _removed_transform, normalized_images, _rf_align, reference_shape, [ align_t # _rf_align, _removed_transform, aligned_shapes, target_shape, # reference_frame, dense_reference_shape, testing_points, # align_t, normalized_images, shapes, lms_shapes, # reference_shape, reference_align_shape ]
def _build_reference_frame(self, mean_shape): return build_reference_frame(mean_shape, boundary=self.boundary)
def _warp_template(self, template, group, reference_shape, scale_index, prefix, verbose): reference_frame = build_reference_frame(reference_shape) shape = template.landmarks[group] return warp_images([template], [shape], reference_frame, self.transform, prefix=prefix, verbose=verbose)