-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions_lines.py
83 lines (67 loc) · 2.18 KB
/
functions_lines.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import cv2
import numpy as np
def findLines(bw_image, LinesThres):
# making horizontal projections
horProj = cv2.reduce(bw_image, 1, cv2.cv.CV_REDUCE_AVG)
# make hist - same dimension as horProj - if 0 (space), then True, else False
th = 0; # black pixels threshold value. this represents the space lines
hist = horProj <= th;
#Get mean coordinate of white white pixels groups
ycoords = []
y = 0
count = 0
isSpace = False
for i in range(0, bw_image.shape[0]):
if (not isSpace):
if (hist[i]): #if space is detected, get the first starting y-coordinates and start count at 1
isSpace = True
count = 1
y = i
else:
if (not hist[i]):
isSpace = False
#when smoothing, thin letters will breakdown, creating a new blank lines or pixel rows, but the count will be small, so we set a threshold.
if (count >=LinesThres):
ycoords.append(y / count)
else:
y = y + i
count = count + 1
ycoords.append(y / count)
#returns y-coordinates of the lines found
return ycoords
def LinesMedian(bw_image):
# making horizontal projections
horProj = cv2.reduce(bw_image, 1, cv2.cv.CV_REDUCE_AVG)
# make hist - same dimension as horProj - if 0 (space), then True, else False
th = 0; # black pixels threshold value. this represents the space lines
hist = horProj <= th;
#Get mean coordinate of white white pixels groups
ycoords = []
y = 0
count = 0
isSpace = False
median_count = []
for i in range(0, bw_image.shape[0]):
if (not isSpace):
if (hist[i]): #if space is detected, get the first starting y-coordinates and start count at 1
isSpace = True
count = 1
#y = i
else:
if (not hist[i]):
isSpace = False
median_count.append(count)
else:
#y = y + i
count = count + 1
median_count.append(count)
#ycoords.append(y / count)
#returns counts of each blank rows of each of the lines found
return median_count
def get_lines_threshold(percent, img_for_det):
ThresPercent = percent
LinMed = LinesMedian(img_for_det)
LinMed = sorted(LinMed)
LinesThres = LinMed[len(LinMed)/3]*(ThresPercent/100.0)
LinesThres = int(LinesThres)
return LinesThres