@@ -68,10 +68,16 @@ def get_gaussian_kernel(sigma):
6868
6969def edge_detection_x (img ):
7070
71+ # x_kernel = [
72+ # [ 0, 1, 2],
73+ # [ -1, 0, 1],
74+ # [ -2, -1 , 0]
75+ # ]
76+
7177 x_kernel = [
72- [ 0 , 1 , 2 ],
7378 [ - 1 , 0 , 1 ],
74- [ - 2 , - 1 , 0 ]
79+ [ - 2 , 0 , 2 ],
80+ [ - 1 , 0 , 1 ]
7581 ]
7682
7783 edge_x_img = convolve_img (img ,x_kernel ,1 )
@@ -115,12 +121,12 @@ def cast_vote(accumulator, x, y):
115121
116122 theta_rad = math .radians (theta )
117123
118- p = (( x * math .cos (theta_rad )) + (y * math .sin (theta_rad )))
124+ p = int ( round (( x * math .cos (theta_rad )) + (y * math .sin (theta_rad ) )))
119125 # print(p)
120126
121127 # print(accumulator)
122128 if p < cl and p > - 1 :
123- accumulator [theta + 180 ][int ( p ) ] += 1
129+ accumulator [theta + 180 ][p ] += 1
124130
125131
126132
@@ -136,7 +142,7 @@ def mark_lines(max_theta, max_p, img):
136142 for i in range (h ):
137143 # p = x cosθ + y sinθ
138144 # x = (p - y sinθ) / cosθ
139- j = int (( max_p - (i * math .sin (theta_rad ))) / math .cos (theta_rad ))
145+ j = int (round (( max_p - (i * math .sin (theta_rad ))) / math .cos (theta_rad ) ))
140146 if j < w and j > - 1 :
141147 img [i ][j ] = [0 ,255 ,0 ]
142148 return img
@@ -164,7 +170,7 @@ def main():
164170
165171 diagonal_length = math .ceil (math .sqrt (h ** 2 + w ** 2 ))
166172
167- accumulator = np .zeros ([360 ,diagonal_length * 2 ])
173+ accumulator = np .zeros ([360 ,diagonal_length ])
168174
169175 for i in range (h ):
170176 for j in range (w ):
@@ -173,7 +179,7 @@ def main():
173179
174180
175181
176- write_image (accumulator ,'output/accumulator ' )
182+ write_image (accumulator ,'output/accumulator_line ' )
177183
178184
179185 hough_img = cv2 .imread ("original_imgs/hough.jpg" )
@@ -192,7 +198,7 @@ def main():
192198
193199
194200 hough_img_blue = hough_img .copy ()
195- co_p_t = np .unravel_index (np .argsort (accumulator .ravel ())[- 3500 :], accumulator .shape )
201+ co_p_t = np .unravel_index (np .argsort (accumulator .ravel ())[- 2800 :], accumulator .shape )
196202
197203 for z in range (len (co_p_t [0 ])):
198204 max_theta = co_p_t [0 ][z ]- 180
0 commit comments