1 # Writer : wojianxinygcl@163.com
2
3 # Data : 2020.3.21
4
5 import cv2 as cv
6
7 import numpy as np
8
9 img = cv.imread('../paojie.jpg')
10
11 gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
12
13 # 50,150 为二值化时的阈值 apertureSize为Sobel滤波器的大小
14
15 edges = cv.Canny(gray,50,150,apertureSize = 3)
16
17 cv.imshow('Canny Result',edges)
18
19 cv.imwrite('Canny_Result.jpg',edges)
20
21 # 高效的霍夫线检测算法
22
23 # edges : 二值图像
24
25 # 1 : ρ
26
27 # pi/180: θ
28
29 # 100 : Accumulator threshold parameter. Only those lines are returned that get enough votes ( >threshold ).
30
31 # minLineLength : Minimum length of line. Line segments shorter than this are rejected.
32
33 # maxLineGap : Maximum allowed gap between line segments to treat them as a single line.
34
35 lines = cv.HoughLinesP(edges,1,np.pi/180,100,minLineLength=100,maxLineGap=10)
36
37 for line in lines:
38
39 x1,y1,x2,y2 = line[0]
40
41 cv.line(img,(x1,y1),(x2,y2),(0,255,0),2)
42
43 cv.imshow('HoughLines Result',img)
44
45 cv.imwrite('HoughLines_Result.jpg',img)
46
47 cv.waitKey(0)
48
49 cv.destroyAllWindows()
原图 ↑
Canny_Result.jpg ↑
HoughLines_Result.jpg ↑