forked from salaee/pegbis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment_graph.py
71 lines (58 loc) · 2.03 KB
/
segment_graph.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
62
63
64
65
66
67
68
69
70
71
from disjoint_set import *
import math
import numpy as np
import random
# ---------------------------------------------------------
# Segment a graph:
# Returns a disjoint-set forest representing the segmentation.
#
# Inputs:
# num_vertices: number of vertices in graph.
# num_edges: number of edges in graph
# edges: array of edges.
# c: constant for threshold function.
#
# Output:
# a disjoint-set forest representing the segmentation.
# ------------------------------------------------------------
def segment_graph(num_vertices, num_edges, edges, c):
# sort edges by weight (3rd column)
edges[0:num_edges, :] = edges[edges[0:num_edges, 2].argsort()]
# make a disjoint-set forest
u = universe(num_vertices)
# init thresholds
threshold = np.zeros(shape=num_vertices, dtype=float)
for i in range(num_vertices):
threshold[i] = get_threshold(1, c)
# for each edge, in non-decreasing weight order...
for i in range(num_edges):
pedge = edges[i, :]
# components connected by this edge
a = u.find(pedge[0])
b = u.find(pedge[1])
if a != b:
if pedge[2] <= min(threshold[a], threshold[b]):
u.join(a, b)
a = u.find(a)
b = u.find(b)
threshold[a] = pedge[2] + get_threshold(u.size(a), c)
threshold[b] = threshold[a]
return u
def get_threshold(size, c):
return c / size
# returns square of a number
def square(value):
return value * value
# randomly creates RGB
def random_rgb():
rgb = np.zeros(3, dtype=int)
rgb[0] = random.randint(0, 255)
rgb[1] = random.randint(0, 255)
rgb[2] = random.randint(0, 255)
return rgb
# dissimilarity measure between pixels
def diff(red_band, green_band, blue_band, x1, y1, x2, y2):
result = math.sqrt(
square(red_band[y1, x1] - red_band[y2, x2]) + square(green_band[y1, x1] - green_band[y2, x2]) + square(
blue_band[y1, x1] - blue_band[y2, x2]))
return result