T.Hough rectas
Publicado por Geomata (21 intervenciones) el 12/01/2021 10:10:42
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import cv2
import numpy as np
import matplotlib.pyplot as plt
import skimage
from skimage import exposure, data, img_as_float, io
from skimage.transform import hough_line,hough_line_peaks
from skimage.feature import canny
from skimage import data
from skimage.draw import line
from matplotlib import cm
from skimage.transform import probabilistic_hough_line
imagen = cv2.imread ('Puerta_Alcala.jpg')
img = np.copy(imagen)
img2 = np.copy(imagen)
img3 = np.copy(imagen)
img4 = np.copy(imagen)
img5 = np.copy (imagen)
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gauss = cv2.GaussianBlur(img_gray,(11,11),0)
bordes =cv2.Canny(gauss,50,220,apertureSize = 3)
plt.imshow (bordes)
lines = cv2.HoughLines(bordes,1,np.pi/180,200)
print(lines[0])
img2 = img.copy()
for row in lines:
for rho,theta in row:
a = np.cos(theta)
b = np.sin (theta)
x0 =a*rho
y0=b*rho
x1 = int(x0 + 5000*(-b))
y1 = int(y0 + 5000*(a))
x2 = int(x0 - 5000*(-b))
y2 = int(y0 - 5000*(a))
cv2.line(img2,(x1,y1),(x2,y2),(0,0,255),2)
plt.figure(figsize=(12,12))
#cv2.imwrite('houghlines.jpg',img)
#houghl=io.imread('houghlines.jpg')
plt.imshow(img2[:,:,::-1]);
plt.title('Rectas');
#Con skimage
tested_angles = np.linspace(-np.pi / 2, np.pi / 2, 360)
h, theta2, d = hough_line(bordes, theta=tested_angles)
# Busqueda de líenas mas votadas
hspace, angles, dists = hough_line_peaks(h, theta2, d, threshold=0, num_peaks=20)
# REpresento las líneas
lines_ski = np.column_stack((dists,angles))
# Añadimos una dimension para que coicida esta matriz con la que empleamos al respresentar líeas en OpenCV
lines_ski = np.expand_dims(lines_ski, axis=1)
for row in lines_ski:
for d,theta3 in row:
a = np.cos(theta3)
b = np.sin (theta3)
x0 =a*d
y0=b*d
x1 = int(x0 + 5000*(-b))
y1 = int(y0 + 5000*(a))
x2 = int(x0 - 5000*(-b))
y2 = int(y0 - 5000*(a))
cv2.line(img3,(x1,y1),(x2,y2),(0,0,255),2)
plt.figure(figsize=(12,12))
plt.imshow(img3[:,:,::-1]);
#Probabilística OpenCv
min_acumulator_votes=20
minLineLength =20
maxLineGap = 5
linesP = cv2.HoughLinesP(bordes, 1, np.pi / 180, min_acumulator_votes, minLineLength, maxLineGap)
B,G,R = cv2.split(img4)
img4= cv2.merge((R,G,B))
if linesP is not None:
for i in range(0, len(linesP)):
l = linesP[i][0]
cv2.line(img4, (l[0], l[1]), (l[2], l[3]), (255,0,0), 1)
plt.figure(figsize=(12,12))
plt.imshow(img4);
#Probabilística Skimage
lines = probabilistic_hough_line(bordes, threshold=10, line_length=5,line_gap=3)
if lines is not None:
for i in range(0, len(lines)):
st = lines[i][0]
en = lines[i][1]
cv2.line(img5, (st[0], st[1]), (en[0], en[1]), (255,0,0), 1)
plt.figure(figsize=(12,12))
plt.imshow(img5[:,:,::-1]);
Valora esta pregunta


0