quelques optimization

master
Ludovic CASTIGLIA 4 months ago
parent 8f77457707
commit e3d2a4dd7a

@ -53,7 +53,6 @@ def doTheInpainting(img,mask,radius):
return newTarget, sourceToTarget, targetToSource return newTarget, sourceToTarget, targetToSource
initial = MaskedImage(img,mask) initial = MaskedImage(img,mask)
radius = radius
pyramid = [initial] pyramid = [initial]
source = initial source = initial
while source.width>radius and source.height>radius: while source.width>radius and source.height>radius:

@ -1,7 +1,7 @@
import numpy as np import numpy as np
class MaskedImage: class MaskedImage:
DSCALE = 65535 DSCALE = 10_000 # valeur arbitraire qui est le nombre max de la function de distance
base = [1.0, 0.99, 0.96, 0.83, 0.38, 0.11, 0.02, 0.005, 0.0006, 0.0001, 0] base = [1.0, 0.99, 0.96, 0.83, 0.38, 0.11, 0.02, 0.005, 0.0006, 0.0001, 0]
similarity = np.interp(np.linspace(0, 1, DSCALE + 1), np.linspace(0, 1, len(base)), base) similarity = np.interp(np.linspace(0, 1, DSCALE + 1), np.linspace(0, 1, len(base)), base)
@ -18,11 +18,11 @@ class MaskedImage:
def containsMask(self,x,y,patchSize): def containsMask(self,x,y,patchSize):
for dy in range(-patchSize,patchSize): for dy in range(-patchSize,patchSize):
if (y+dy < 0 or self.height <= y+dy):
continue
for dx in range(-patchSize,patchSize): for dx in range(-patchSize,patchSize):
if (x+dx < 0 or self.width <= x+dx): if (x+dx < 0 or self.width <= x+dx):
continue continue
if (y+dy < 0 or self.height <= y+dy):
continue
if self.mask[y+dy,x+dx]: if self.mask[y+dy,x+dx]:
return True return True
return False return False
@ -60,7 +60,7 @@ class MaskedImage:
m += 1 m += 1
if ksum > 0: if ksum > 0:
newimage[y // 2, x // 2] = [r // ksum, g // ksum, b // ksum] newimage[y // 2, x // 2] = (r // ksum, g // ksum, b // ksum)
newmask[y // 2, x // 2] = False newmask[y // 2, x // 2] = False
else: else:
newmask[y // 2, x // 2] = True newmask[y // 2, x // 2] = True
@ -68,15 +68,10 @@ class MaskedImage:
return MaskedImage(image=newimage, mask=newmask) return MaskedImage(image=newimage, mask=newmask)
def upscale(self, newH, newW): def upscale(self, newH, newW):
newImage = MaskedImage(width=newW,height=newH) y_indices = np.floor(np.linspace(0, self.height-1, newH)).astype(int)
for y in range(newH): x_indices = np.floor(np.linspace(0, self.width-1, newW)).astype(int)
for x in range(newW): y_coords, x_coords = np.meshgrid(y_indices, x_indices, indexing='ij')
xs = int(x*self.width/newW) newImage = MaskedImage(width=newW, height=newH)
ys = int(y*self.height/newH) newImage.image = self.image[y_coords, x_coords]
newImage.mask = self.mask[y_coords, x_coords]
if not self.mask[ys,xs]:
newImage.image[y,x] = self.image[ys,xs]
newImage.mask[y,x] = False
else:
newImage.mask[y,x] = True
return newImage return newImage

@ -80,22 +80,21 @@ class Nnf:
return distance(self.input,x,y,self.output,xp,yp,self.patchSize) return distance(self.input,x,y,self.output,xp,yp,self.patchSize)
def distance(source, xs, ys, target, xt, yt, patchSize): def distance(source, xs, ys, target, xt, yt, patchSize):
ssd_max = 9 * 255 * 255 ssd_max = 255 * 255
distance, wsum = 0, 0 distance, wsum = 0, ssd_max * (patchSize*2)**2
for dy in range(-patchSize, patchSize): for dy in range(-patchSize, patchSize):
yks, ykt = ys + dy, yt + dy
if not (1 <= yks < source.height - 1 and 1 <= ykt < target.height - 1):
distance += ssd_max * patchSize * 2
continue
for dx in range(-patchSize, patchSize): for dx in range(-patchSize, patchSize):
wsum += ssd_max xks, xkt = xs + dx, xt + dx
xks, yks = xs + dx, ys + dy if not (1 <= xks < source.width - 1 and 1 <= xkt < target.width - 1):
xkt, ykt = xt + dx, yt + dy
if not (1 <= xks < source.width - 1 and 1 <= yks < source.height - 1):
distance += ssd_max distance += ssd_max
continue continue
if source.containsMask(xks, yks,patchSize): if source.containsMask(xks, yks,patchSize):
distance += ssd_max distance += ssd_max
continue continue
if not (1 <= xkt < target.width - 1 and 1 <= ykt < target.height - 1):
distance += ssd_max
continue
if target.containsMask(xkt, ykt,patchSize): if target.containsMask(xkt, ykt,patchSize):
distance += ssd_max distance += ssd_max
continue continue

Loading…
Cancel
Save