opencv 霍夫曼变换 直线提取
生活随笔
收集整理的這篇文章主要介紹了
opencv 霍夫曼变换 直线提取
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import cv2
import numpy as np img = cv2.imread("hd.jpeg", 0)img = cv2.GaussianBlur(img,(3,3),0)
edges = cv2.Canny(img, 50, 150, apertureSize = 3)
lines = cv2.HoughLines(edges,1,np.pi/10,118) #這里對最后一個參數使用了經驗型的值
result = img.copy()for m in range(lines.ndim):for line in lines[m]:rho = line[0] #第一個元素是距離rhotheta= line[1] #第二個元素是角度theta# print rho# print thetaif (theta < (np.pi/4. )) or (theta > (3.*np.pi/4.0)): #垂直直線#該直線與第一行的交點pt1 = (int(rho/np.cos(theta)),0)#該直線與最后一行的焦點pt2 = (int((rho-result.shape[0]*np.sin(theta))/np.cos(theta)),result.shape[0])#繪制一條白線cv2.line( result, pt1, pt2, (255))else: #水平直線# 該直線與第一列的交點pt1 = (0,int(rho/np.sin(theta)))#該直線與最后一列的交點pt2 = (result.shape[1], int((rho-result.shape[1]*np.cos(theta))/np.sin(theta)))#繪制一條直線cv2.line(result, pt1, pt2, (255), 1)cv2.imshow('Canny', edges )
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
總結
以上是生活随笔為你收集整理的opencv 霍夫曼变换 直线提取的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: opencv sobe 边缘检测算子
- 下一篇: opencv 转换图像为灰度