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
initial = MaskedImage(img,mask)
radius = radius
pyramid = [initial]
source = initial
while source.width>radius and source.height>radius:

@ -1,7 +1,7 @@
import numpy as np
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]
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):
for dy in range(-patchSize,patchSize):
if (y+dy < 0 or self.height <= y+dy):
continue
for dx in range(-patchSize,patchSize):
if (x+dx < 0 or self.width <= x+dx):
continue
if (y+dy < 0 or self.height <= y+dy):
continue
if self.mask[y+dy,x+dx]:
return True
return False
@ -60,7 +60,7 @@ class MaskedImage:
m += 1
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
else:
newmask[y // 2, x // 2] = True
@ -68,15 +68,10 @@ class MaskedImage:
return MaskedImage(image=newimage, mask=newmask)
def upscale(self, newH, newW):
y_indices = np.floor(np.linspace(0, self.height-1, newH)).astype(int)
x_indices = np.floor(np.linspace(0, self.width-1, newW)).astype(int)
y_coords, x_coords = np.meshgrid(y_indices, x_indices, indexing='ij')
newImage = MaskedImage(width=newW, height=newH)
for y in range(newH):
for x in range(newW):
xs = int(x*self.width/newW)
ys = int(y*self.height/newH)
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
newImage.image = self.image[y_coords, x_coords]
newImage.mask = self.mask[y_coords, x_coords]
return newImage

@ -80,22 +80,21 @@ class Nnf:
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
ssd_max = 255 * 255
distance, wsum = 0, ssd_max * (patchSize*2)**2
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):
wsum += ssd_max
xks, yks = xs + dx, ys + dy
xkt, ykt = xt + dx, yt + dy
if not (1 <= xks < source.width - 1 and 1 <= yks < source.height - 1):
xks, xkt = xs + dx, xt + dx
if not (1 <= xks < source.width - 1 and 1 <= xkt < target.width - 1):
distance += ssd_max
continue
if source.containsMask(xks, yks,patchSize):
distance += ssd_max
continue
if not (1 <= xkt < target.width - 1 and 1 <= ykt < target.height - 1):
distance += ssd_max
continue
if target.containsMask(xkt, ykt,patchSize):
distance += ssd_max
continue

Loading…
Cancel
Save