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

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
         form = CrosswordForm(request.POST, request.FILES)
15
         form = CrosswordForm(request.POST, request.FILES)
16
         if not form.is_valid():
16
         if not form.is_valid():
17
             return HttpResponseBadRequest('Invalid form data')
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
         return render(request, 'home/output.html', {
22
         return render(request, 'home/output.html', {
20
             'warnings': warnings,
23
             'warnings': warnings,
21
             'image_file_name': 'xword_{}.png'.format(datetime.now().strftime('%Y%m%d_%H%M%S')),
24
             'image_file_name': 'xword_{}.png'.format(datetime.now().strftime('%Y%m%d_%H%M%S')),

+ 22 - 3
home/xword.py

5
 import argparse
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
 def preprocess_image(original, gaussian_blur_size, adaptive_threshold_block_size, adaptive_threshold_mean_adjustment, num_dilations):
27
 def preprocess_image(original, gaussian_blur_size, adaptive_threshold_block_size, adaptive_threshold_mean_adjustment, num_dilations):
9
     img = cv2.GaussianBlur(original, (gaussian_blur_size, gaussian_blur_size), 0)
28
     img = cv2.GaussianBlur(original, (gaussian_blur_size, gaussian_blur_size), 0)
10
     img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, adaptive_threshold_block_size, adaptive_threshold_mean_adjustment)
29
     img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, adaptive_threshold_block_size, adaptive_threshold_mean_adjustment)
179
 
198
 
180
 def extract_crossword(
199
 def extract_crossword(
181
     file_name,
200
     file_name,
201
+    filter_colours=False,
202
+    colour_filter_threshold=48,
182
     gaussian_blur_size=11,
203
     gaussian_blur_size=11,
183
     adaptive_threshold_block_size=11,
204
     adaptive_threshold_block_size=11,
184
     adaptive_threshold_mean_adjustment=2,
205
     adaptive_threshold_mean_adjustment=2,
196
 ):
217
 ):
197
     warnings = []
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
     img = preprocess_image(original, gaussian_blur_size, adaptive_threshold_block_size, adaptive_threshold_mean_adjustment, num_dilations)
222
     img = preprocess_image(original, gaussian_blur_size, adaptive_threshold_block_size, adaptive_threshold_mean_adjustment, num_dilations)
204
 
223