Skip to content

Commit b9a10d6

Browse files
committed
add hough line method in Opencv code files
1 parent 37faba1 commit b9a10d6

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

Readme.md

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
* shape_detection_opencv.py
2929
* histogram_opencv.py
3030
* template_matching_opencv.py
31+
* hough_line_transform_opencv.py
32+
* probabilistic_hough_line_transform.py
3133

3234
## Helpful Documentations⭐:
3335
* OpenCV : https://docs.opencv.org/master/

hough_line_transform_opencv.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import cv2
2+
import numpy as np
3+
4+
img = cv2.imread('test_images/sudoku.png')
5+
grey_img = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY)
6+
7+
######## Standard Hough Transform #########
8+
9+
# 1 Edge Detection
10+
edges = cv2.Canny(grey_img , 50 , 150 , apertureSize=3)
11+
cv2.imshow('canny' , edges)
12+
13+
# 2 Convert lines to Hough lines ( Standard Method )
14+
lines = cv2.HoughLines(edges, 1 , np.pi / 180 , 200)
15+
16+
17+
for line in lines:
18+
rho , theta = line[0]
19+
a = np.cos(theta)
20+
b = np.sin(theta)
21+
x0 = a*rho
22+
y0 = b*rho
23+
24+
# x1 stores (r*cos(theta) - 1000*sin(theta))
25+
x1 = int(x0 + 1000*(-b))
26+
# y1 stores (r*sin(theta) + 1000*(cos(theta)))
27+
y1 = int(y0 + 1000*(a))
28+
# x2 stores (r*cos(theta) + 1000*sin(theta))
29+
x2 = int(x0 - 1000*(-b))
30+
# y2 stores (r*sin(theta) - 1000*(cos(theta)))
31+
y2 = int(y0 - 1000*(a))
32+
33+
cv2.line(img , (x1,y1) , (x2,y2) , (0,0,255) , 2)
34+
35+
cv2.imshow('image' , img)
36+
cv2.waitKey(0)
37+
cv2.destroyAllWindows()
38+
39+
## Problem : Lines are not perfactly detected.

output/Readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
# Canny Edge Detection
1414
<img src="canny_edge_detection.png">
1515

16-
# Approximation Of Geometircal Shapes
16+
# Approximation Of Geometrical Shapes
1717
<img src="approximation_of_shapes.png">

probabilistic_hough_line_transform.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import cv2
2+
import numpy as np
3+
4+
img = cv2.imread('test_images/sudoku.png')
5+
gray = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY)
6+
7+
# find edges
8+
canny = cv2.Canny(gray , 50 , 150 , apertureSize=3)
9+
cv2.imshow("Edges", canny)
10+
11+
# Hough lines Transform ( Probabilistic method )
12+
lines = cv2.HoughLinesP(canny , 1 , np.pi / 180 , 100 , minLineLength=100 , maxLineGap=10)
13+
14+
for line in lines:
15+
x1,y1,x2,y2 = line[0]
16+
cv2.line(img , (x1,y1) , (x2,y2) , (0,0,255) , 2)
17+
18+
cv2.imshow("Image",img)
19+
cv2.waitKey(0)
20+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)