-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquadtree.py
180 lines (134 loc) · 6.95 KB
/
quadtree.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import numpy as np
from statistics import mean, pstdev
class QuadTreeNode :
def __init__(self, greyscale, homogenity = False) :
self.greyscale = greyscale
self.homogenity = homogenity
class region :
def __init__(self, boundaries) :
self._boundaries = boundaries
@property
def left(self) :
return self._boundaries[0]
@property
def right(self):
return self._boundaries[1]
@property
def top(self):
return self._boundaries[2]
@property
def bottom(self):
return self._boundaries[3]
@staticmethod
def CheckBounds(quadrant):
return quadrant.left <= quadrant.right and quadrant.top <= quadrant.bottom
@staticmethod
def IsPixel(region_) :
return (region_.left == region_.right and region_.top == region_.bottom)
def __str__(self):
return str(self._boundaries)
class QuadTree :
DEVIATION_THRESHOLD = 0.07
def __init__(self) :
self._list=[]
self._image_size=(0,0)
def _BuildTreeUtil(self, pixel_matrix, index, quadrant):
if not region.CheckBounds(quadrant) :
return 0
if region.IsPixel(quadrant) :
self._list[index] = QuadTreeNode(pixel_matrix[quadrant.left, quadrant.top], True)
return self._list[index]
vertical_middle = (quadrant.right+quadrant.left)//2
horizontal_middle = (quadrant.top+quadrant.bottom)//2
quad_top_left = region((quadrant.left, vertical_middle, quadrant.top, horizontal_middle))
quad_top_right = region((vertical_middle+1, quadrant.right, quadrant.top, horizontal_middle))
quad_bottom_left = region((quadrant.left, vertical_middle, horizontal_middle+1, quadrant.bottom))
quad_bottom_right = region((vertical_middle+1, quadrant.right, horizontal_middle+1, quadrant.bottom))
val = (
self._BuildTreeUtil(pixel_matrix, 4*index+1, quad_top_left).greyscale+
self._BuildTreeUtil(pixel_matrix, 4*index+2, quad_top_right).greyscale+
self._BuildTreeUtil(pixel_matrix, 4*index+3, quad_bottom_left).greyscale+
self._BuildTreeUtil(pixel_matrix, 4*index+4, quad_bottom_right).greyscale)//4
self._list[index] = QuadTreeNode(val)
return self._list[index]
def _RenderTreeUtil(self, pixel_matrix, index, quadrant) :
if not region.CheckBounds(quadrant) :
return
if region.IsPixel(quadrant) :
pixel_matrix[quadrant.left, quadrant.top] = self._list[index].greyscale
return
vertical_middle = (quadrant.right + quadrant.left)//2
horizontal_middle = (quadrant.top + quadrant.bottom)//2
quad_top_left = region((quadrant.left, vertical_middle, quadrant.top, horizontal_middle))
quad_top_right = region((vertical_middle+1, quadrant.right, quadrant.top, horizontal_middle))
quad_bottom_left = region((quadrant.left, vertical_middle, horizontal_middle+1, quadrant.bottom))
quad_bottom_right = region((vertical_middle+1, quadrant.right, horizontal_middle+1, quadrant.bottom))
self._RenderTreeUtil(pixel_matrix, 4*index+1, quad_top_left)
self._RenderTreeUtil(pixel_matrix, 4*index+2, quad_top_right)
self._RenderTreeUtil(pixel_matrix, 4*index+3, quad_bottom_left)
self._RenderTreeUtil(pixel_matrix, 4*index+4, quad_bottom_right)
def _FillDescendants(self, index, quadrant, value) :
if not region.CheckBounds(quadrant) :
return
if region.IsPixel(quadrant) :
self._list[index].greyscale = value
self._list[index].homogenity = True
return
vertical_middle = (quadrant.left + quadrant.right) // 2
horizontal_middle = (quadrant.top + quadrant.bottom) // 2
quad_top_left = region((quadrant.left, vertical_middle, quadrant.top, horizontal_middle))
quad_top_right = region((vertical_middle+1, quadrant.right, quadrant.top, horizontal_middle))
quad_bottom_left = region((quadrant.left, vertical_middle, horizontal_middle+1, quadrant.bottom))
quad_bottom_right = region((vertical_middle+1, quadrant.right, horizontal_middle+1, quadrant.bottom))
self._list[index].greyscale = value
self._list[index].homogenity = True
self._FillDescendants(4*index+1, quad_top_left, value)
self._FillDescendants(4*index+2, quad_top_right, value)
self._FillDescendants(4*index+3, quad_bottom_left, value)
self._FillDescendants(4*index+4, quad_bottom_right, value)
def _PruneTree(self, index, quadrant) :
def rsd(data, avg) :
s = 0
for i in data :
s = (i-avg)**2
s /= len(data)
s **= 0.5
return s/avg if avg!=0 else 0
if not region.CheckBounds(quadrant) :
return 0
if region.IsPixel(quadrant) :
return self._list[index]
vertical_middle = (quadrant.left + quadrant.right) // 2
horizontal_middle = (quadrant.top + quadrant.bottom) // 2
quad_top_left = region((quadrant.left, vertical_middle, quadrant.top, horizontal_middle))
quad_top_right = region((vertical_middle+1, quadrant.right, quadrant.top, horizontal_middle))
quad_bottom_left = region((quadrant.left, vertical_middle, horizontal_middle+1, quadrant.bottom))
quad_bottom_right = region((vertical_middle+1, quadrant.right, horizontal_middle+1, quadrant.bottom))
value_tl = self._PruneTree(4*index+1, quad_top_left).greyscale
value_tr = self._PruneTree(4*index+2, quad_top_right).greyscale
value_bl = self._PruneTree(4*index+3, quad_bottom_left).greyscale
value_br = self._PruneTree(4*index+4, quad_bottom_right).greyscale
average = (value_bl + value_br + value_tl + value_tr)/4
deviation = rsd([value_bl, value_br, value_tl, value_tr], average)
homogenity = self._list[4*index+1].homogenity and self._list[4*index+2].homogenity and self._list[4*index+3].homogenity and self._list[4*index+4].homogenity
if deviation <= self.DEVIATION_THRESHOLD and homogenity :
self._FillDescendants(index,quadrant, int(average))
self._list[index].greyscale = int(average)
self._list[index].homogenity = True
return self._list[index]
def BuildTree(self, pixel_matrix) :
c,r = self._image_size = pixel_matrix.shape
self._list = [None for i in range(4*r*c)]
self._BuildTreeUtil(pixel_matrix, 0, region((0, c-1, 0, r-1)))
def RenderTree(self) :
c,r = image_size = self.image_size
pixel_matrix = np.empty(image_size, dtype='uint16')
quadrant = region((0, c-1, 0, r-1))
self._RenderTreeUtil(pixel_matrix, 0, quadrant)
return pixel_matrix
def CompressTree(self) :
c,r = self.image_size
self._PruneTree(0, region((0, c-1, 0, r-1)))
@property
def image_size(self):
return self._image_size