-
Notifications
You must be signed in to change notification settings - Fork 8
/
pattern.py
162 lines (134 loc) · 5.37 KB
/
pattern.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
import numpy as np
class Pattern:
"""
Pattern is a configuration of tiles from the input image.
"""
index_to_pattern = {}
color_to_index = {}
index_to_color = {}
def __init__(self, data, index):
self.index = index
self.data = np.array(data)
self.legal_patterns_index = {} # offset -> [pattern_index]
def get(self, index=None):
if index is None:
return self.data.item(0)
return self.data[index]
def set_legal_patterns(self, offset, legal_patterns):
self.legal_patterns_index[offset] = legal_patterns
@property
def shape(self):
return self.data.shape
def is_compatible(self, candidate_pattern, offset):
"""
Check if pattern is compatible with a candidate pattern for a given offset
:param candidate_pattern:
:param offset:
:return: True if compatible
"""
assert (self.shape == candidate_pattern.shape)
# Precomputed compatibility
if offset in self.legal_patterns_index:
return candidate_pattern.index in self.legal_patterns_index[offset]
# Computing compatibility
ok_constraint = True
start = tuple([max(offset[i], 0) for i, _ in enumerate(offset)])
end = tuple([min(self.shape[i] + offset[i], self.shape[i]) for i, _ in enumerate(offset)])
for index in np.ndindex(end): # index = (x, y, z...)
start_constraint = True
for i, d in enumerate(index):
if d < start[i]:
start_constraint = False
break
if not start_constraint:
continue
if candidate_pattern.get(tuple(np.array(index) - np.array(offset))) != self.get(index):
ok_constraint = False
break
return ok_constraint
def to_image(self):
return Pattern.index_to_img(self.data)
@staticmethod
def from_sample(sample, pattern_size):
"""
Compute patterns from sample
:param pattern_size:
:param sample:
:return: list of patterns
"""
sample = Pattern.sample_img_to_indexes(sample)
shape = sample.shape
patterns = []
pattern_index = 0
for index, _ in np.ndenumerate(sample):
# Checking if index is out of bounds
out = False
for i, d in enumerate(index): # d is a dimension, e.g.: x, y, z
if d > shape[i] - pattern_size[i]:
out = True
break
if out:
continue
pattern_location = [range(d, pattern_size[i] + d) for i, d in enumerate(index)]
pattern_data = sample[np.ix_(*pattern_location)]
datas = [pattern_data, np.fliplr(pattern_data)]
if shape[1] > 1: # is 2D
datas.append(np.flipud(pattern_data))
datas.append(np.rot90(pattern_data, axes=(1, 2)))
datas.append(np.rot90(pattern_data, 2, axes=(1, 2)))
datas.append(np.rot90(pattern_data, 3, axes=(1, 2)))
if shape[0] > 1: # is 3D
datas.append(np.flipud(pattern_data))
datas.append(np.rot90(pattern_data, axes=(0, 2)))
datas.append(np.rot90(pattern_data, 2, axes=(0, 2)))
datas.append(np.rot90(pattern_data, 3, axes=(0, 2)))
# Checking existence
# TODO: more probability to multiple occurrences when observe phase
for data in datas:
exist = False
for p in patterns:
if (p.data == data).all():
exist = True
break
if exist:
continue
pattern = Pattern(data, pattern_index)
patterns.append(pattern)
Pattern.index_to_pattern[pattern_index] = pattern
pattern_index += 1
# Pattern.plot_patterns(patterns)
return patterns
@staticmethod
def sample_img_to_indexes(sample):
"""
Convert a rgb image to a 2D array with pixel index
:param sample:
:return: pixel index sample
"""
Pattern.color_to_index = {}
Pattern.index_to_color = {}
sample_index = np.zeros(sample.shape[:-1]) # without last rgb dim
color_number = 0
for index in np.ndindex(sample.shape[:-1]):
color = tuple(sample[index])
if color not in Pattern.color_to_index:
Pattern.color_to_index[color] = color_number
Pattern.index_to_color[color_number] = color
color_number += 1
sample_index[index] = Pattern.color_to_index[color]
print('Unique color count = ', color_number)
return sample_index
@staticmethod
def index_to_img(sample):
color = next(iter(Pattern.index_to_color.values()))
image = np.zeros(sample.shape + (len(color),))
for index in np.ndindex(sample.shape):
pattern_index = sample[index]
if pattern_index == -1:
image[index] = [0.5 for _ in range(len(color))] # Grey
else:
image[index] = Pattern.index_to_color[pattern_index]
return image
@staticmethod
def from_index(pattern_index):
return Pattern.index_to_pattern[pattern_index]