Newer
Older
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
import numpy as np
import matplotlib.pyplot as plt
import skimage.io
import slgbuilder
I = skimage.io.imread('../../../../Data/week7/peaks_image.png').astype(np.int32)
fig, ax = plt.subplots(1,2)
ax[0].imshow(I, cmap='gray')
layers = [slgbuilder.GraphObject(0*I), slgbuilder.GraphObject(0*I)] # no on-surface cost
helper = slgbuilder.MaxflowBuilder()
helper.add_objects(layers)
# Addin regional costs,
# the region in the middle is bright compared to two darker regions.
helper.add_layered_region_cost(layers[0], I, 255-I)
helper.add_layered_region_cost(layers[1], 255-I, I)
# Adding geometric constrains
helper.add_layered_boundary_cost()
helper.add_layered_smoothness(delta=1, wrap=False)
helper.add_layered_containment(layers[0], layers[1], min_margin=1)
# Cut
helper.solve()
segmentations = [helper.what_segments(l).astype(np.int32) for l in layers]
segmentation_lines = [s.shape[0] - np.argmax(s[::-1,:], axis=0) - 1 for s in segmentations]
# Visualization
ax[1].imshow(I, cmap='gray')
for line in segmentation_lines:
ax[1].plot(line, 'r')