def test_same_output(num_clusters, normals_per_cluster): fixture = setup_fastga_simple(level=4) clusters, normals = cluster_normals( num_clusters=num_clusters, normals_per_cluster=normals_per_cluster, patch_deg=5) clusters = np.concatenate(clusters) ga = fixture['ga'] ico = fixture['ico'] ga.integrate(MatX3d(clusters)) # New API new_api_peaks = np.array( ga.find_peaks(threshold_abs=20, cluster_distance=0.1, min_cluster_weight=0.1)) # Old API normalized_bucket_counts_by_vertex = ga.get_normalized_bucket_counts_by_vertex( True) average_vertex_normals = np.asarray(ga.get_average_normals_by_vertex(True)) ico.fill_image(normalized_bucket_counts_by_vertex) _, _, old_api_peaks, _ = find_peaks_from_ico_charts( ico, np.asarray(normalized_bucket_counts_by_vertex), vertices=average_vertex_normals) # Sort the peaks new_api_peaks = sort_by_distance_from_point(new_api_peaks) old_api_peaks = sort_by_distance_from_point(old_api_peaks) print(new_api_peaks) print(old_api_peaks) np.testing.assert_allclose(new_api_peaks, old_api_peaks)
def get_image_peaks(ga_cpp_s2, level=2, **kwargs): ico_chart = IcoCharts(level) normalized_bucket_counts_by_vertex = ga_cpp_s2.get_normalized_bucket_counts_by_vertex( True) ico_chart.fill_image(normalized_bucket_counts_by_vertex) find_peaks_kwargs = dict(threshold_abs=20, min_distance=1, exclude_border=False, indices=False) cluster_kwargs = dict(t=0.2, criterion='distance') average_filter = dict(min_total_weight=0.05) peaks, clusters, avg_peaks, avg_weights = find_peaks_from_ico_charts( ico_chart, np.asarray(normalized_bucket_counts_by_vertex), find_peaks_kwargs=find_peaks_kwargs, cluster_kwargs=cluster_kwargs, average_filter=average_filter) gaussian_normals_sorted = np.asarray(ico_chart.sphere_mesh.vertices) pcd_all_peaks = get_pc_all_peaks(peaks, clusters, gaussian_normals_sorted) arrow_avg_peaks = get_arrow_normals(avg_peaks, avg_weights) print(avg_peaks) return [pcd_all_peaks, *arrow_avg_peaks]
def example_normals(normals: np.ndarray): LEVEL = 4 kwargs_base = dict(level=LEVEL) kwargs_s2 = dict(**kwargs_base) axis_frame = o3d.geometry.TriangleMesh.create_coordinate_frame( 0.5).translate([-2.0, 0, 0]) # Create Gaussian Accumulator ga_cpp_s2 = GaussianAccumulatorS2Beta(**kwargs_s2) # Integrate the normals and get open3d visualization colored_icosahedron = integrate_normals_and_visualize(normals, ga_cpp_s2) o3d.visualization.draw_geometries([colored_icosahedron, axis_frame]) # Create the IcoChart for unwrapping ico_chart_ = IcoCharts(LEVEL) normalized_bucket_counts_by_vertex = ga_cpp_s2.get_normalized_bucket_counts_by_vertex( True) ico_chart_.fill_image(normalized_bucket_counts_by_vertex) average_vertex_normals = np.asarray( ga_cpp_s2.get_average_normals_by_vertex(True)) # 2D Peak Detection find_peaks_kwargs = dict(threshold_abs=20, min_distance=1, exclude_border=False, indices=False) cluster_kwargs = dict(t=0.05, criterion='distance') average_filter = dict(min_total_weight=0.2) # New simplified API for finding peaks res = np.array( ga_cpp_s2.find_peaks( threshold_abs=find_peaks_kwargs['threshold_abs'], cluster_distance=cluster_kwargs['t'], min_cluster_weight=average_filter['min_total_weight'])) print("New Detected Peaks:") res = sort_by_distance_from_point(res) print(res) # Old Way of finding peaks _, _, avg_peaks, _ = find_peaks_from_ico_charts( ico_chart_, np.asarray(normalized_bucket_counts_by_vertex), vertices=average_vertex_normals, find_peaks_kwargs=find_peaks_kwargs, cluster_kwargs=cluster_kwargs) avg_peaks = sort_by_distance_from_point(avg_peaks) print("Detected Peaks:") print(avg_peaks) full_image = np.asarray(ico_chart_.image) plt.imshow(full_image) plt.xticks(np.arange(0, full_image.shape[1], step=1)) plt.yticks(np.arange(0, full_image.shape[0], step=1)) plt.show() # Don't forget to reset the GA ga_cpp_s2.clear_count()
def find_peaks_from_ico_charts_updated(ga, ico, find_peaks_kwargs, cluster_kwargs, average_filter): normalized_bucket_counts_by_vertex = np.asarray( ga.get_normalized_bucket_counts_by_vertex(True)) average_vertex_normals = np.asarray(ga.get_average_normals_by_vertex(True)) res = find_peaks_from_ico_charts(ico, normalized_bucket_counts_by_vertex, vertices=average_vertex_normals, find_peaks_kwargs=find_peaks_kwargs, cluster_kwargs=cluster_kwargs, average_filter=average_filter) return res
def get_image_peaks(ico_chart, ga, level=2, with_o3d=False, find_peaks_kwargs=dict(threshold_abs=2, min_distance=1, exclude_border=False, indices=False), cluster_kwargs=dict(t=0.10, criterion='distance'), average_filter=dict(min_total_weight=0.01), **kwargs): normalized_bucket_counts_by_vertex = ga.get_normalized_bucket_counts_by_vertex( True) t1 = time.perf_counter() ico_chart.fill_image( normalized_bucket_counts_by_vertex) # this takes microseconds # plt.imshow(np.asarray(ico_chart.image)) # plt.show() peaks, clusters, avg_peaks, avg_weights = find_peaks_from_ico_charts( ico_chart, np.asarray(normalized_bucket_counts_by_vertex), find_peaks_kwargs=find_peaks_kwargs, cluster_kwargs=cluster_kwargs, average_filter=average_filter) t2 = time.perf_counter() gaussian_normals_sorted = np.asarray(ico_chart.sphere_mesh.vertices) # Create Open3D structures for visualization if with_o3d: pcd_all_peaks = get_pc_all_peaks(peaks, clusters, gaussian_normals_sorted) arrow_avg_peaks = get_arrow_normals(avg_peaks, avg_weights) else: pcd_all_peaks = None arrow_avg_peaks = None elapsed_time = (t2 - t1) * 1000 timings = dict(t_fastga_peak=elapsed_time) logging.debug("Peak Detection - Took (ms): %.2f", (t2 - t1) * 1000) return avg_peaks, pcd_all_peaks, arrow_avg_peaks, timings
def main(): EXAMPLE_INDEX = 1 kwargs_base = dict(level=2, max_phi=180) kwargs_s2 = dict(**kwargs_base) kwargs_opt_integrate = dict(num_nbr=12) query_max_phi = kwargs_base['max_phi'] - 5 # Get an Example Mesh ga_cpp_s2 = GaussianAccumulatorS2(**kwargs_s2) example_mesh = o3d.io.read_triangle_mesh(str(ALL_MESHES[EXAMPLE_INDEX])) r = ALL_MESHES_ROTATIONS[EXAMPLE_INDEX] example_mesh_filtered = example_mesh if r is not None: example_mesh_filtered = example_mesh_filtered.rotate(r.as_matrix()) example_mesh_filtered = example_mesh_filtered.filter_smooth_laplacian( 5) example_mesh_filtered.compute_triangle_normals() # np.save('fixtures/normals/basement.npy', np.asarray(example_mesh_filtered.triangle_normals)) colored_icosahedron_s2, normals, neighbors_s2 = visualize_gaussian_integration( ga_cpp_s2, example_mesh_filtered, max_phi=query_max_phi, integrate_kwargs=kwargs_opt_integrate) o3d.visualization.draw_geometries([example_mesh_filtered]) o3d.visualization.draw_geometries([colored_icosahedron_s2]) # Visualize unwrapping ico_chart_ = IcoCharts(kwargs_base['level']) t2 = time.perf_counter() normalized_bucket_counts_by_vertex = ga_cpp_s2.get_normalized_bucket_counts_by_vertex( True) ico_chart_.fill_image(normalized_bucket_counts_by_vertex) find_peaks_kwargs = dict(threshold_abs=50, min_distance=1, exclude_border=False, indices=False) print(np.asarray(ico_chart_.image).shape) cluster_kwargs = dict(t=0.1, criterion='distance') _, _, avg_peaks, avg_weights = find_peaks_from_ico_charts( ico_chart_, np.asarray(normalized_bucket_counts_by_vertex), find_peaks_kwargs=find_peaks_kwargs, cluster_kwargs=cluster_kwargs) t3 = time.perf_counter() print(t3 - t2) print(avg_peaks) arrow_avg_peaks = get_arrow_normals(avg_peaks, avg_weights) o3d.visualization.draw_geometries( [colored_icosahedron_s2, *arrow_avg_peaks]) full_image = np.asarray(ico_chart_.image) plt.imshow(full_image) plt.axis('off') # plt.xticks(np.arange(0, full_image.shape[1], step=1)) # plt.yticks(np.arange(0, full_image.shape[0], step=1)) plt.show()