-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrun_example.py
61 lines (45 loc) · 1.87 KB
/
run_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from __future__ import division, absolute_import, unicode_literals, print_function
import matplotlib.pyplot as plt
import maskslic as seg
import numpy as np
from skimage.segmentation import mark_boundaries
from skimage.io import imread
import time
# Load image
img = imread('chelsea.png')
# The ROI is also stored as an image for viewing convenience
# But the roi input input maskSLIC should be a binary image with the same spatial
# Dimensions as the image (in this case 300x451)
roi = imread('chelsea_mask.png')
# The alpha channel is used to store the ROI in this case and is converted into a logical array of 0s and 1s
roi = roi[:, :, 3] > 0
# Alternatively a mask could be created manually with for example a disk:
# roi = np.zeros((img.shape[0], img.shape[1]))
# a, b = 150, 150
# r = 100
# y,x = np.ogrid[-a:img.shape[0]-a, -b:img.shape[1]-b]
# mask = x*x + y*y <= r*r
# roi[mask] = 1
# ~~~~~~~~~~~~ Example 1: maskSLIC ~~~~~~~~~~~~~
t1 = time.time()
# Note that compactness is defined differently because a grid is not used. Lower compactness for maskSLIC is equivalent
segments = seg.slic(img, compactness=10, seed_type='nplace', mask=roi, n_segments=12,
recompute_seeds=True, plot_examples=True, enforce_connectivity=True)
print("Time: {:.2f} s".format(time.time() - t1))
plt.figure()
plt.title('maskSLIC')
plt.imshow(mark_boundaries(img, segments))
plt.contour(roi, contours=1, colors='red', linewidths=1)
plt.axis('off')
# ~~~~~~~~~~~ Example 2: SLIC ~~~~~~~~~~~~~~~~~
t1 = time.time()
segments = seg.slic(img, compactness=30, seed_type='grid', n_segments=80, plot_examples=False, enforce_connectivity=True)
# segments[roi==0] = -1
print("Time: {:.2f} s".format(time.time() - t1))
plt.figure()
plt.title('Conventional SLIC')
plt.imshow(mark_boundaries(img, segments))
plt.contour(roi, contours=1, colors='red', linewidths=1)
plt.axis('off')
plt.show()
plt.show()