def template_segmentation_image(source, color_radius, color_neighbors, object_radius, object_neighbors, noise_size): data = read_image(source); dbscan_instance = dbscan(data, color_radius, color_neighbors, True); print("Segmentation: '", source, "', Dimensions:", len(data[0])); dbscan_instance.process(); clusters = dbscan_instance.get_clusters(); real_clusters = [cluster for cluster in clusters if len(cluster) > noise_size]; print("Draw allocated color segments (back mask representation)..."); draw_image_mask_segments(source, real_clusters); print("Draw allocated color segments (color segment representation)..."); draw_image_color_segments(source, real_clusters); if (object_radius is None): return; # continue analysis pointer_image = Image.open(source); image_size = pointer_image.size; object_colored_clusters = []; for cluster in clusters: coordinates = []; for index in cluster: y = int(floor(index / image_size[0])); x = index - y * image_size[0]; coordinates.append([x, y]); # perform clustering analysis of the colored objects if (len(coordinates) < noise_size): continue; dbscan_instance = dbscan(coordinates, object_radius, object_neighbors, True); dbscan_instance.process(); object_clusters = dbscan_instance.get_clusters(); # decode it real_description_clusters = []; for object_cluster in object_clusters: real_description = []; for index_object in object_cluster: real_description.append(cluster[index_object]); real_description_clusters.append(real_description); if (len(real_description) > noise_size): object_colored_clusters.append(real_description); print("Draw allocated object segments (back mask representation)..."); draw_image_mask_segments(source, object_colored_clusters); print("Draw allocated object segments (color segment representation)..."); draw_image_color_segments(source, object_colored_clusters);
def template_segmentation_image(source, color_radius, color_neighbors, object_radius, object_neighbors, noise_size): data = read_image(source); dbscan_instance = dbscan(data, color_radius, color_neighbors, True); print("Segmentation: '", source, "', Dimensions:", len(data[0])); dbscan_instance.process(); clusters = dbscan_instance.get_clusters(); real_clusters = [cluster for cluster in clusters if len(cluster) > noise_size]; print("Draw allocated color segments (back mask representation)..."); draw_image_mask_segments(source, real_clusters); print("Draw allocated color segments (color segment representation)..."); draw_image_color_segments(source, real_clusters); if (object_radius is None): return; # continue analysis pointer_image = Image.open(source); image_size = pointer_image.size; object_colored_clusters = []; for cluster in clusters: coordinates = []; for index in cluster: y = int(floor(index / image_size[0])); x = index - y * image_size[0]; coordinates.append([x, y]); # perform clustering analysis of the colored objects if (len(coordinates) < noise_size): continue; dbscan_instance = dbscan(coordinates, object_radius, object_neighbors, True); dbscan_instance.process(); object_clusters = dbscan_instance.get_clusters(); # decode it real_description_clusters = []; for object_cluster in object_clusters: real_description = []; for index_object in object_cluster: real_description.append(cluster[index_object]); real_description_clusters.append(real_description); if (len(real_description) > noise_size): object_colored_clusters.append(real_description); print("Draw allocated object segments (back mask representation)..."); draw_image_mask_segments(source, object_colored_clusters); print("Draw allocated object segments (color segment representation)..."); draw_image_color_segments(source, object_colored_clusters);
def testDrawSegmentationResultNoFailure(self): data = utils.read_image(IMAGE_SIMPLE_SAMPLES.IMAGE_SIMPLE01); kmeans_instance = kmeans(data, [[255, 0, 0], [0, 0, 255], [180, 136, 0], [255, 255, 255]]); kmeans_instance.process(); clusters = kmeans_instance.get_clusters(); utils.draw_image_mask_segments(IMAGE_SIMPLE_SAMPLES.IMAGE_SIMPLE01, clusters); utils.draw_image_color_segments(IMAGE_SIMPLE_SAMPLES.IMAGE_SIMPLE01, clusters);