Python代码库OpenCV之09线检测line detection(含代码)

Python代码库OpenCV之09线检测line detection(含代码)

代码


import cv2
import numpy as np
filename="D:\\pythondev\\dev\\opencv\\img\\tu.png"
image = cv2.imread(filename)

gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,100,170,apertureSize =3 )

lines = cv2.HoughLines(edges,1,np.pi/180,240)

for rho,theta in lines[0]:
    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(image,(x1,y1),(x2,y2),(255,0,0),2)

cv2.imshow("Hough Lines",image)
cv2.waitKey(0)
cv2.destroyAllWindows()

效果

image.png

更多精彩代码请关注我的专栏

  • selenium & python 源码大全
  • reportlab教程和源码大全
  • python源码大全

你可能感兴趣的:(Python代码库OpenCV之09线检测line detection(含代码))