1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 | import cv2
import numpy as np
import math
dir = r'D:\\img\\'
imgname = 'shape.jpg'
path = dir + imgname
imgOriginal = cv2.imread(path, cv2.IMREAD_COLOR)
gray = cv2.cvtColor(imgOriginal,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5),0)
edges = cv2.Canny(blur, 50, 200,apertureSize=3)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,9))
edges = cv2.dilate(edges, kernel)
rho = 1
theta = np.pi
threshold = 200;
minLineLength = 80
maxLineGap = 10
lines = cv2.HoughLinesP(image=edges,rho=rho,theta=theta,threshold=threshold,minLineLength=minLineLength,maxLineGap=maxLineGap)
if(lines is not None):
for line in lines:
x1,y1,x2,y2 = line[0]
cv2.line(imgOriginal,(x1,y1),(x2,y2),(0,0,255),2)
print(theta)
cv2.imshow("Hough Lines", imgOriginal)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
print('No Lines found')
|