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
| import numpy as np
import cv2
def gauss(A, y):
n = len(A)
for i in range(n):
max_row = i + max(range(n - i), key=lambda r: abs(A[i + r][i]))
A[i], A[max_row] = A[max_row], A[i]
y[i], y[max_row] = y[max_row], y[i]
for j in range(i + 1, n):
if A[j][i] != 0:
factor = A[j][i] / A[i][i]
for k in range(i, n):
A[j][k] -= factor * A[i][k]
y[j] -= factor * y[i]
return A, y
def solve_upper_triangular(A, y):
n = len(A)
x = [0] * n
for i in range(n - 1, -1, -1):
if A[i][i] == 0:
raise ValueError("La matrice A est singulière, impossible de résoudre.")
sum_ax = sum(A[i][j] * x[j] for j in range(i + 1, n))
x[i] = (y[i] - sum_ax) / A[i][i]
return x
def get_affine_transform(start, end):
if len(start) != 3 or len(end) != 3:
raise ValueError("Les listes de points doivent contenir exactement 3 points.")
A = []
B = []
for i in range(3):
x, y = start[i]
x_prime, y_prime = end[i]
A.append([x, y, 1, 0, 0, 0])
A.append([0, 0, 0, x, y, 1])
B.append(x_prime)
B.append(y_prime)
A = np.array(A)
B = np.array(B)
a = np.linalg.solve(A, B)
affine_matrix = np.array([[a[0], a[1], a[2]],
[a[3], a[4], a[5]]])
return affine_matrix
def morph_images(img1, img2, alpha, points1, points2):
if img1.shape != img2.shape:
raise ValueError("Les images doivent avoir la même taille.")
affine_transform = get_affine_transform(points1, points2)
morphed_image = np.zeros_like(img1)
for y in range(img1.shape[0]):
for x in range(img1.shape[1]):
src_point = np.array([x, y, 1])
transformed_point = affine_transform @ src_point
x_transformed = int(transformed_point[0])
y_transformed = int(transformed_point[1])
if 0 <= x_transformed < img1.shape[1] and 0 <= y_transformed < img1.shape[0]:
color1 = img1[y_transformed, x_transformed]
color2 = img2[y, x]
morphed_color = (1 - alpha) * color1 + alpha * color2
morphed_image[y, x] = morphed_color
return morphed_image.astype(np.uint8)
def main():
start_image = cv2.imread('koala.ppm')
end_image = cv2.imread('tigre.ppm')
# Points correspondants pour le morphing
points_start = [(174, 167), (202, 196), (188, 176)] # Exemple de points sur l'image de départ
points_end = [(188, 183), (201, 193), (202, 196)] # Exemple de points sur l'image d'arrivée
for i in range(0, 101, 4):
alpha = i / 100.0
morphed_image = morph_images(start_image, end_image, alpha, points_start, points_end)
cv2.imwrite(f'morphing_{i}.ppm', morphed_image)
if __name__ == "__main__":
main() |
Partager