示例#1
0
def SNIC(filename,compactness, numSegments):
    # load image
    color_image = np.array(Image.open(resource_stream(__name__, filename)))
    color_image = cv2.imread(filename)
    number_of_pixels = color_image.shape[0] * color_image.shape[1]
    # 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)
    segmentation, distances, numSegments = snic(
        skimage.color.rgb2lab(color_image).tolist(),
        seeds,
        compactness, nd_computations["3"], distance_metric)
    return np.array(segmentation)
示例#2
0
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 pysnic.algorithms.snic import snic

# load image
color_image = np.array(
    Image.open(resource_stream(__name__, "../data/20x.png")).convert("RGB"))
lab_image = skimage.color.rgb2lab(color_image).tolist()
number_of_pixels = color_image.shape[0] * color_image.shape[1]

# SNIC parameters
number_of_segments = 100
compactness = 10.00

segmentation, _, number_of_segments, centroids = snic(
    lab_image,
    number_of_segments,
    compactness,
    update_func=lambda num_pixels: print("processed %05.2f%%" % (
        num_pixels * 100 / number_of_pixels)))

# show the output of SNIC
fig = plt.figure("SNIC with %d segments" % number_of_segments)
plt.imshow(mark_boundaries(color_image, np.array(segmentation)))
plt.show()
示例#3
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("superpixelation took: %fs" % (end - start))

# show the output of SNIC
plt.figure("SNIC with %d segments" % len(centroids))
plt.imshow(mark_boundaries(color_image, np.array(segmentation)))
plt.show()

plt.figure("segmentations")
plt.imshow(segmentation, cmap="prism")
plt.show()