Parameterize thresholding blocksize and constant

This commit is contained in:
Vinayak Mehta
2017-04-10 21:15:54 +05:30
parent 8b07aa2702
commit 72233f25ce
5 changed files with 143 additions and 61 deletions
+18 -4
View File
@@ -139,6 +139,17 @@ class Lattice:
List of ints specifying m-tolerance parameters.
(optional, default: [2])
blocksize: int
Size of a pixel neighborhood that is used to calculate a
threshold value for the pixel: 3, 5, 7, and so on.
(optional, default: 15)
threshold_constant: float
Constant subtracted from the mean or weighted mean
(see the details below). Normally, it is positive but may be
zero or negative as well.
(optional, default: -2)
scale : int
Used to divide the height/width of a pdf to get a structuring
element for image processing.
@@ -177,15 +188,17 @@ class Lattice:
(optional, default: None)
"""
def __init__(self, table_area=None, fill=None, headers=None, mtol=[2],
scale=15, invert=False, margins=(1.0, 0.5, 0.1),
split_text=False, flag_size=True, shift_text=['l', 't'],
debug=None):
blocksize=15, threshold_constant=-2, scale=15, invert=False,
margins=(1.0, 0.5, 0.1), split_text=False, flag_size=True,
shift_text=['l', 't'], debug=None):
self.method = 'lattice'
self.table_area = table_area
self.fill = fill
self.headers = headers
self.mtol = mtol
self.blocksize = blocksize
self.threshold_constant = threshold_constant
self.scale = scale
self.invert = invert
self.char_margin, self.line_margin, self.word_margin = margins
@@ -230,7 +243,8 @@ class Lattice:
subprocess.call(gs_call, stdout=open(os.devnull, 'w'),
stderr=subprocess.STDOUT)
img, threshold = adaptive_threshold(imagename, invert=self.invert)
img, threshold = adaptive_threshold(imagename, invert=self.invert,
blocksize=self.blocksize, c=self.threshold_constant)
pdf_x = width
pdf_y = height
img_x = img.shape[1]
+17 -3
View File
@@ -27,6 +27,17 @@ class OCR:
List of ints specifying m-tolerance parameters.
(optional, default: [2])
blocksize: int
Size of a pixel neighborhood that is used to calculate a
threshold value for the pixel: 3, 5, 7, and so on.
(optional, default: 15)
threshold_constant: float
Constant subtracted from the mean or weighted mean
(see the details below). Normally, it is positive but may be
zero or negative as well.
(optional, default: -2)
dpi : int
Dots per inch.
(optional, default: 300)
@@ -46,12 +57,14 @@ class OCR:
of detected contours, lines, joints and the table generated.
(optional, default: None)
"""
def __init__(self, table_area=None, mtol=[2], dpi=300, lang="eng", scale=15,
debug=None):
def __init__(self, table_area=None, mtol=[2], blocksize=15, threshold_constant=-2,
dpi=300, lang="eng", scale=15, debug=None):
self.method = 'ocr'
self.table_area = table_area
self.mtol = mtol
self.blocksize = blocksize
self.threshold_constant = threshold_constant
self.tool = pyocr.get_available_tools()[0] # fix this
self.dpi = dpi
self.lang = lang
@@ -75,7 +88,8 @@ class OCR:
subprocess.call(gs_call, stdout=open(os.devnull, 'w'),
stderr=subprocess.STDOUT)
img, threshold = adaptive_threshold(imagename)
img, threshold = adaptive_threshold(imagename, blocksize=self.blocksize,
c=self.threshold_constant)
vmask, v_segments = find_lines(threshold, direction='vertical',
scale=self.scale)
hmask, h_segments = find_lines(threshold, direction='horizontal',
+11 -8
View File
@@ -570,14 +570,17 @@ def get_score(error_weights):
score : float
"""
SCORE_VAL = 100
score = 0
if sum([ew[0] for ew in error_weights]) != SCORE_VAL:
raise ValueError("Please assign a valid weightage to each parameter"
" such that their sum is equal to 100")
for ew in error_weights:
weight = ew[0] / len(ew[1])
for error_percentage in ew[1]:
score += weight * (1 - error_percentage)
try:
score = 0
if sum([ew[0] for ew in error_weights]) != SCORE_VAL:
raise ValueError("Please assign a valid weightage to each parameter"
" such that their sum is equal to 100")
for ew in error_weights:
weight = ew[0] / len(ew[1])
for error_percentage in ew[1]:
score += weight * (1 - error_percentage)
except ZeroDivisionError:
score = 0
return score