Skip to content

Commit 55a0190

Browse files
committed
better line segmentation algo
1 parent f7225ee commit 55a0190

File tree

4 files changed

+76
-17
lines changed

4 files changed

+76
-17
lines changed
Loading
Loading
Loading

Character_Segmentation/main.py

+76-17
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,59 @@ def closewindows():
1010
if k & 0xFF == ord('s'):
1111
comment = input("Comment:-\n ")
1212
cv2.imwrite('./data/test_result/'+comment+'_thres'+'.jpg',final_thr)
13-
cv2.imwrite('./data/test_result/'+comment+'_src'+'.jpg',src_img)
13+
cv2.imwrite('./data/test_result/'+comment+'_adpthres'+'.jpg',gud_img)
1414
cv2.imwrite('./data/test_result/'+comment+'_contr'+'.jpg',final_contr)
1515
print("Completed")
1616
elif k & 0xFF == int(27):
1717
cv2.destroyAllWindows()
1818
else:
1919
closewindows()
2020

21-
def line_array(array):
22-
list_x = []
23-
for y in range(len(array)):
24-
if all(i >= 3 for i in array[y:y+9]) == True:
25-
list_x.append(y-1)
26-
return list_x
21+
def strtline(y, array):
22+
count_ahead = 0
23+
count_prev = 0
24+
for i in array[y:y+20]:
25+
if i > 3:
26+
count_ahead+= 1
27+
for i in array[y-10:y]:
28+
if i==0:
29+
count_prev += 1
30+
return count_ahead, count_prev
31+
32+
def endline(y, array):
33+
count_ahead = 0
34+
count_prev = 0
35+
for i in array[y:y+10]:
36+
if i==0:
37+
count_ahead+= 1
38+
for i in array[y-20:y]:
39+
if i >3:
40+
count_prev += 1
41+
return count_ahead, count_prev
2742

43+
def line_array(array):
44+
list_x_upper = []
45+
list_x_lower = []
46+
for y in range(5, len(array)-5):
47+
s_a, s_p = strtline(y, array)
48+
e_a, e_p = endline(y, array)
49+
if s_a>=16 and s_p>=7:
50+
list_x_upper.append(y)
51+
# noise_remove[y][:] = 255
52+
if e_a>=5 and e_p>=16:
53+
list_x_lower.append(y)
54+
# noise_remove[y][:] = 255
55+
return list_x_upper, list_x_lower
56+
57+
# def refine_array(count_array,array_upper, array_lower):
58+
# for y in array_upper:
59+
# if all(i >= 3 for i in count_array[y:y+10]) == False:
60+
# array_upper.remove(y)
61+
# for y in array_lower:
62+
# if all(i >= 3 for i in count_array[y-10:y]) == False:
63+
# array_lower.remove(y)
64+
65+
# return array_upper, array_lower
2866

2967
#-------------Thresholding Image--------------#
3068

@@ -33,27 +71,30 @@ def line_array(array):
3371
# src_img = cv2.resize(copy, dsize =(1500, 1000), interpolation = cv2.INTER_AREA)
3472
height = src_img.shape[0]
3573
width = src_img.shape[1]
36-
print("#----------------------------#")
37-
print("Image Info:-")
74+
print("#---------Image Info:--------#")
3875
print("Height =",height,"\nWidth =",width)
3976
print("#----------------------------#")
4077

4178
grey_img = cv2.cvtColor(src_img, cv2.COLOR_BGR2GRAY)
4279

4380
gud_img = cv2.adaptiveThreshold(grey_img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,101,2)
4481

82+
# edges = cv2.Canny(grey_img,100,200)
83+
4584
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
4685
noise_remove = cv2.erode(gud_img,kernel,iterations = 2)
4786

48-
kernel1 = np.array([[1,0,1],[0,1,0],[1,0,1]], dtype = np.uint8)
87+
4988

5089
opening = cv2.morphologyEx(gud_img, cv2.MORPH_OPEN, kernel, iterations = 2) # To remove "pepper-noise"
5190
kernel1 = np.array([[1,0,1],[0,1,0],[1,0,1]], dtype = np.uint8)
52-
final_thr = cv2.dilate(noise_remove,kernel1,iterations = 3)
91+
final_thr = cv2.dilate(opening,kernel1,iterations = 1)
5392

5493
#-------------/Thresholding Image-------------#
5594

5695

96+
97+
5798
#-------------Line Detection------------------#
5899

59100
count_x = np.zeros(shape= (height))
@@ -63,12 +104,28 @@ def line_array(array):
63104
count_x[y] = count_x[y]+1
64105
# print(count_x[y])
65106

66-
line_list = line_array(count_x)
107+
# line_list_upper, line_list_lower = line_array(count_x)
67108
# print(line_list)
68109

69-
# t = np.arange(0,height, 1)
70-
# plt.plot(t, count_x[t])
71-
# plt.axis([0, height, 0, 350])
110+
upper_lines, lower_lines = line_array(count_x)
111+
112+
113+
114+
if len(upper_lines)==len(lower_lines):
115+
print("Oh Yeah!!!")
116+
117+
for y in upper_lines:
118+
noise_remove[y][:] = 255
119+
120+
for y in lower_lines:
121+
noise_remove[y][:] = 255
122+
123+
124+
t = np.arange(0,height, 1)
125+
plt.plot(t, count_x[t])
126+
plt.axis([0, height, 0, 350])
127+
128+
72129

73130

74131
# for y in range(len(line_list)):
@@ -81,7 +138,7 @@ def line_array(array):
81138
# print(main_list)
82139

83140
# for y in main_list:
84-
# src_img[y][:] = (0 , 255,0)
141+
# src_img[y][:] = (0 , 255,0)
85142

86143

87144

@@ -124,12 +181,14 @@ def line_array(array):
124181
cv2.namedWindow('Source Image', cv2.WINDOW_NORMAL)
125182
cv2.namedWindow('Threshold Image', cv2.WINDOW_NORMAL)
126183
cv2.namedWindow('Contour Image', cv2.WINDOW_NORMAL)
184+
cv2.namedWindow('noise_remove Image', cv2.WINDOW_NORMAL)
127185

128186
cv2.imshow("Source Image", src_img)
129187
cv2.imshow("Threshold Image", final_thr)
130188
cv2.imshow("Contour Image", final_contr)
189+
cv2.imshow('noise_remove Image', noise_remove)
131190

132-
# plt.show()
191+
plt.show()
133192

134193
#-------------/Displaying Image---------------#
135194

0 commit comments

Comments
 (0)