-
Notifications
You must be signed in to change notification settings - Fork 3
/
superpixel.py
40 lines (28 loc) · 1.14 KB
/
superpixel.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
import matplotlib.pyplot as plt
import numpy as np
from skimage.segmentation import slic, quickshift
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float
from skimage import io
import os
inputfile = 'coins.jpg'
output_slic = os.path.splitext(inputfile)[0] + '_slic' + '.bmp'
output_quickshift = os.path.splitext(inputfile)[0] + '_quickshift' + '.bmp'
image = img_as_float(io.imread(inputfile))
segments_slic = slic(image, n_segments = 100, sigma = 5)
segments_quickshift = quickshift(image, kernel_size = 5, max_dist = 6, ratio = 0.5)
slic_result = mark_boundaries(image, segments_slic, color = (1, 0, 0))
quickshift_result = mark_boundaries(image, segments_quickshift, color = (1, 0, 0))
fig, ax = plt.subplots(1, 2, figsize=(20, 10), sharex=True, sharey=True)
ax[0].imshow(slic_result)
ax[0].set_title('slic')
ax[1].imshow(quickshift_result)
ax[1].set_title('quickshift')
for a in ax.ravel():
a.set_axis_off()
plt.tight_layout()
plt.show()
#plt.imsave(output_slic, slic_result)
#plt.imsave(output_quickshift, quickshift_result)
io.imsave(output_slic, slic_result)
io.imsave(output_quickshift, quickshift_result)