simplification

master
Ludovic CASTIGLIA 4 months ago
parent 067d1000f6
commit be0997a6fd

@ -25,14 +25,10 @@ def doTheInpainting(img,mask,radius):
targetToSource.input = newTarget
target = newTarget
newTarget = None
for y in range(source.height):
for x in range(source.width):
if not source.containsMask(x,y,radius):
sourceToTarget.field[y,x] = (x,y,0)
for y in range(target.height):
for x in range(target.width):
if not source.containsMask(x,y,radius):
targetToSource.field[y,x] = (x,y,0)
sourceToTarget.minimize(iterNnf)
targetToSource.minimize(iterNnf)
@ -122,7 +118,7 @@ def ExpectationStep(nnf,sourceToTarget, vote, source, upscale):
weightedCopy(source,2*xs+1,2*ys+1,vote,2*xt+1,2*yt+1,w)
else:
weightedCopy(source,xs,ys,vote,xt,yt,w)
def weightedCopy(src,xs,ys,vote,xd,yd,w):
if src.mask[ys,xs]:
return
@ -139,4 +135,4 @@ def MaximizationStep(target,vote):
g = int(vote[x,y,1]/vote[x,y,3])
b = int(vote[x,y,2]/vote[x,y,3])
target.image[y,x] = (r,g,b)
target.mask[y,x] = False
target.mask[y,x] = False

@ -66,7 +66,7 @@ class MaskedImage:
newmask[y // 2, x // 2] = True
return MaskedImage(image=newimage, mask=newmask)
def upscale(self, newH, newW):
newImage = MaskedImage(width=newW,height=newH)
for y in range(newH):
@ -75,10 +75,8 @@ class MaskedImage:
ys = int(y*self.height/newH)
if not self.mask[ys,xs]:
newImage.image[y,x,0] = self.image[ys,xs,0]
newImage.image[y,x,1] = self.image[ys,xs,1]
newImage.image[y,x,2] = self.image[ys,xs,2]
newImage.image[y,x] = self.image[ys,xs]
newImage.mask[y,x] = False
else:
newImage.mask[y,x] = True
return newImage
return newImage

@ -30,5 +30,3 @@ img = doTheInpainting(img,mask,2)
plt.imshow(img)
plt.title("image final")
plt.pause(20)

@ -9,10 +9,11 @@ class Nnf:
self.patchSize = patchSize
def randomize(self):
self.field = np.zeros((self.input.height, self.input.width, 3), dtype=int)
for x in range(self.input.width):
for y in range(self.input.height):
self.field[y,x] = (random.randint(0,self.output.width),random.randint(0,self.output.height),MaskedImage.DSCALE)
self.field[:,:,2] += MaskedImage.DSCALE
self.field[:,:,0] = np.random.randint(0,self.output.width,self.input.width)
self.field[:,:,1] = np.random.randint(0,self.output.height,self.input.height)
self.initialize()
def initializeFromNnf(self,nnf):
@ -30,8 +31,8 @@ class Nnf:
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
iter= 0
maxIter = 10
while (self.field[y,x,2] == MaskedImage.DSCALE and iter<maxIter):
self.field[y,x] = (random.randint(0,self.output.width),random.randint(0,self.output.height),self.distance(x,y,self.field[y,x,0],self.field[y,x,1]))
iter += 1
@ -54,9 +55,7 @@ class Nnf:
yp = self.field[y,x-direction,1]
dp = self.distance(x,y,xp,yp)
if (dp<self.field[y,x,2]):
self.field[y,x,0] = xp
self.field[y,x,1] = yp
self.field[y,x,2] = dp
self.field[y,x] = (xp,yp,dp)
# verticale
if (0<y-direction<self.input.height):
@ -64,9 +63,7 @@ class Nnf:
yp = self.field[y-direction,x,1] + direction
dp = self.distance(x,y,xp,yp)
if (dp<self.field[y,x,2]):
self.field[y,x,0] = xp
self.field[y,x,1] = yp
self.field[y,x,2] = dp
self.field[y,x] = (xp,yp,dp)
# recherche random
zoneRecherche = max(self.output.height,self.output.width)
@ -77,15 +74,12 @@ class Nnf:
yp = max(0,min(self.output.height-1,yp))
dp = self.distance(x,y,xp,yp)
if (dp<self.field[y,x,2]):
self.field[y,x,0] = xp
self.field[y,x,1] = yp
self.field[y,x,2] = dp
self.field[y,x] = (xp,yp,dp)
zoneRecherche = int(zoneRecherche/2)
def distance(self,x,y,xp,yp):
return distance(self.input,x,y,self.output,xp,yp,self.patchSize)
def distance(source, xs, ys, target, xt, yt, patchSize):
ssd_max = 9 * 255 * 255
distance, wsum = 0, 0
@ -108,4 +102,4 @@ def distance(source, xs, ys, target, xt, yt, patchSize):
continue
ssd = np.sum((source.image[yks, xks] - target.image[ykt, xkt]) ** 2)
distance += ssd
return int(MaskedImage.DSCALE * distance / wsum)
return int(MaskedImage.DSCALE * distance / wsum)

Loading…
Cancel
Save