標籤:

每天一練P9-Python和OpenCV做圖像處理(HoughLines)

每天一練P9-Python和OpenCV做圖像處理 (HoughLines)

HoughLines用來提取直線特徵

函數原型為 HoughLines(img, rho, theta, threshold )

  • img 為輸入的圖像,Canny提取後的邊界數據
  • rho 為距離解析度
  • theta 為角度範圍
  • threshold 為累加器閾值

示常式序為

# coding by 劉雲飛# email: liuyunfei.1314@163.com # date: 2018-4-23import cv2import numpy as np# 讀取名稱為 p9.png的圖片org = cv2.imread("p9.png",1)img = cv2.imread("p9.png",1)gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)# 提取邊緣edges = cv2.Canny(gray,30,250,apertureSize=3)# 提取直線lines = cv2.HoughLines(edges,1,np.pi/180,200)for line in lines: 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),(0,0,255),2)# 顯示原圖和處理後的圖像cv2.imshow("org",org)cv2.imshow("processed",img) cv2.waitKey(0)

效果圖像

推薦閱讀:

《Django By Example》第二章 中文翻譯
如何去尋找網路爬蟲的需求?
黃哥Python每日新聞(2017-8-19)
4 個用於構建優秀的命令行用戶界面的 Python 庫
如何在用 Python 編程時添加中文注釋?

TAG:OpenCV | Python |