-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemantic_segmentation.py
39 lines (32 loc) · 1.26 KB
/
semantic_segmentation.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
from PyQt5.QtGui import QPixmap
from operation import *
from scipy import ndimage as ndi
import matplotlib.pyplot as plt
import matplotlib as mpl
from skimage.morphology import watershed, disk
from skimage.filters import rank
import numpy as np
from skimage.util import img_as_ubyte
class SemanticSegmentation(Operation):
@staticmethod
def execute(input_image, settings):
size = input_image.size
size = (int(size[0]/100), int(size[1]/100))
grayscale = input_image.convert('L')
image_arr = np.array(img_as_ubyte(grayscale))
denoised = rank.median(image_arr, disk(2))
markers = rank.gradient(denoised, disk(5)) < 10
markers = ndi.label(markers)[0]
gradient = rank.gradient(denoised, disk(2))
labels = watershed(gradient, markers)
figure = plt.figure(frameon=False)
ax = plt.Axes(figure, [0., 0., 1., 1.])
ax.set_axis_off()
figure.add_axes(ax)
figure.set_size_inches(size)
extent = mpl.transforms.Bbox(((0,0), size))
ax.imshow(labels, cmap=plt.cm.spectral, interpolation='nearest')
buffer = io.BytesIO()
figure.savefig(buffer, bbox_inches=extent, pad_inches=0, format='png')
buffer.seek(0)
return Image.open(buffer)