File: merge_graph.py

package info (click to toggle)
libvigraimpex 1.11.1%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 50,356 kB
  • sloc: cpp: 57,785; python: 8,603; ansic: 1,798; makefile: 97; javascript: 65; sh: 50
file content (61 lines) | stat: -rw-r--r-- 1,698 bytes parent folder | download | duplicates (5)
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 print_function
import vigra
from vigra import graphs
from vigra import numpy
import pylab
# parameter
filepath = '12003.jpg'  # input image path
sigmaGradMag = 2.0      # sigma Gaussian gradient
superpixelDiameter = 10 # super-pixel size
slicWeight = 10.0       # SLIC color - spatial weight
beta = 0.5              # node vs edge weight
nodeNumStop = 50        # desired num. nodes in result


# load image and convert to LAB
img = vigra.impex.readImage(filepath)

# get super-pixels with slic on LAB image
imgLab = vigra.colors.transform_RGB2Lab(img)
labels, nseg = vigra.analysis.slicSuperpixels(imgLab, slicWeight,
                                              superpixelDiameter)
labels = vigra.analysis.labelImage(labels)


gridGraph = graphs.gridGraph(img.shape[0:2])
rag = graphs.regionAdjacencyGraph(gridGraph, labels)

# get the merge graph
mg = graphs.mergeGraph(rag)

# do n runs where we erase k edges in each run
n = 3
k = 200
for r in range(n):

    erased = 0

    while(erased<k):

        # get a random edge
        randEdgeId = numpy.random.randint(rag.edgeNum)

        print("random edge:",randEdgeId)
        # edge could be gone
        # -since we could have merged it already
        # - or due to transitivity of other merges
        if mg.hasEdgeId(randEdgeId):
            mg.contractEdge(mg.edgeFromId(randEdgeId))
            erased+=1

    # get the segmentation
    # (brand new c++ function for max speed)
    labels = mg.graphLabels()

    # view results
    rag.show(img,labels)
    vigra.show()

    # get the result as pixels wise labeling
    asImage = rag.projectLabelsToGridGraph(labels)
    asImage = vigra.taggedView(asImage, "xy")