-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHoughWithPython.py
More file actions
60 lines (49 loc) · 1.43 KB
/
HoughWithPython.py
File metadata and controls
60 lines (49 loc) · 1.43 KB
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
# -*- coding:gbk -*-
import cv2
import numpy as np
import matplotlib.pyplot as plt
def draw_lines(img, houghLines, color=[255, 0, 0], thickness=2):
for line in houghLines:
for rho,theta in line:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
cv2.line(img,(x1,y1),(x2,y2),color,thickness)
def weighted_img(img, initial_img, alpha=0.8, beta=1., ¦Ë=0.):
return cv2.addWeighted(initial_img, alpha, img, beta, ¦Ë)
## read image
img = cv2.imread("D:\python\images.png")
## white and black
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
## glass
img_blur = cv2.GaussianBlur(img_gray,(5,5),0)
## edge
img_edges = cv2.Canny(img_blur,50,120)
## hough
rho = 1
theta = np.pi/180
threshold = 100
hough_lines = cv2.HoughLines(img_edges,rho , theta , threshold)
## return
img_lines = np.zeros_like(img)
draw_lines(img_lines,hough_lines)
img_lines = weighted_img(img_lines,img)
plt.figure(figsize=(15,5))
plt.subplot(1,3,1)
plt.imshow(img,cmap="gray")
plt.title("source",fontsize=12)
plt.axis("off")
plt.subplot(1,3,2)
plt.imshow(img_edges,cmap="gray")
plt.title("edge",fontsize=12)
plt.axis("off")
plt.subplot(1,3,3)
plt.imshow(img_lines)
plt.title("image with hough lines",fontsize=12)
plt.axis("off")
plt.show()