Even more improved line detection
This commit is contained in:
2
main.py
2
main.py
@@ -12,8 +12,6 @@ for root, dirs, files in os.walk("data"):
|
|||||||
if file.startswith("receipt-08"):
|
if file.startswith("receipt-08"):
|
||||||
image = load_image("data/"+file)
|
image = load_image("data/"+file)
|
||||||
receipt = cut_receipt(image, draw_steps=True)
|
receipt = cut_receipt(image, draw_steps=True)
|
||||||
plt.imshow(receipt)
|
|
||||||
plt.show()
|
|
||||||
lines = find_lines(receipt)
|
lines = find_lines(receipt)
|
||||||
for line in lines:
|
for line in lines:
|
||||||
plt.imshow(line, cmap="gray")
|
plt.imshow(line, cmap="gray")
|
||||||
|
|||||||
@@ -2,15 +2,11 @@ import numpy as np
|
|||||||
from scipy.ndimage import measurements
|
from scipy.ndimage import measurements
|
||||||
|
|
||||||
from src.processing.imageprocessing import rgb2gray
|
from src.processing.imageprocessing import rgb2gray
|
||||||
from src.processing.loader import load_numpy, save_numpy, save_image, load_image
|
from src.processing.loader import load_numpy, save_numpy, save_image
|
||||||
from src.utils.cmap_generator import rand_cmap, list_cmap
|
|
||||||
from matplotlib import pyplot as plt
|
|
||||||
|
|
||||||
|
|
||||||
def find_lines(image):
|
def find_lines(image):
|
||||||
gray, binary, magnitude = preparation(image)
|
gray, binary, magnitude = preparation(image)
|
||||||
plt.imshow(binary, cmap="gray")
|
|
||||||
plt.show()
|
|
||||||
backtrack = load_numpy("result/backtrack.npz")
|
backtrack = load_numpy("result/backtrack.npz")
|
||||||
if backtrack is None:
|
if backtrack is None:
|
||||||
energy, backtrack = minimum_seam(binary, magnitude)
|
energy, backtrack = minimum_seam(binary, magnitude)
|
||||||
@@ -18,7 +14,8 @@ def find_lines(image):
|
|||||||
save_image("result/gray.png", gray)
|
save_image("result/gray.png", gray)
|
||||||
seams = calculate_seams(backtrack)
|
seams = calculate_seams(backtrack)
|
||||||
labeled, ncomponents = group_empty_boxes(seams)
|
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):
|
def preparation(image):
|
||||||
@@ -160,4 +157,15 @@ def generate_lines(labeled, ncomponents, gray):
|
|||||||
else:
|
else:
|
||||||
pixelgroup = np.concatenate((pixelgroup, pixel))
|
pixelgroup = np.concatenate((pixelgroup, pixel))
|
||||||
submit_entry()
|
submit_entry()
|
||||||
return entries
|
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
|
||||||
@@ -2,7 +2,6 @@ from collections import defaultdict
|
|||||||
|
|
||||||
from skimage.transform import resize
|
from skimage.transform import resize
|
||||||
from scipy.ndimage import gaussian_filter
|
from scipy.ndimage import gaussian_filter
|
||||||
from matplotlib import pyplot as plt
|
|
||||||
|
|
||||||
from src.processing.imageprocessing import rgb2gray_value
|
from src.processing.imageprocessing import rgb2gray_value
|
||||||
from scipy import signal
|
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)
|
y = int(n - x * m)
|
||||||
if 0 < y < image.shape[0]:
|
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])
|
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.imshow(draw_image)
|
||||||
plt.show()
|
#plt.show()
|
||||||
|
|
||||||
|
|
||||||
def convert_to_lines(scale, results, references, shape, theta_res=5, width_res=5):
|
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)
|
x = int(a[1] + (b[1] - a[1]) * i / 5000)
|
||||||
y = int(a[0] + (b[0] - a[0]) * 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])
|
draw_image[max(0, y - 15):y + 10, max(0, x - 10):x + 10] = np.array([255, 0, 0])
|
||||||
plt.imshow(draw_image)
|
#plt.imshow(draw_image)
|
||||||
plt.show()
|
#plt.show()
|
||||||
|
|
||||||
|
|
||||||
def crop_image(image, corners):
|
def crop_image(image, corners):
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ from matplotlib.colors import LinearSegmentedColormap
|
|||||||
import colorsys
|
import colorsys
|
||||||
import numpy as np
|
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
|
Creates a random colormap to be used together with matplotlib. Useful for segmentation tasks
|
||||||
:param nlabels: Number of labels (size of colormap)
|
:param nlabels: Number of labels (size of colormap)
|
||||||
|
|||||||
Reference in New Issue
Block a user