Преглед изворни кода

Added ability to filter out non-greys before trying to extract the
crossword.

Andrew Klopper пре 6 година
родитељ
комит
3de3b2f972
2 измењених фајлова са 26 додато и 4 уклоњено
  1. 4 1
      home/views.py
  2. 22 3
      home/xword.py

+ 4 - 1
home/views.py

@@ -15,7 +15,10 @@ class HomeView(View):
15 15
         form = CrosswordForm(request.POST, request.FILES)
16 16
         if not form.is_valid():
17 17
             return HttpResponseBadRequest('Invalid form data')
18
-        image, warnings = extract_crossword(form.cleaned_data['file'].temporary_file_path())
18
+        image, warnings = extract_crossword(
19
+            form.cleaned_data['file'].temporary_file_path(),
20
+            filter_colours=False
21
+        )
19 22
         return render(request, 'home/output.html', {
20 23
             'warnings': warnings,
21 24
             'image_file_name': 'xword_{}.png'.format(datetime.now().strftime('%Y%m%d_%H%M%S')),

+ 22 - 3
home/xword.py

@@ -5,6 +5,25 @@ import copy
5 5
 import argparse
6 6
 
7 7
 
8
+def non_greys_to_white(img, threshold=48):
9
+    b, g, r = cv2.split(img)
10
+    rgb_diff = cv2.subtract(cv2.max(cv2.max(b, g), r), cv2.min(cv2.min(b, g), r))
11
+    filtered = img.copy()
12
+    filtered[np.where(rgb_diff > threshold)] = (255, 255, 255)
13
+    return filtered
14
+
15
+
16
+def load_image_as_greyscale(file_name, filter_colours, colour_filter_threshold):
17
+    img = cv2.imread(file_name)
18
+    if img is None:
19
+        raise RuntimeError("Failed to load image")
20
+
21
+    if filter_colours:
22
+        img = non_greys_to_white(img, colour_filter_threshold)
23
+
24
+    return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
25
+
26
+
8 27
 def preprocess_image(original, gaussian_blur_size, adaptive_threshold_block_size, adaptive_threshold_mean_adjustment, num_dilations):
9 28
     img = cv2.GaussianBlur(original, (gaussian_blur_size, gaussian_blur_size), 0)
10 29
     img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, adaptive_threshold_block_size, adaptive_threshold_mean_adjustment)
@@ -179,6 +198,8 @@ def show_image(image):
179 198
 
180 199
 def extract_crossword(
181 200
     file_name,
201
+    filter_colours=False,
202
+    colour_filter_threshold=48,
182 203
     gaussian_blur_size=11,
183 204
     adaptive_threshold_block_size=11,
184 205
     adaptive_threshold_mean_adjustment=2,
@@ -196,9 +217,7 @@ def extract_crossword(
196 217
 ):
197 218
     warnings = []
198 219
 
199
-    original = cv2.imread(file_name, cv2.IMREAD_GRAYSCALE)
200
-    if original is None:
201
-        raise RuntimeError("Failed to load image")
220
+    original = load_image_as_greyscale(file_name, filter_colours, colour_filter_threshold)
202 221
 
203 222
     img = preprocess_image(original, gaussian_blur_size, adaptive_threshold_block_size, adaptive_threshold_mean_adjustment, num_dilations)
204 223