-
Notifications
You must be signed in to change notification settings - Fork 1
/
process.py
243 lines (180 loc) · 6.95 KB
/
process.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import cv2
import numpy as np
import os
import pprint
from collections import Counter
from random import randrange
# Nessecary for show image to work
os.environ["QT_QPA_PLATFORM"] = "xcb"
frame1 = "frames/00131.png"
frame2 = "frames/06413.png"
frame3 = "frames/04294.png"
frame4 = "frames/00544.png"
frame5 = "frames/02733.png"
blank_black = "frames/00001.png"
blank_white = "frames/02764.png"
def _base_split(imageData, min_region_size, dominant_value):
regions = []
def recursion(imageData):
image, coordinates = imageData
h, w = image.shape
x, y = coordinates
# Check if the region is small enough
if (
h * w <= min_region_size
or (image == dominant_value).sum() < 3
or (image == dominant_value).all()
):
regions.append(imageData) # Return a single region
return
# Split the image into quadrants
mid_x = w // 2
mid_y = h // 2
top_left = (image[0:mid_y, 0:mid_x], (x, y))
top_right = (image[0:mid_y, mid_x:w], (x + mid_x, y))
bottom_left = (image[mid_y:h, 0:mid_x], (x, y + mid_y))
bottom_right = (image[mid_y:h, mid_x:w], (x + mid_x, y + mid_y))
# Recursively split each quadrant
recursion(top_left),
recursion(top_right),
recursion(bottom_left),
recursion(bottom_right)
recursion(imageData)
return regions
def _merge(region1, region2):
image1, coordinates1 = region1
image2, coordinates2 = region2
x1, y1 = coordinates1
x2, y2 = coordinates2
h1, w1 = image1.shape
h2, w2 = image2.shape
x = min(x1, x2)
y = min(y1, y2)
if not (w1 == w2 and x1 == x2):
axis = 1
if not (h1 == h2 and y1 == y2):
axis = 0
image = np.concatenate((image1, image2), axis=axis)
return (image, (x, y))
def _should_merge(region1, region2, threshold=20):
image1, coordinates1 = region1
image2, coordinates2 = region2
h1, w1 = image1.shape
h2, w2 = image2.shape
x1, y1 = coordinates1
x2, y2 = coordinates2
# return True
isShapeMatching = (w1 == w2 and x1 == x2) or (h1 == h2 and y1 == y2)
if not isShapeMatching:
return False
isBorderingX = x1 == x2 + w2 or x2 == x1 + w1
isBorderingY = y1 == y2 + h2 or y2 == y1 + h1
if not isBorderingX and not isBorderingY:
return False
imageDifference = np.mean(image2) - np.mean(image1)
if imageDifference <= threshold and imageDifference >= threshold * -1:
return True
pixel_threshold = 5
amount_zero_1 = (image1 == 0).sum()
amount_255_1 = (image1 == 255).sum()
amount_zero_2 = (image2 == 0).sum()
amount_255_2 = (image2 == 255).sum()
if amount_zero_1 < pixel_threshold and amount_zero_2 < pixel_threshold:
return True
if amount_255_1 < pixel_threshold and amount_255_2 < pixel_threshold:
return True
return False
def _split_and_merge(imageData, chunk_size, dominant_value):
base_h, base_w = imageData[0].shape
regions = _base_split(imageData, chunk_size, dominant_value)
def recursive_merge(regions, threshold):
initial_length = len(regions)
merged_ones = []
for i in range(initial_length):
# for j in range(i + 1, len(regions)):
for j in range(initial_length):
if i == j:
continue
if regions[i] == None or regions[j] == None:
continue
if _should_merge(regions[i], regions[j], threshold):
merged_ones.append(_merge(regions[i], regions[j]))
regions[j] = None
regions[i] = None
# Remove marked regions
regions[:] = [r for r in regions if r is not None]
new_regions = [*regions, *merged_ones]
if len(new_regions) == initial_length:
return new_regions
return recursive_merge(new_regions, threshold)
merged = recursive_merge(regions, threshold=20)
whites_removed = list(filter(lambda x: np.mean(x[0]) <= 120, merged))
final_merged = recursive_merge(whites_removed, 255)
# Target array size
target_size = 64
if len(final_merged) == 0:
for _ in range(target_size):
final_merged.append(
(np.zeros((1, 1), np.uint8), (int(-1), int(base_h + 1)))
)
# Sort the array based on the size of the images
sorted_array = sorted(final_merged, key=lambda x: x[0].size)
elements_to_adjust = target_size - len(sorted_array)
if elements_to_adjust > 0:
# Add new images by splitting the largest ones
for _ in range(elements_to_adjust):
largest_image, coordinates = sorted_array.pop()
x, y = coordinates
half_height = largest_image.shape[0] // 2
half_width = largest_image.shape[1] // 2
if half_height >= half_width:
# Split the image into two halves
image1 = (largest_image[:half_height, :], coordinates)
image2 = (largest_image[half_height:, :], (x, y + half_height))
else:
image1 = (largest_image[:, :half_width], coordinates)
image2 = (largest_image[:, half_width:], (x + half_width, y))
# Add the new images to the array
sorted_array.extend([image1, image2])
sorted_array = sorted(sorted_array, key=lambda x: x[0].size)
else:
# Remove the smallest images until the target size is reached
for _ in range(-elements_to_adjust):
sorted_array.pop(0) # Remove the smallest image
return sorted_array
def process_frame(frame):
# Read the binary image (thresholded image)
binary_image = cv2.imread(frame, cv2.IMREAD_REDUCED_GRAYSCALE_2)
# binary_image = cv2.GaussianBlur(binary_image, (5,5), cv2.BORDER_DEFAULT )
binary_image = cv2.threshold(binary_image, 128, 255, cv2.THRESH_BINARY)[1]
imgUint8 = binary_image.astype(np.uint8)
blackMask = imgUint8 == 0
blackPixels = np.sum(blackMask)
whitePixels = np.sum(~blackMask)
if blackPixels > whitePixels:
dominant_value = 0
# binary_image = cv2.bitwise_not(binary_image)
else:
dominant_value = 255
# Apply split-and-merge segmentation
min_region_size = 25 # Adjust this parameter as needed
regions = _split_and_merge((binary_image, (0, 0)), min_region_size, dominant_value)
# TODO Add sorting here maybe?
return regions
def show_results():
imagesArray = (
process_frame(blank_white),
process_frame(blank_black),
# process_frame(frame5),
)
for index, images in enumerate(imagesArray):
cv2.imshow("output" + str(index), images[0])
cv2.imshow("wireframe" + str(index), images[1])
cv2.imshow("binary" + str(index), images[2])
while 1:
key = cv2.waitKey(0)
# Escape
if key == 27:
cv2.destroyAllWindows()
break
# show_results()