forked from rudolflam/comp_vision_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.py
117 lines (111 loc) · 5.03 KB
/
debug.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- coding: utf-8 -*-
"""
@author: rudolf
"""
import mser
import cv2
import numpy as np
import logging
from mser import MSER
from sets import Set
cv2.namedWindow("image", cv2.WINDOW_NORMAL)
image = cv2.imread("vsmall.jpg")
cv2.imshow("image" , image)
m = mser.MSER(image)
universe = m.sorted_universe()
nodes = {}
subtreeRoot = {}
set1 = {}
set2 = {}
num_rows, num_cols = np.shape(m.grey_scale)
higher_intensity = filter(lambda neighbour: neighbour.intensity>=universe[0].intensity , universe)
def already_processed_neighbours(point,higher_intensity):
if point.intensity < higher_intensity[-1].intensity:
higher_intensity = filter(lambda neighbour: neighbour.intensity>=point.intensity , universe)
return point.neighbours(higher_intensity)
logging.info("Preprocessing step")
num_points = len(universe)
i = 0
for point in universe:
i += 1
set1[point] = MSER.DisjointSet(point)
set2[point] = MSER.DisjointSet(point)
nodes[point] = MSER.ComponentTree(point.intensity)
subtreeRoot[point] = point
i = 0
print "Universe bomb"
for point in universe:
i += 1
current_canonical = set1[point].find()
current_node = set2[subtreeRoot[current_canonical.data]].find()
# print "Looking at ", nodes[current_node.data]
# if point.row == 22 and point.col == 19:
# print "Looking at special point"
for neighbour in already_processed_neighbours(point,higher_intensity) :
# if point.row == 22 and point.col == 19:
# print "Neighbour of special pt ", neighbour, " ", neighbour.intensity
if neighbour.intensity >= point.intensity:
neighbour_canonical = set1[neighbour].find()
print "node : ", point, " neighbour : ", neighbour
if point.row == 0 and point.col == 7:
print ">>>>current canonical ", current_canonical.data
print ">>>>Neighbour canonical ", neighbour_canonical.data
else :
print "current canonical ", current_canonical.data
print "neighbour canonical ", neighbour_canonical.data
neighbour_node = set2[subtreeRoot[neighbour_canonical.data]].find()
# logging.info("Comparing intensities of "+str(current_node.data)+ " " + str(neighbour_node.data))
if current_node != neighbour_node:
if nodes[current_node.data].level == nodes[neighbour_node.data].level :
if point.row == 0 and point.col == 7:
print ">>>>merging ", current_node.data, " & ",neighbour_node.data , " @ ",nodes[current_node.data].level
else:
print "merging ",current_node.data, " & ",neighbour_node.data , " @ ",nodes[current_node.data].level
# merge the nodes
nn = set2[neighbour_node.data]
cn = set2[current_node.data]
temp = nn.union(cn)
if temp is cn :
nodes[cn.data].add_children(nodes[nn.data].children)
nodes[nn.data].children = nodes[cn.data].children
else:
nodes[nn.data].add_children(nodes[cn.data].children)
nodes[cn.data].children = nodes[nn.data].children
current_node = temp
# print "Merging neigbhour ", neighbour, " and point ", point
# print "To ",nodes[current_node.data]
else:
# the level is less than neighbour node's level
nodes[current_node.data].add_child(nodes[neighbour_node.data])
current_canonical = neighbour_canonical.union(current_canonical)
subtreeRoot[current_canonical.data] = current_node.data
print "Subtree root ", current_node.data," given to ", subtreeRoot[current_canonical.data]
# print "Subtree roots ", subtreeRoot
print "==============="
print "Subtree built ", nodes[point]
print "==============="
print subtreeRoot
component_map = {}
inv_component_map = {}
logging.info("Post processing ")
for point in universe:
component_map[point] = nodes[set2[point].find().data]
def give_point_to_component(point,component,inv_component_map):
try:
inv_component_map[component_map[point]].add(point)
except:
inv_component_map[component_map[point]] = Set([point])
for child in component.children:
give_point_to_component(point, child,inv_component_map)
def get_points_from_subtree(tree,inv_component_map):
for c in tree.children:
get_points_from_subtree(c, inv_component_map)
for c in tree.children:
inv_component_map[tree] |= inv_component_map[c]
for point in component_map.keys():
give_point_to_component(point, component_map[point],inv_component_map)
root_point = subtreeRoot[set1[set2[universe[0]].find().data].find().data]
root_node = nodes[root_point]
copy = image.copy()
from main import TextDetector as td
td.draw_points(image, inv_component_map[root_node])