diff --git a/main.py b/main.py index 20eeb93..85cde4f 100644 --- a/main.py +++ b/main.py @@ -12,8 +12,6 @@ for root, dirs, files in os.walk("data"): if file.startswith("receipt-08"): image = load_image("data/"+file) receipt = cut_receipt(image, draw_steps=True) - plt.imshow(receipt) - plt.show() lines = find_lines(receipt) for line in lines: plt.imshow(line, cmap="gray") diff --git a/src/processing/linefinder.py b/src/processing/linefinder.py index 46826ec..0bfc681 100644 --- a/src/processing/linefinder.py +++ b/src/processing/linefinder.py @@ -2,15 +2,11 @@ import numpy as np from scipy.ndimage import measurements from src.processing.imageprocessing import rgb2gray -from src.processing.loader import load_numpy, save_numpy, save_image, load_image -from src.utils.cmap_generator import rand_cmap, list_cmap -from matplotlib import pyplot as plt +from src.processing.loader import load_numpy, save_numpy, save_image def find_lines(image): gray, binary, magnitude = preparation(image) - plt.imshow(binary, cmap="gray") - plt.show() backtrack = load_numpy("result/backtrack.npz") if backtrack is None: energy, backtrack = minimum_seam(binary, magnitude) @@ -18,7 +14,8 @@ def find_lines(image): save_image("result/gray.png", gray) seams = calculate_seams(backtrack) labeled, ncomponents = group_empty_boxes(seams) - return generate_lines(labeled, ncomponents, gray) + lines = generate_lines(labeled, ncomponents, gray) + return filter_lines(lines) def preparation(image): @@ -160,4 +157,15 @@ def generate_lines(labeled, ncomponents, gray): else: pixelgroup = np.concatenate((pixelgroup, pixel)) submit_entry() - return entries \ No newline at end of file + return entries + +def filter_lines(lines): + filtered = [] + for line in lines: + cnt, vals = np.histogram(line, 256) + threshold = get_threshold(cnt)/256*1.13#*0.96 + binary = (line > threshold).astype(np.int_) + labeled, ncomponents = group_empty_boxes(binary) + if ncomponents > 2: + filtered.append(line) + return filtered \ No newline at end of file diff --git a/src/processing/receiptcutter.py b/src/processing/receiptcutter.py index faba022..4c6935a 100644 --- a/src/processing/receiptcutter.py +++ b/src/processing/receiptcutter.py @@ -2,7 +2,6 @@ from collections import defaultdict from skimage.transform import resize from scipy.ndimage import gaussian_filter -from matplotlib import pyplot as plt from src.processing.imageprocessing import rgb2gray_value from scipy import signal @@ -162,8 +161,8 @@ def draw_hough_lines(image, scale, results, references, shape, theta_res=5, widt y = int(n - x * m) if 0 < y < image.shape[0]: draw_image[max(0, y - GREEN_WIDTH):y + GREEN_WIDTH, max(0, x - GREEN_WIDTH):x + GREEN_WIDTH] = np.array([255, 0, 0]) - plt.imshow(draw_image) - plt.show() + #plt.imshow(draw_image) + #plt.show() def convert_to_lines(scale, results, references, shape, theta_res=5, width_res=5): @@ -343,8 +342,8 @@ def draw_rectangle(image, corners): x = int(a[1] + (b[1] - a[1]) * i / 5000) y = int(a[0] + (b[0] - a[0]) * i / 5000) draw_image[max(0, y - 15):y + 10, max(0, x - 10):x + 10] = np.array([255, 0, 0]) - plt.imshow(draw_image) - plt.show() + #plt.imshow(draw_image) + #plt.show() def crop_image(image, corners): diff --git a/src/utils/cmap_generator.py b/src/utils/cmap_generator.py index d264376..5f5cf46 100644 --- a/src/utils/cmap_generator.py +++ b/src/utils/cmap_generator.py @@ -2,7 +2,7 @@ from matplotlib.colors import LinearSegmentedColormap import colorsys import numpy as np -def rand_cmap(nlabels, type='bright', first_color_black=True, last_color_black=False, verbose=True): +def rand_cmap(nlabels, type='bright', first_color_black=True, last_color_black=False, verbose=False): """ Creates a random colormap to be used together with matplotlib. Useful for segmentation tasks :param nlabels: Number of labels (size of colormap)