-
Notifications
You must be signed in to change notification settings - Fork 0
/
conncomp.py
63 lines (55 loc) · 2.07 KB
/
conncomp.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
import cv2
import numpy as np
def countZeroSequences(seq):
zero_sequences = np.array([0])
for i in seq:
if i == 0:
zero_sequences[len(zero_sequences) - 1] += 1
elif i != 0 and zero_sequences[len(zero_sequences) - 1] != 0:
zero_sequences = np.append(zero_sequences, 0)
else:
continue
return zero_sequences
def countNonZeroSequences(seq):
non_zero_sequences = np.array([0])
for i in seq:
if i != 0:
non_zero_sequences[len(non_zero_sequences) - 1] += 1
elif i == 0 and non_zero_sequences[len(non_zero_sequences) - 1] != 0:
non_zero_sequences = np.append(non_zero_sequences, 0)
else:
continue
return non_zero_sequences
def verticalProfileProjection(img):
count = np.zeros(img.shape[0])
test = cv2.bitwise_not(img)
for i in range(test.shape[0]):
count[i] = np.count_nonzero(test[i])
if count[i] == 0:
test[i, :] = 125
zero_count = countZeroSequences(count)
non_zero_count = countNonZeroSequences(count)
avg_line_spacing = np.mean(zero_count[1:len(zero_count) - 1])
avg_line_height = np.mean(non_zero_count)
return {"histogram": count,
"zero_count": zero_count,
"non_zero_count": non_zero_count,
"avg_line_spacing": avg_line_spacing,
"avg_line_height": avg_line_height}
def horizontalProfileProjection(img):
count = np.zeros(img.shape[1])
test = cv2.bitwise_not(img)
for i in range(test.shape[1]):
count[i] = np.count_nonzero(test[:, i])
if count[i] == 0:
test[:, i] = 125
zero_count = countZeroSequences(count)
non_zero_count = countNonZeroSequences(count)
# print zero_count, non_zero_count
avg_word_spacing = np.mean(zero_count[1:len(zero_count) - 1])
avg_word_width = np.mean(non_zero_count)
return {"histogram": count,
"zero_count": zero_count,
"non_zero_count": non_zero_count,
"avg_word_spacing": avg_word_spacing,
"avg_word_width": avg_word_width}