From 07386ca269396724140c03c4643c092b216666e4 Mon Sep 17 00:00:00 2001 From: Moritz Willig Date: Fri, 5 Mar 2021 03:40:24 +0100 Subject: [PATCH] add script to measure snic performance --- pysnic/examples/timing.py | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 pysnic/examples/timing.py diff --git a/pysnic/examples/timing.py b/pysnic/examples/timing.py new file mode 100644 index 0000000..4954f78 --- /dev/null +++ b/pysnic/examples/timing.py @@ -0,0 +1,44 @@ +from pkg_resources import resource_stream +from PIL import Image +import matplotlib.pyplot as plt +import numpy as np +import skimage.color +from skimage.segmentation import mark_boundaries +from timeit import default_timer as timer +from itertools import chain +from pysnic.algorithms.snic import snic, compute_grid +from pysnic.ndim.operations_collections import nd_computations +from pysnic.metric.snic import create_augmented_snic_distance +import math + +# load image +color_image = np.array(Image.open(resource_stream(__name__, "../data/orchid.jpg"))) +number_of_pixels = color_image.shape[0] * color_image.shape[1] + +# SNIC parameters +numSegments = 100 +compactness = 10.0 + +# compute grid +grid = compute_grid(color_image.shape, numSegments) +seeds = list(chain.from_iterable(grid)) +seed_len = len(seeds) + +# choose a distance metric +distance_metric = create_augmented_snic_distance(color_image.shape, seed_len, compactness) + + +start = timer() + +segmentation, distances, centroids = snic( + skimage.color.rgb2lab(color_image).tolist(), + seeds, + compactness, nd_computations["3"], distance_metric, + update_func=lambda num_pixels: print("processed %05.2f%%" % (num_pixels * 100 / number_of_pixels))) + +end = timer() +print(f"superpixelation took: {math.floor((end - start)*1000)}ms") + +plt.figure("SNIC with %d segments" % len(centroids)) +plt.imshow(mark_boundaries(color_image, np.array(segmentation))) +plt.show()