-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMeasureLidar.py
229 lines (152 loc) · 5.79 KB
/
MeasureLidar.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
import json
def resizeImage(image, divider):
dim = (int(image.shape[1] / divider), int(image.shape[0] / divider))
image_resized = cv.resize(image, dim, interpolation=cv.INTER_LINEAR)
return image_resized
def mostra(legenda, imagem):
cv.imshow(legenda, imagem)
cv.waitKey(0)
return imageResized
def showTwoImagesO(image1, image2):
fig = plt.figure(figsize=(10, 7))
rows = 1
columns = 2
fig.add_subplot(rows, columns, 1)
plt.imshow(image1)
# plt.axis('off')
plt.title("Original reduzida")
fig.add_subplot(rows, columns, 2)
plt.imshow(image2)
# plt.axis('off')
plt.title("Representação Lidar")
plt.show()
cv.waitKey(0)
def showTwoImages(nome_imagem, image1, image2, angulo, ponto1, ponto2, distancia):
fig = plt.figure(figsize=(10, 7))
plt.suptitle(nome_imagem)
rows = 1
columns = 2
fig.add_subplot(rows, columns, 1)
plt.imshow(image1)
plt.title("Original reduzida")
fig.add_subplot(rows, columns, 2)
plt.imshow(image2)
plt.title("Representação Lidar")
plt.figtext(0.1, 0.09, ponto1, ha='left', fontsize=10)
plt.figtext(0.1, 0.07, ponto2, ha='left', fontsize=10)
plt.figtext(0.1, 0.04, angulo, ha='left', fontsize=10)
plt.figtext(0.1, 0.01, distancia, ha='left', fontsize=10)
plt.show()
cv.waitKey(0)
def organizaTXT(arquivo):
arq = open(arquivo)
lines = arq.readlines()
pontos = []
print("Number of lines = " + str(len(lines)))
for i in range(len(lines)):
values = lines[i].split(",")
print("Line " + str(i) + " there are = " + str(len(values)))
for j in range(len(values)):
print(str(j) + " : " + values[j])
pontos.append(values)
# print(pontos)
return pontos
def organizaJSON1(arquivo):
pontos = []
with open(arquivo, "r") as arq:
lines = arq.read()
print("Number of lines = " + str(len(lines)))
for i in range(len(lines)):
values = lines[i].split(",")
print("Line " + str(i) + "there are = " + str(len(values)))
for j in range(len(values)):
print(str(j) + " : " + values[j])
pontos.append(values)
return pontos
def organizaJSON(arquivo):
with open(arquivo, 'r') as file:
dados = json.load(file)
lidar_data = dados["LIDARData"]
'''
for i, linha in enumerate(lidar_data):
print("Linha", i, "possui", len(linha), "valores")
for j, valor in enumerate(linha):
print("Linha", i, "Coluna", j, ":", valor)
'''
return lidar_data
def calibrationLIDAR(pontos, imageOriginal, imageLidar):
## Eixo vertical
ponto1 = [0, 127]
ponto2 = [191, 127]
## Eixo horizontal
# ponto1 = [95, 0]
# ponto2 = [95, 255]
sizeHeight = 0.168
sizeWidth = 0.248
x = float(pontos[ponto1[0]][ponto1[1]])
print(str(x) + " m")
y = float(pontos[ponto2[0]][ponto2[1]])
print(str(y) + " m")
angle = np.arccos((x**2+y**2-sizeHeight**2)/(2*x*y))
print(angle)
print(np.rad2deg(angle))
## valor achado vertical 57.87424017358846
## valor achado horizontal 81.27321528320013
# a = 58.24929668917284
# b = 59.01806172559483
# media = (a + b) / 2
# angle = np.arccos((x ** 2 + y ** 2 - sizeWidth ** 2) / (2 * x * y))
# print(angle)
# print(np.rad2deg(angle))
# a = 81.40737774694823
# b = 82.31491307843667
# media = (a + b) / 2
# print(media)
cv.circle(imageOriginal, (ponto1[1], ponto1[0]), 0, (0, 0, 255), -1)
cv.circle(imageOriginal, (ponto2[1], ponto2[0]), 0, (255, 0, 0), -1)
cv.circle(imageLidar, (ponto1[1], ponto1[0]), 0, (0, 0, 255), -1)
cv.circle(imageLidar, (ponto2[1], ponto2[0]), 0, (255, 0, 0), -1)
showTwoImages(imageOriginal, imageLidar)
def measureDistance(nome_imagem, ponto1, ponto2, pontos, image, imageLidar):
xGrau = 81.27321528320013 * ponto1[1] / 256
yGrau = 57.87424017358846 * ponto1[0] / 192
ponto1Grau = [yGrau, xGrau]
xGrau = 81.86114541269245 * ponto2[1] / 256
yGrau = 58.633679207383835 * ponto2[0] / 192
ponto2Grau = [yGrau, xGrau]
angle = np.sqrt((ponto2Grau[0] - ponto1Grau[0]) ** 2 + (ponto2Grau[1] - ponto1Grau[1]) ** 2)
print("O angulo encontrado é: " + str(angle) + " graus")
x = float(pontos[ponto1[0]][ponto1[1]])
print("Ponto 1: " + str(x) + " m")
y = float(pontos[ponto2[0]][ponto2[1]])
print("Ponto 2: " + str(y) + " m")
size = np.sqrt(x ** 2 + y ** 2 - (2 * x * y * np.cos(np.deg2rad(angle))))
print("O valor calculado é: " + str(size * 100) + " cm")
cv.circle(image, (ponto1[1], ponto1[0]), 0, (0, 0, 255), -1)
cv.circle(image, (ponto2[1], ponto2[0]), 0, (255, 0, 0), -1)
cv.circle(imageLidar, (ponto1[1], ponto1[0]), 0, (0, 0, 255), -1)
cv.circle(imageLidar, (ponto2[1], ponto2[0]), 0, (255, 0, 0), -1)
showTwoImages(nome_imagem, image, imageLidar, "angulo: " + str(angle) + " graus", "ponto 1: " + str(x) + " m", "ponto2: " + str(y) + " m", "distancia: " + str(size*100) + " cm")
def main():
import matplotlib
#matplotlib.use('MacOSX')
#baseURL = "/Users/alanpaulino/projects/NewCapture/Demo/"
baseURL = "C:/Users/aalbu/Dropbox/PC/Documents/Projeto Embrapii/Lidar/file1/"
numberImage = "7"
#pontos = organizaTXT(baseURL + numberImage + "_depth.txt")
pontos = organizaJSON(baseURL + numberImage + ".json")
imageOriginal = cv.imread(baseURL + numberImage + ".jpg")
image = resizeImage(imageOriginal, 7.5)
imageLidar = cv.imread(baseURL + numberImage + "_imageDepth.jpg")
ponto1 = [107, 79]
ponto2 = [107, 112]
nome_imagem = "Imagem: " + numberImage
# ponto1 = [75, 129]
# ponto2 = [82, 129]
# calibrationLIDAR(pontos, image, imageLidar)
measureDistance(nome_imagem, ponto1, ponto2, pontos, image, imageLidar)
if __name__ == "__main__":
main()