2013-04-05 11:02:07 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
'''
|
|
|
|
This example illustrates how to use Hough Transform to find lines
|
|
|
|
Usage: ./houghlines.py [<image_name>]
|
2014-09-13 16:28:41 +02:00
|
|
|
image argument defaults to ../data/pic1.png
|
2013-04-05 11:02:07 +02:00
|
|
|
'''
|
2015-09-13 18:00:22 +02:00
|
|
|
# Python 2/3 compatibility
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2013-04-05 11:02:07 +02:00
|
|
|
import cv2
|
|
|
|
import numpy as np
|
|
|
|
import sys
|
|
|
|
import math
|
|
|
|
|
2015-08-04 23:01:51 +02:00
|
|
|
if __name__ == '__main__':
|
2013-04-05 11:02:07 +02:00
|
|
|
|
2015-08-04 23:01:51 +02:00
|
|
|
try:
|
|
|
|
fn = sys.argv[1]
|
|
|
|
except:
|
|
|
|
fn = "../data/pic1.png"
|
2015-09-13 18:00:22 +02:00
|
|
|
print(__doc__)
|
2015-08-04 23:01:51 +02:00
|
|
|
src = cv2.imread(fn)
|
|
|
|
dst = cv2.Canny(src, 50, 200)
|
|
|
|
cdst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)
|
2015-02-20 10:18:08 +01:00
|
|
|
|
2015-08-04 23:01:51 +02:00
|
|
|
if True: # HoughLinesP
|
|
|
|
lines = cv2.HoughLinesP(dst, 1, math.pi/180.0, 40, np.array([]), 50, 10)
|
|
|
|
a,b,c = lines.shape
|
|
|
|
for i in range(a):
|
|
|
|
cv2.line(cdst, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA)
|
2013-04-05 11:02:07 +02:00
|
|
|
|
2015-08-04 23:01:51 +02:00
|
|
|
else: # HoughLines
|
|
|
|
lines = cv2.HoughLines(dst, 1, math.pi/180.0, 50, np.array([]), 0, 0)
|
|
|
|
a,b,c = lines.shape
|
|
|
|
for i in range(a):
|
|
|
|
rho = lines[i][0][0]
|
|
|
|
theta = lines[i][0][1]
|
|
|
|
a = math.cos(theta)
|
|
|
|
b = math.sin(theta)
|
|
|
|
x0, y0 = a*rho, b*rho
|
|
|
|
pt1 = ( int(x0+1000*(-b)), int(y0+1000*(a)) )
|
|
|
|
pt2 = ( int(x0-1000*(-b)), int(y0-1000*(a)) )
|
|
|
|
cv2.line(cdst, pt1, pt2, (0, 0, 255), 3, cv2.LINE_AA)
|
2013-04-05 11:02:07 +02:00
|
|
|
|
2015-08-04 23:01:51 +02:00
|
|
|
|
|
|
|
cv2.imshow("source", src)
|
|
|
|
cv2.imshow("detected lines", cdst)
|
|
|
|
cv2.waitKey(0)
|