import random import numpy as np from maskedImage import MaskedImage class Nnf: def __init__(self,input,output,patchSize): self.input = input self.output = output self.patchSize = patchSize def randomize(self): self.field = np.zeros((self.input.height, self.input.width, 3), dtype=int) self.field[:,:,2] = MaskedImage.DSCALE 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() def initializeFromNnf(self,nnf): self.field = np.zeros((self.input.height, self.input.width, 3), dtype=int) fx = int(self.input.width/nnf.input.width) fy = int(self.input.height/nnf.input.height) for y in range(self.input.height): for x in range(self.input.width): xl = min(int(x/fx),nnf.input.width-1) yl = min(int(y/fy),nnf.input.height-1) self.field[y,x] = (nnf.field[yl,xl,0]*fx, nnf.field[yl,xl,1]*fy, MaskedImage.DSCALE) self.initialize() 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]) iter= 0 maxIter = 10 while (self.field[y,x,2] == MaskedImage.DSCALE and iter0): self.minimizeLink(x,y,1) for y in range(self.input.height-1,0,-1): for x in range(self.input.width-1,0,-1): if (self.field[y,x,2]>0): self.minimizeLink(x,y,-1) def minimizeLink(self,x,y,direction): # 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