-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAnamorphosePlanaire.py
executable file
·226 lines (184 loc) · 8.98 KB
/
AnamorphosePlanaire.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue May 25 11:51:13 2021
@author: yvan
"""
import numpy as np
import matplotlib.pyplot as plt
#from IPython import get_ipython
from cv2 import cv2
plt.close('all')
#get_ipython().magic('reset -sf')
##--------------------------------FONCTIONS----------------------------------##
def Pix2Meter(Pospix, image, Lim_inf_H, Lim_max_H, Lim_inf_V, Lim_max_V):
Posmet = np.zeros((len(Pospix),2),np.float32)
Posmet[:, 0] = (Lim_max_V-Lim_inf_V)*Pospix[:,0]/image.shape[1]+Lim_inf_V
Posmet[:, 1] = (Lim_max_H-Lim_inf_H)*Pospix[:,1]/image.shape[0]+Lim_inf_H
return Posmet
def Meter2Pix(Posmet, image, Lim_inf_H, Lim_max_H, Lim_inf_V, Lim_max_V):
Pospix = np.zeros((len(Posmet),2), np.float32)
Pospix[:, 0] = image.shape[0]*(Posmet[:,0]-Lim_inf_V)/(Lim_max_V-Lim_inf_V)
Pospix[:, 1] = image.shape[1]*(Posmet[:,1]-Lim_inf_H)/(Lim_max_H-Lim_inf_H)
return Pospix
def set_aspect_equal_3d(ax):
"""Fix equal aspect bug for 3D plots."""
xlim = ax.get_xlim3d()
ylim = ax.get_ylim3d()
zlim = ax.get_zlim3d()
from numpy import mean
xmean = mean(xlim)
ymean = mean(ylim)
zmean = mean(zlim)
plot_radius = max([abs(lim - mean_)
for lims, mean_ in ((xlim, xmean),
(ylim, ymean),
(zlim, zmean))
for lim in lims])
ax.set_xlim3d([xmean - plot_radius, xmean + plot_radius])
ax.set_ylim3d([ymean - plot_radius, ymean + plot_radius])
ax.set_zlim3d([zmean - plot_radius, zmean + plot_radius])
##------------------------------FIN FONCTIONS--------------------------------##
##-------------------------------CONSTANTES----------------------------------##
img = cv2.imread("/Users/yvan/Desktop/ETS_montreal/Cours/E21/MTR892/Banque_Speckle/4mm/speckle_1.png")
heightpi = img.shape[0]
widthpi = img.shape[1]
height=29.7e-2#hauteur en m de l'image de reference
width=21e-2#largeur en m de l'image de reference
#Angles
gamma = 80.0 #angle entre capteur et plan aile (deg)
theta = 90.0-gamma #Angle entre normale capteur et plan aile (deg)
alpha = 10.0#Angle de champ de vue
beta = 180.0-165.0-alpha/2.0 #Angle aigu entre aile et axe optique
#Calcul Point debut champ de vue
l = np.sqrt(0.9**2 + 2.5**2 + 0.9*2.5*np.cos(105*np.pi/180))
A = np.array([l*np.cos((alpha/2)*np.pi/180), 0, l*np.sin((-alpha/2)*np.pi/180)])
B = np.array([A[0] + (5.5-2.5)*np.cos(beta*np.pi/180), 0, A[2] + (5.5-2.5)*np.sin(beta*np.pi/180)])
C1 = np.array([[(B[0]+A[0])/2, (60e-2)/2, (B[2]+A[2])/2],
[(B[0]+A[0])/2, (-60e-2)/2, (B[2]+A[2])/2]])
CadreAile = np.vstack((A, B, C1))#Points qui definissent les limites spatiales de l'aile
#Plane aile - normal vector
a = -np.sin(theta*np.pi/180)
b = 0
c = np.cos(theta*np.pi/180)
dprim = a*A[0]+b*A[1]+c*A[2]
#Plane 1 - normal vector
xa = 1
ya = 0
za = 0
d = A[0]#7.52928#
#Taille de l'image de référence
new_height = 2*d*np.tan((alpha/2)*np.pi/180)
new_width = 2*d*np.tan((alpha/2)*np.pi/180)
#Creation des plans dans l'espace centré sur le centre optique
yg1, zg1 = np.meshgrid(np.arange(-new_width/2, new_width/2, new_width/20), np.arange(-new_height/2, new_height/2, new_height/20))
xg1 = (d-ya*yg1-za*zg1)/xa
xgp, ygp = np.meshgrid(np.arange(A[0], B[0], (B[0]-A[0])/20), np.arange(-(60e-2)/2, (60e-2)/2, (60e-2)/20))
zplane = (dprim-b*ygp-a*xgp)/c
#Cadre de l'image de réference
POI = np.array([[d, -new_width/2, -new_height/2],
[d, -new_width/2, new_height/2],
[d, new_width/2, new_height/2],
[d, new_width/2, -new_height/2]], np.float32)
##------------------------------FIN CONSTANTES-------------------------------##
##---------------------------------WARPING-----------------------------------##
originB = np.array([d, 0, 0], np.float32)#milieu du plan de l'image de reference
delta = (a*POI[:, 0]+b*POI[:, 1]+c*POI[:, 2])/dprim
Pntprojection = POI/delta[:, None]# Coordonnées des points projetés
delta3=(a*originB[0]+b*originB[1]+c*originB[2])/dprim
originR=originB/delta3 #Origine du plan rouge (projection de l'origine bleu sur plan incliné)
PntPrjtOnPng = Meter2Pix(Pntprojection[:, 1:3], img, -new_height/2, new_height/2, -new_width/2, new_width/2)#np.zeros((4, 2),np.float32)
POIOnPng = Meter2Pix(POI[:, 1:3], img, -new_height/2, new_height/2, -new_width/2, new_width/2)#np.zeros((4, 2),np.float32)
passage_horizontal_incline=np.array([[np.cos(gamma*np.pi/180), 0, -np.sin(gamma*np.pi/180)],
[0, 1, 0],
[np.sin(gamma*np.pi/180), 0, np.cos(gamma*np.pi/180)]], np.float32)
PntprojCoorplanR = np.zeros((4, 3),np.float32)
PntprojCoorplanR = Pntprojection[:, :] - originR
for i in range(0,4):
PntprojCoorplanR[i,:]=np.dot(passage_horizontal_incline,PntprojCoorplanR[i,:])
#PntprojCoorplanR.dtype='float32'
#Dimension réelle de l'image
hauteur = np.sqrt((Pntprojection[0,0]-Pntprojection[1,0])**2+(Pntprojection[0,1]
-Pntprojection[1,1])**2+(Pntprojection[0,2]-Pntprojection[1,2])**2)
largeur = np.sqrt((Pntprojection[1,0]-Pntprojection[2,0])**2+(Pntprojection[1,1]
-Pntprojection[2,1])**2+(Pntprojection[1,2]-Pntprojection[2,2])**2)
C=np.array([[widthpi/2, (B[0]-A[0])/hauteur * heightpi],
[(60e-2)/largeur * widthpi + widthpi/2, (B[0]-A[0])/hauteur * heightpi/2],
[widthpi/2, 0],
[-(60e-2)/largeur * widthpi + widthpi/2, (B[0]-A[0])/hauteur * heightpi/2]])
PntPrjtOnPng2 = Meter2Pix(PntprojCoorplanR[:, 1:3], img, -new_height/2, new_height/2, -new_width/2, new_width/2)#np.zeros((4, 2),np.float32)
POIOnPng2 = Meter2Pix(POI[:, 1:3], img, -new_height/2, new_height/2, -new_width/2, new_width/2)#np.zeros((4, 2),np.float32)
CadreAileOnPng = Meter2Pix(CadreAile[:, 1:3], img, -new_height/2, new_height/2, -new_width/2, new_width/2)# np.zeros((4, 2),np.float32)
# Matrice de passage reference-deformée
tform = cv2.getPerspectiveTransform(POIOnPng, PntPrjtOnPng)
tform2 = cv2.getPerspectiveTransform(POIOnPng2, PntPrjtOnPng2)
delta2 = (xa*B[0]+ya*B[1]+za*B[2])/d
D=B/delta2
ratio=(np.sqrt((D[0]-A[0])**2+(D[1]-A[1])**2+(D[2]-A[2])**2))/(np.sqrt((B[0]-A[0])**2+(B[1]-A[1])**2+(B[2]-A[2])**2))
ratio2=hauteur/np.sqrt((POI[2,0]-POI[2,1])**2)
#Déformation image de reference
new_heightpi = heightpi*hauteur/height
new_widthpi = widthpi*largeur/width
tf_img_warp = cv2.warpPerspective(img, tform, (int(widthpi), int(heightpi)))
tf_img_warp2 = cv2.warpPerspective(img, tform2, (int(new_widthpi), int(new_heightpi)))
#cv2.imwrite('/Users/yvan/Desktop/ETS_montreal/Cours/E21/MTR892/Deformeecv2100pi_cmBoutdaile2.png', tf_img_warp)#Enregistrement de l'image déformée
imgresize=tf_img_warp[int(C[2,1]):int(C[0,1]), int(C[3,0]):int(C[1,0])]
imgresize2=tf_img_warp2[int(C[2,1]):int(C[0,1]), int(C[3,0]):int(C[1,0])]
#cv2.imwrite('/Users/yvan/Desktop/ETS_montreal/Cours/E21/MTR892/Deformeecv2100pi_cmBoutdAileResize2.png', imgresize)#Enregistrement de l'image déformée
##-----------------------------FIN WARPING-----------------------------------##
##------------------------------AFFICHAGE-----------------------------------##
fig = plt.figure(1)
ax = fig.add_subplot(111, projection='3d')
ax.scatter(0, 0, 0, color='b')
#ax.scatter(originB[0], originB[1], originB[2], color='b')
#ax.scatter(originR[0], originR[1], originR[2], color='b')
ax.scatter(CadreAile[:,0], CadreAile[:,1], CadreAile[:,2], color='r')
#ax.scatter(D[0], D[1], D[2], color='g')
ax.plot(POI[:, 0], POI[:, 1], POI[:, 2], color='b', marker='o')
ax.plot(Pntprojection[:, 0], Pntprojection[:, 1], Pntprojection[:, 2], color='k', marker='o')
ax.plot(PntprojCoorplanR[:, 0], PntprojCoorplanR[:, 1], PntprojCoorplanR[:, 2], color='k', marker='o')
ax.plot_surface(xg1, yg1, zg1, rstride=10, cstride=10, color='b', alpha=0.2)
ax.plot_surface(xgp, ygp, zplane, rstride=10, cstride=10, color='r', alpha=0.2)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.axes.set_xlim3d(left=0, right=7)
ax.axes.set_ylim3d(bottom=-1, top=1)
ax.axes.set_zlim3d(bottom=-0.6, top=4)
#set_aspect_equal_3d(ax)
plt.show()
plt.figure(2)
plt.imshow(img, origin='lower')
#plt.plot(POIOnPng[:, 1], POIOnPng[:, 0], marker='+', color='red')
plt.xlabel('Columns')
plt.ylabel('Rows')
plt.title('Réference')
plt.show()
# plt.figure(3)
# plt.imshow(tf_img_warp, origin='lower')
# plt.plot(C[:,0],C[:,1], marker='+', color='red')
# plt.xlabel('Columns')
# plt.ylabel('Rows')
# plt.title('Déformée')
# plt.show()
# plt.figure(4)
# plt.imshow(imgresize, origin='lower')
# plt.scatter(C[:,0],C[:,1], marker='+', color='red')
# plt.xlabel('Columns')
# plt.ylabel('Rows')
# plt.title('imgresize')
# plt.show()
plt.figure(5)
plt.imshow(tf_img_warp2, origin='lower')
plt.scatter(C[:,0],C[:,1], marker='+', color='red')
plt.xlabel('Columns')
plt.ylabel('Rows')
plt.title('Avec rotation')
plt.show()
plt.figure(6)
plt.imshow(imgresize2, origin='lower')
plt.scatter(C[:,0],C[:,1], marker='+', color='red')
plt.xlabel('Columns')
plt.ylabel('Rows')
plt.title('Avec coordonnées réelles projetées resize')
plt.show()