import numpy as np
import matplotlib.pyplot as plt
# question 1
def plotmesh(X,Y):
# fonction pour afficher une grille de points
# X,Y : matrices de même taille donnant les coordonnées des points de la grille
plt.plot(X,Y,'g');
plt.plot(X.T,Y.T,'g')
# correction
M, N = 50, 25
X, Y = np.meshgrid(np.arange(M),np.arange(N),indexing='ij')
plotmesh(X,Y)
plt.axis('equal')
plt.show()
# question 2
def rotation(X,Y,theta,cx,cy):
# Rotation d'un ensemble de points 2D.
# X,Y : NumPy arrays de même taille donnant les coordonnées des points d'entrée
# theta : angle de la rotation en radians
# cx,cy : coordonnées du centre de la rotation
# Xout,Yout : tableaux de même taille que X et Y donnant les coordonnées des points après rotation.
sz = X.shape
R = np.array([[np.cos(theta),-np.sin(theta)],[np.sin(theta),np.cos(theta)]])
XYout = R @ np.array([X.flatten()-cx,Y.flatten()-cy])
XYout[0,:] += cx
XYout[1,:] += cy
Xout = np.reshape(XYout[0,:],sz)
Yout = np.reshape(XYout[1,:],sz)
return Xout, Yout
# to do : rotation de la grille et affichage
# ...
# question 3
# to do : projection de la grille tournée sur la grille initiale et affichage
# ...
# question 4
# correction
I1 = plt.imread('images/Barbara.bmp')
plt.figure(figsize=(10,10))
plt.imshow(I1,cmap='gray')
plt.show()
# question 5
def imrot_v1(I,theta):
# to do ...
RI1_v1 = imrot_v1(I1,theta)
plt.figure(figsize=(10,10))
plt.imshow(RI1_v1,cmap='gray')
plt.show()
# question 6
def imrot_v2(I,theta):
# to do ...
RI1_v2 = imrot_v2(I1,theta)
plt.figure(figsize=(10,10))
plt.imshow(RI1_v2,cmap='gray')
plt.show()
# question 7
import scipy.ndimage
def imrot(I,theta):
# to do ...
RI1 = imrot(I1,theta)
plt.figure(figsize=(10,10))
plt.imshow(RI1,cmap='gray')
plt.show()
# question 8
# to do : rotations successives pour faire un tour complet
# ...
# question 9
# to do : zoom et transformation affine
# ...
# question 12
import scipy.io
def imdef_field(I,Uinv,Vinv):
# to do ...
tmp = scipy.io.loadmat('field.mat')
Uinv, Vinv = tmp['Uinv'], tmp['Vinv']
I1d = imdef_field(I1,Uinv,Vinv)
plt.figure(figsize=(10,10))
plt.imshow(I1d,cmap='gray')
plt.show()
# question 13
def invchamp(Uinv,Vinv):
# to do...
U, V = invchamp(Uinv,Vinv)
I1_ = imdef_field(I1d,U,V)
plt.figure(figsize=(10,10))
plt.imshow(I1_,cmap='gray')
plt.show()