2016-05-27 107 views
1

我遇到一些麻煩cv2.Houghlines()顯示垂直線時,我相信真正適合應提供水平線。 這裏是我使用的代碼的片段:霍夫空間CV2 houghlines

 rho_resoultion = 1 
     theta_resolution = np.pi/180 
     threshold = 200 

     lines = cv2.HoughLines(image, rho_resoultion, theta_resolution, threshold) 

     # print(lines) 
     for line in lines: 
      rho, theta = line[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,255,255),1) 

     cv2.namedWindow('thing', cv2.WINDOW_NORMAL) 
     cv2.imshow("thing", image) 
     cv2.waitKey(0) 

這是輸入和輸出:我認爲這將是更容易提取出正在發生的事情,如果

可以查看霍夫空間圖像。 但是,文檔不提供如何顯示完整空間的信息。 如何顯示整個Hough變換空間? 我試圖將閾值降低到1,但沒有提供圖像。

回答

0

也許你calculationg角度時,得到的東西是錯誤的。隨意展示一些代碼。

這裏是如何顯示圖像中的所有霍夫行的例子:

import cv2 
import numpy as np 

img = cv2.imread('sudoku.jpg') 

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
edges = cv2.Canny(gray,50,150,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('Houghlines',img) 
if cv2.waitKey(0) & 0xff == 27: 
    cv2.destroyAllWindows() 

原圖:

enter image description here

結果:

enter image description here

+0

這實際上非常類似於我的代碼sed,但結果非常糟糕。你可以看到我在問題中添加的結果。 – chase