Bladeren bron

Change grid colour threshold detection method.

Andrew Klopper 6 jaren geleden
bovenliggende
commit
dfe0a1ed67
1 gewijzigde bestanden met toevoegingen van 16 en 2 verwijderingen
  1. 16 2
      home/xword.py

+ 16 - 2
home/xword.py

1
+import itertools
1
 import math
2
 import math
2
 import cv2
3
 import cv2
3
 import numpy as np
4
 import numpy as np
152
     return grid
153
     return grid
153
 
154
 
154
 
155
 
156
+def get_grid_colour_threshold(grid_colours):
157
+    pixels = sorted(itertools.chain.from_iterable(grid_colours))
158
+    delta_max = -1
159
+    i_max = -1
160
+    for i in range(1, len(pixels)):
161
+        delta = pixels[i] - pixels[i - 1]
162
+        if delta > delta_max:
163
+            delta_max = delta
164
+            i_max = i
165
+    return (pixels[i_max] + pixels[i_max - 1]) / 2
166
+
167
+
155
 def grid_colours_to_blocks(grid_colours, num_rows, num_cols, sampling_threshold):
168
 def grid_colours_to_blocks(grid_colours, num_rows, num_cols, sampling_threshold):
156
     grid = copy.deepcopy(grid_colours)
169
     grid = copy.deepcopy(grid_colours)
157
     warning = False
170
     warning = False
246
         warnings.append("Crossword is not square")
259
         warnings.append("Crossword is not square")
247
 
260
 
248
     block_img = extract_square(original, top_left, top_right, bottom_right, bottom_left)
261
     block_img = extract_square(original, top_left, top_right, bottom_right, bottom_left)
262
+    grid_colours = extract_grid_colours(block_img, num_rows, num_cols, sampling_block_size_ratio)
249
 
263
 
250
     if sampling_threshold is None:
264
     if sampling_threshold is None:
251
-        sampling_threshold = get_threshold_from_quantile(block_img, sampling_threshold_quantile)
265
+        #sampling_threshold = get_threshold_from_quantile(block_img, sampling_threshold_quantile)
266
+        sampling_threshold = get_grid_colour_threshold(grid_colours)
252
     else:
267
     else:
253
         sampling_threshold = sampling_threshold
268
         sampling_threshold = sampling_threshold
254
 
269
 
255
-    grid_colours = extract_grid_colours(block_img, num_rows, num_cols, sampling_block_size_ratio)
256
     warning, grid = grid_colours_to_blocks(grid_colours, num_rows, num_cols, sampling_threshold)
270
     warning, grid = grid_colours_to_blocks(grid_colours, num_rows, num_cols, sampling_threshold)
257
     if warning:
271
     if warning:
258
         warnings.append("Some blocks may be the wrong colour")
272
         warnings.append("Some blocks may be the wrong colour")