-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit_extractor.py
executable file
·183 lines (151 loc) · 5.81 KB
/
bit_extractor.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
180
181
182
import cv2
import numpy as np
import sys
import imutils
def is_frame_marker_border(border, errorPercentage = 80):
return np.count_nonzero(border) < int(border.size * errorPercentage / 100.0)
def is_valid_border(image):
borderHeigh = image.shape[0] // 18
borderWidth = image.shape[1]
#check head border
border = image[0:borderHeigh, 0: borderWidth]
if not is_frame_marker_border(border):
return False
#check tail border
border = image[-borderHeigh : -1, -borderWidth: -1]
# border[:] = 200
if not is_frame_marker_border(border):
return False
##check left border
border = image[0:borderWidth, 0: borderHeigh]
# border[:] = 100
if not is_frame_marker_border(border):
return False
##check right border
border = image[-borderWidth: -1, -borderHeigh: -1]
# border[:] = 100
if not is_frame_marker_border(border):
return False
return True
def is_valid_border_(image):
borderHeigh = image.shape[0] // 18
borderWidth = image.shape[1]
total_pixel = 0
total_nonzero = 0
#check head border
border = image[0:borderHeigh, 0: borderWidth]
total_pixel = total_pixel + border.size
total_nonzero = total_nonzero + np.count_nonzero(border)
#check tail border
border = image[-borderHeigh : -1, -borderWidth: -1]
total_pixel = total_pixel + border.size
total_nonzero = total_nonzero + np.count_nonzero(border)
# border[:] = 200
##check left border
border = image[0:borderWidth, 0: borderHeigh]
total_pixel = total_pixel + border.size
total_nonzero = total_nonzero + np.count_nonzero(border)
# border[:] = 100
##check right border
border = image[-borderWidth: -1, -borderHeigh: -1]
total_pixel = total_pixel + border.size
total_nonzero = total_nonzero + np.count_nonzero(border)
# border[:] = 100
return total_nonzero < 0.3 * total_pixel
def get_bit(bitCell):
# print("{} {}".format(np.count_nonzero(bitCell), bitCell.size))
bit = 0 if np.count_nonzero(bitCell) > bitCell.size * 0.5 else 1
# print(bit)
# cv2.imshow("bit cell", bitCell)
# cv2.waitKey(0)
return bit
def pre_process_image(image):
out = image.copy()
#has 3 channels
if len(out.shape) == 3 and out.shape[-1] == 3:
out = cv2.cvtColor(out, cv2.COLOR_BGR2GRAY)
_, thres = cv2.threshold(out, np.mean(out) + np.std(out), 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# print(np.mean(out) + np.std(out) * 3.5)
# _, thres = cv2.threshold(out, np.mean(out) - np.std(out) / 2, 255, cv2.THRESH_BINARY)
return thres
def extract_bit(image):
out = pre_process_image(image)
# if not is_valid_border(out):
# return None
borderSize = out.shape[0] // 18
inner = out[borderSize:-borderSize, borderSize:-borderSize] if is_valid_border_(out) else out
bitSize = inner.shape[0] // 20
# cv2.imshow("Inner", inner)
bit_ranges = [[], [], [], []]
for i in range(0, 20):
#get top border bit
bitCell = inner[0:bitSize, i * bitSize: (i + 1) * bitSize]
bit_ranges[0].append(get_bit(bitCell))
#get bottom border bit
bitCell = inner[-bitSize: -1, i * bitSize: (i + 1) * bitSize]
bit_ranges[2].append(get_bit(bitCell))
#get left border bit
bitCell = inner[i * bitSize: (i + 1) * bitSize, 0:bitSize]
# bitCell[:] = 200 if (i // 2) % 2 == 0 else 100
bit_ranges[3].append(get_bit(bitCell))
#get right border bit
bitCell = inner[i * bitSize: (i + 1) * bitSize, -bitSize:-1]
bit_ranges[1].append(get_bit(bitCell))
# bitCell[:] = 200# if i % 2 == 0 else 100
# print(bit_ranges)
#chane direct for left and bottom
bit_ranges[2].reverse()
bit_ranges[3].reverse()
# cv2.imshow('Inner', inner)
#remove unnessesary at head and tail of bit_range
for i in range(4):
bit_ranges[i] = bit_ranges[i][1:-2]
bit_ranges[i] = [bit_ranges[i][j] for j in range(0, len(bit_ranges[i]), 2)]
return bit_ranges
def get_id(bit_ranges):
zero_id = np.array([292, 246, 177, 472])
if bit_ranges is None:
return -1, 0
#convert to decimal
id_matrix = np.array([int("".join(str(i) for i in bit), 2) for bit in bit_ranges], dtype=np.int32)
for rotation in range(4):
xor = id_matrix ^ np.roll(zero_id, rotation)
# print(xor)
values, count = np.unique(xor, return_counts=True)
# print(value)
# if(values.shape == (1, )):
# return values[0], rotation
# else:
for value, freq in zip(values, count):
if freq >= 3:
return value, rotation
if freq >= 2:
error_bit = (sum([str(bin(element)).count("1") for element in (xor ^ value).tolist()]))
# print(error_bit)
if error_bit < 5:
return value, rotation
# for i in range(dictionary.shape[0]):
# print(dictionary[i][:])
# for rotation in range(4):
# # rotated = np.roll(dictionary[i][:], rotation)
# # print(rotated)
# cmp = id_matrix == rotated
# if cmp.all():
# return i, rotation
# break
return -1, 0
if __name__ == "__main__":
# image = cv2.imread('../markers/Marker000.png')
image = cv2.imread(sys.argv[1], cv2.IMREAD_GRAYSCALE)
# print(len(image.shape))
# _, marker = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# image = cv2.resize(image, (180, 180), interpolation=cv2.INTER_AREA)
# image = imutils.rotate(image, 270)
# image = cv2.flip(image, 1)
# cv2.imwrite(sys.argv[1], image)
bits = extract_bit(image)
print(bits)
print(get_id(bits))
cv2.imshow('marker', image)
cv2.imshow('thres', pre_process_image(image))
cv2.waitKey(0)