@@ -10,21 +10,59 @@ def closewindows():
10
10
if k & 0xFF == ord ('s' ):
11
11
comment = input ("Comment:-\n " )
12
12
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 )
14
14
cv2 .imwrite ('./data/test_result/' + comment + '_contr' + '.jpg' ,final_contr )
15
15
print ("Completed" )
16
16
elif k & 0xFF == int (27 ):
17
17
cv2 .destroyAllWindows ()
18
18
else :
19
19
closewindows ()
20
20
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
27
42
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
28
66
29
67
#-------------Thresholding Image--------------#
30
68
@@ -33,27 +71,30 @@ def line_array(array):
33
71
# src_img = cv2.resize(copy, dsize =(1500, 1000), interpolation = cv2.INTER_AREA)
34
72
height = src_img .shape [0 ]
35
73
width = src_img .shape [1 ]
36
- print ("#----------------------------#" )
37
- print ("Image Info:-" )
74
+ print ("#---------Image Info:--------#" )
38
75
print ("Height =" ,height ,"\n Width =" ,width )
39
76
print ("#----------------------------#" )
40
77
41
78
grey_img = cv2 .cvtColor (src_img , cv2 .COLOR_BGR2GRAY )
42
79
43
80
gud_img = cv2 .adaptiveThreshold (grey_img ,255 ,cv2 .ADAPTIVE_THRESH_MEAN_C ,cv2 .THRESH_BINARY_INV ,101 ,2 )
44
81
82
+ # edges = cv2.Canny(grey_img,100,200)
83
+
45
84
kernel = cv2 .getStructuringElement (cv2 .MORPH_ELLIPSE ,(3 ,3 ))
46
85
noise_remove = cv2 .erode (gud_img ,kernel ,iterations = 2 )
47
86
48
- kernel1 = np . array ([[ 1 , 0 , 1 ],[ 0 , 1 , 0 ],[ 1 , 0 , 1 ]], dtype = np . uint8 )
87
+
49
88
50
89
opening = cv2 .morphologyEx (gud_img , cv2 .MORPH_OPEN , kernel , iterations = 2 ) # To remove "pepper-noise"
51
90
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 )
53
92
54
93
#-------------/Thresholding Image-------------#
55
94
56
95
96
+
97
+
57
98
#-------------Line Detection------------------#
58
99
59
100
count_x = np .zeros (shape = (height ))
@@ -63,12 +104,28 @@ def line_array(array):
63
104
count_x [y ] = count_x [y ]+ 1
64
105
# print(count_x[y])
65
106
66
- line_list = line_array (count_x )
107
+ # line_list_upper, line_list_lower = line_array(count_x)
67
108
# print(line_list)
68
109
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
+
72
129
73
130
74
131
# for y in range(len(line_list)):
@@ -81,7 +138,7 @@ def line_array(array):
81
138
# print(main_list)
82
139
83
140
# for y in main_list:
84
- # src_img[y][:] = (0 , 255,0)
141
+ # src_img[y][:] = (0 , 255,0)
85
142
86
143
87
144
@@ -124,12 +181,14 @@ def line_array(array):
124
181
cv2 .namedWindow ('Source Image' , cv2 .WINDOW_NORMAL )
125
182
cv2 .namedWindow ('Threshold Image' , cv2 .WINDOW_NORMAL )
126
183
cv2 .namedWindow ('Contour Image' , cv2 .WINDOW_NORMAL )
184
+ cv2 .namedWindow ('noise_remove Image' , cv2 .WINDOW_NORMAL )
127
185
128
186
cv2 .imshow ("Source Image" , src_img )
129
187
cv2 .imshow ("Threshold Image" , final_thr )
130
188
cv2 .imshow ("Contour Image" , final_contr )
189
+ cv2 .imshow ('noise_remove Image' , noise_remove )
131
190
132
- # plt.show()
191
+ plt .show ()
133
192
134
193
#-------------/Displaying Image---------------#
135
194
0 commit comments