使用步骤
1.引入库
代码如下:
import cv2
import numpy as np
2.1读入数据
代码如下:
HoughLines
import cv2
import numpy as np
img = cv2.imread("shape.png")#读取原图
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edge = cv2.Canny(gray,50,150)
lines = cv2.HoughLines(edge,1,np.pi/180,50)
#画线
for line in lines:
rho,theta = line[0]
a = np.cos(theta)
b = np.sin(theta)
x0 = rho * a
y0 = rho * b
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),(0,255,0),2)
cv2.imshow("img",img)
cv2.imshow("gray",gray)
cv2.imshow("