from maskedImage import MaskedImage import numpy as np import random class ImageRelation: def __init__(self,input,output,patchSize): self.input = input self.output = output self.patchSize = patchSize def randomize(self): # on crée un assignation zone patch de manière random self.field = np.zeros((self.input.height, self.input.width, 3), dtype=int) self.field[:,:,2] = MaskedImage.DSCALE # tant que la réel distance est pas calculé, elle est concidéré comme maximal self.field[:,:,0] = np.random.randint(0,self.output.width,(self.input.height,self.input.width)) self.field[:,:,1] = np.random.randint(0,self.output.height,(self.input.height,self.input.width)) self.initialize() # on calcule la vrai distance et on change les patch si ils ne conviennent pas def initializeFromImageRelation(self,imRel): # on crée l'assignation zone patch à partir des assignations précédentes self.field = np.zeros((self.input.height, self.input.width, 3), dtype=int) fx = int(self.input.width/imRel.input.width) fy = int(self.input.height/imRel.input.height) for y in range(self.input.height): for x in range(self.input.width): xl = min(int(x/fx),imRel.input.width-1) yl = min(int(y/fy),imRel.input.height-1) self.field[y,x] = (imRel.field[yl,xl,0]*fx, imRel.field[yl,xl,1]*fy, MaskedImage.DSCALE) self.initialize() # on calcule la vrai distance et on change si ils ne conviennent pas def initialize(self): for y in range(self.input.height): for x in range(self.input.width): self.field[y,x,2] = self.distance(x,y,self.field[y,x,0],self.field[y,x,1]) # calcule la vrai distance des patchs iter= 0 maxIter = 10 # au cas ou pour ne pas rester bloqué par manque de chance while (self.field[y,x,2] == MaskedImage.DSCALE and iter0): self.findBestPatchFroOne(x,y,i) for y in range(self.input.height-1,0,-1): # on cherche le meilleur patche en haut et à gauche for x in range(self.input.width-1,0,-1): if (self.field[y,x,2]>0): self.findBestPatchFroOne(x,y,-i) def findBestPatchFroOne(self,x,y,direction): # recherche le meilleur patch pour une zone en particulier dans un sens (en haut et à gauche | à droite et en bas) + cherche random # horizontale if (00: xp = self.field[y,x,0] + random.randint(0,2*zoneRecherche)-zoneRecherche yp = self.field[y,x,1] + random.randint(0,2*zoneRecherche)-zoneRecherche xp = max(0,min(self.output.width-1,xp)) yp = max(0,min(self.output.height-1,yp)) dp = self.distance(x,y,xp,yp) if (dp