Last active
March 2, 2022 12:24
-
-
Save lktslionel/75437638e605456446e72d6c907bd09d to your computer and use it in GitHub Desktop.
Carole Morphing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# ALGORITHM | |
# | |
# | |
# Function to handle image | |
# | |
# 1. load_image("/checmi/vers/fichier") -> np.darray (image) | |
# 2. move_image(image: np.darray, (dy: int, dx: int)) -> np.darray (image) | |
# 3. mediam_image_between(image_source: np.darray, image_target: np.darray) -> np.darray (image) | |
# | |
# Morphing | |
# | |
# 1. nb_intermediary_images = 4 (int) | |
# 2. compute_median_images_between(image_source: np.darray, image_target: np.darray, nb_intermediary_images: int) -> np.darray[] (image array) | |
# 3. compute_images(image_source: np.darray, image_target: np.darray, nb_intermediary_images: int) -> np.darray[] (image array with translation applied) | |
# 3.1 compute image median between source and target: mediam_image_between(...) | |
# 3.2 compute median images between source and median: compute_median_images_between(...) | |
# 3.3 compute median images between median and target: compute_median_images_between(...) | |
# 3.4 merge images as [image_source] + 3.2 + [3.1] + 3.3 + [image_target] | |
# 3.5 iterate on all images obtained in 3.4 and apply translation: [ move_image(image, (dy, dx)) for image in frames ] ( dx, dy must be defined) | |
# 3.6 return 3.5 | |
# 4. save_as_gif("/path/to/file", compute_images()) | |
# 4.1 Use: imageio.mimsave(...) | |
# 5. solution("/path/image/source/png", "/path/image/target/png", nb_intermediary_images: int) -> /path/image/morphing/gif | |
# | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""TP1_ATDN2.ipynb | |
Automatically generated by Colaboratory. | |
Original file is located at | |
https://colab.research.google.com/drive/1DJC_EmYk1Vrdxl3XNgWtAc0ZEhXoIsSV | |
""" | |
import typing | |
class Image: | |
def __init__(self, filepath: str) -> None: | |
self.path = filepath | |
pass | |
def __repr__(self) -> str: | |
return self.path | |
def move_to(self, x: int, y: int): | |
self.path = (' > '* y) + self.path | |
@classmethod | |
def mediam_image_between(cls, source: 'Image', target: 'Image') -> 'Image': | |
return Image(source.path + target.path) | |
class ImageMorphing: | |
def __init__(self, src_path: str, dst_path: str, inner_frame_count = 4) -> None: | |
self.source_image = Image(src_path) | |
self.target_image = Image(dst_path) | |
self.inner_frame_count = inner_frame_count | |
def compute_median_frames_between(self, source: Image, target: Image, frames=[], reverse=False) -> list[Image]: | |
if len(frames) == self.inner_frame_count: | |
return frames | |
median_image = Image.mediam_image_between(source, target) | |
remaining_frames = frames + [median_image] if reverse else [median_image] + frames | |
return self.compute_median_frames_between(source, median_image, remaining_frames, reverse=reverse) | |
def compute_frames(self) -> list[Image]: | |
frames = [] | |
median_image = Image.mediam_image_between(self.source_image, self.target_image) | |
frames = ( | |
[self.source_image] | |
+ self.compute_median_frames_between(self.source_image, median_image) | |
+ [median_image] | |
+ self.compute_median_frames_between(self.target_image, median_image, reverse=True) | |
+ [self.target_image] | |
) | |
for index, image in enumerate(frames): | |
image.move_to(0, index) | |
return frames | |
def save_as_gif(filename: str): pass | |
m = ImageMorphing('A', 'B') | |
# #La fonction translation permettra de déplacer nos images crées, afin de les | |
# # ramener au centre du repère. | |
# # Translation d'image | |
# def translation_img(src_img,shift_distance, shape_of_out_img): | |
# h,w = src_img.shape[:2] | |
# out_img=np.zeros(shape_of_out_img,src_img.dtype) | |
# for i in range(h): | |
# for j in range(w): | |
# new_x=j+shift_distance[0] | |
# new_y=i+shift_distance[1] | |
# if 0<=new_x<w and 0<=new_y<h: # Test d'écriture | |
# out_img[new_y,new_x]=src_img[i,j] | |
# return out_img | |
# # ENTREE = FORMES EN NOIR SUR FOND BLANC | |
# # INDIQUER LES NOMS DE VOS PROPRES FICHIERS ICI | |
# IN1=imread('Im4.png',as_gray=True) | |
# IN2=imread('Im5.png',as_gray=True) | |
# # Conversion en images binaires GRAY -> BOOLEAN | |
# IM1=(IN1==0) | |
# imshow(np.uint8(IM1)*255) # Pour la visualisation de l'image 1,on affiche | |
# #l'images en "8 bits" | |
# plt.show() | |
# IM2=(IN2==0) | |
# imshow(np.uint8(IM2)*255) #Pour la visualisation de l'image 2, | |
# #on affiche l'image en "8 bits" | |
# plt.show() | |
# # Positionnement / Centrage des 2 formes | |
# #Dans cette partie, nous allons positionner nos images crées au centre du | |
# #repère | |
# # centre de l'image | |
# (szx,szy)=IM1.shape | |
# centrex=int(szx/2) | |
# centrey=int(szy/2) | |
# tab = [] #Création d'un tableau vide pour stocker nos images pour | |
# #créer la vidéo | |
# # centrage Forme 1 (On centre la figure "coeur") | |
# (tabx, taby)=np.where(IM1==1) | |
# mx=int(np.sum(tabx)/np.size(tabx)) | |
# my=int(np.sum(taby)/np.size(taby)) | |
# dx=int(centrex-mx) | |
# dy=int(centrey-my) | |
# #Avec cette ligne de code, la nouvelle forme 1 au centre | |
# # est obtenue par la translation de l'image 1 au centre | |
# FORME1=translation_img(IM1,(dy,dx), IM1.shape) | |
# # centrage Forme 2 ( On centre la figure "étoile") | |
# (tabx, taby)=np.where(IM2==1) | |
# mx=np.rint(np.sum(tabx)/np.size(tabx)) | |
# my=np.rint(np.sum(taby)/np.size(taby)) | |
# dx=int(centrex-mx) | |
# dy=int(centrey-my) | |
# # Avec cette ligne de code, la nouvelle forme 2 au centre | |
# # est obtenue par la transformation de l'image 2 au centre. | |
# FORME2=translation_img(IM2,(dy,dx), IM2.shape) | |
# Img1 = np.uint8(FORME1)*255 | |
# imshow(Img1) # Pour la visualisation de l'image centrée en "8 bits" | |
# plt.show() | |
# Img2 = np.uint8(FORME2)*255 | |
# imshow(Img2) # Pour la visualisation de l'image centrée en "8 bits" | |
# plt.show() | |
# #png1 = imsave('4.png',Img1) #sauvegarder l'image Img1 | |
# #tab.append(imageio.imread('4.png')) | |
# #png2 = imsave('5.png',Img2) # sauvegarder l'image Im2 | |
# #tab.append(imageio.imread('5.png')) | |
# #Intersection des images | |
# IS=np.minimum(FORME1,FORME2) | |
# imshow(np.uint8(IS)*255) | |
# plt.title('Intersection des deux images') | |
# #Union des images | |
# UN=np.maximum(FORME1,FORME2) | |
# imshow(np.uint8(UN)*255) | |
# plt.title('Union des deux images') | |
# #Calcul de la forme médiane | |
# #X=IS | |
# #Res=IS #résultat de l'intersection | |
# #test=1 | |
# def mediane(FORME1,FORME2): #on définit une liste de médiane entre nos 2 formes | |
# IS=np.minimum(FORME1, FORME2) | |
# #imshow(np.uint8(IS)*255) | |
# UN=np.maximum(FORME1,FORME2) | |
# #imshow(np.uint8(UN)*255) | |
# Res=IS | |
# mediane=IS | |
# #Essaie avec un disque de rayon n=1 | |
# n=1 | |
# test=1 | |
# while test>0: | |
# D= dilation(IS,disk(n)) #Dilatation de l'intersection | |
# E = erosion(UN,disk(n)) #Erosion de l'union | |
# DE= np.minimum(D,E) # intersection entre les deux résultats D et E | |
# Res=np.maximum(Res,DE) #ligne | |
# mediane=np.maximum(mediane,Res) #on fait une union entre la mediane et le | |
# # Res trouvé dans la ligne précédente. | |
# n=n+1 #on incrémente à chaque fois le rayon ou la taille du disque | |
# if np.max(E)==0: #Quand il n'y'aura plus d'érosion | |
# test=0 #Test égal à zéro permet de sortir de la boucle. | |
# imshow(np.uint8(mediane)*255) | |
# plt.show() | |
# plt.title('mediane') | |
# return mediane | |
# Med=mediane(FORME1, FORME2) | |
# medianeliste=[FORME1,Med,FORME2] | |
# for j in range(4): | |
# nb=0 | |
# for i in range(len(medianeliste)-1): | |
# Med=medianeliste[i+nb] | |
# Med2=medianeliste[i+nb+1] | |
# Med3=mediane(Med, Med2) | |
# medianeliste.insert(i+nb+1, Med3) | |
# nb=nb+1 | |
# images=[] | |
# print(len(medianeliste)) | |
# for i in range(len(medianeliste)): | |
# #imshow(np.uint8(medianeliste[i]*255)) | |
# #plt.show() | |
# images.append(np.uint8(medianeliste[i]*255)) | |
# imageio.mimsave('testt.gif', images) | |
# Forme11=np.zeros(np.dot(FORME1.shape,4), FORME2.dtype) | |
# dx=0 | |
# dy=0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment