Loovelj
9/7/2018 - 6:20 AM

霍夫line检测表格示例

霍夫line检测表格示例

# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name:     Hough_line1.py
   Description :
   Author :       liujun
   date:          2018/7/11
-------------------------------------------------
   Change Activity:
                   2018/7/11:
-------------------------------------------------
"""
import cv2
import numpy as np

img = cv2.imread('000033.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
height,wight=img.shape[:2]


lines_hor = cv2.HoughLines(edges,1,np.pi/180,wight*7//10)
for lineone in lines:
    for rho,theta in lineone:
        a = np.cos(theta)
        b = np.sin(theta)
        if b==1:
            x0 = a*rho
            y0 = b*rho
            x1 = 0
            y1 = int(y0 + 1000*(a))
            x2 = wight-1
            y2 = int(y0 - 1000*(a))
        
            cv2.line(img,(x1,y1),(x2,y2),(255,0,0),2)
            
lines_ver = cv2.HoughLines(edges,1,np.pi/180,height*7//10)
for lineone in lines:
    for rho,theta in lineone:
        a = np.cos(theta)
        b = np.sin(theta)
        if b==0:
            x0 = a*rho
            y0 = b*rho
            x1 = x0
            y1 =0
            x2 = x0
            y2 =height-1
        
            cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2) 
            
 
            
            
            


cv2.imwrite("houghline_demo.jpg",img)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()