opti et simplification

master
Ludovic CASTIGLIA 4 months ago
parent e3d2a4dd7a
commit 7a5e1081bc

@ -21,16 +21,13 @@ def doTheInpainting(img,mask,radius):
newTarget = None newTarget = None
for emloop in range(1,iterEM+1): for emloop in range(1,iterEM+1):
if (newTarget != None): if (newTarget != None):
sourceToTarget.output = newTarget
targetToSource.input = newTarget targetToSource.input = newTarget
target = newTarget target = newTarget
newTarget = None newTarget = None
for y in range(source.height): for y in range(source.height):
for x in range(source.width): for x in range(source.width):
if not source.containsMask(x,y,radius): if not source.containsMask(x,y,radius):
sourceToTarget.field[y,x] = (x,y,0)
targetToSource.field[y,x] = (x,y,0) targetToSource.field[y,x] = (x,y,0)
sourceToTarget.minimize(iterNnf)
targetToSource.minimize(iterNnf) targetToSource.minimize(iterNnf)
upscaled = False upscaled = False
@ -44,7 +41,6 @@ def doTheInpainting(img,mask,radius):
upscaled = False upscaled = False
vote = np.zeros((newTarget.width, newTarget.height, 4)) vote = np.zeros((newTarget.width, newTarget.height, 4))
ExpectationStep(sourceToTarget,True,vote,newSource,upscaled)
ExpectationStep(targetToSource,False,vote,newSource,upscaled) ExpectationStep(targetToSource,False,vote,newSource,upscaled)
MaximizationStep(newTarget, vote) MaximizationStep(newTarget, vote)
result = cv2.resize(newTarget.image, (initial.width, initial.height), interpolation=cv2.INTER_AREA) result = cv2.resize(newTarget.image, (initial.width, initial.height), interpolation=cv2.INTER_AREA)

@ -3,7 +3,7 @@ import numpy as np
class MaskedImage: class MaskedImage:
DSCALE = 10_000 # valeur arbitraire qui est le nombre max de la function de distance 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), np.linspace(0, 1, len(base)), base)
def __init__(self,image=None,mask=None,width=None,height=None): def __init__(self,image=None,mask=None,width=None,height=None):
if image is not None: if image is not None:

@ -68,7 +68,7 @@ class Nnf:
zoneRecherche = max(self.output.height,self.output.width) zoneRecherche = max(self.output.height,self.output.width)
while zoneRecherche>0: while zoneRecherche>0:
xp = self.field[y,x,0] + random.randint(0,2*zoneRecherche)-zoneRecherche xp = self.field[y,x,0] + random.randint(0,2*zoneRecherche)-zoneRecherche
xp = self.field[y,x,1] + 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)) xp = max(0,min(self.output.width-1,xp))
yp = max(0,min(self.output.height-1,yp)) yp = max(0,min(self.output.height-1,yp))
dp = self.distance(x,y,xp,yp) dp = self.distance(x,y,xp,yp)
@ -80,7 +80,7 @@ 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 = 255 * 255 ssd_max = 255 ** 2
distance, wsum = 0, ssd_max * (patchSize*2)**2 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 yks, ykt = ys + dy, yt + dy

@ -6,27 +6,30 @@ import numpy as np
def onselect(eclick, erelease): def onselect(eclick, erelease):
x1, y1 = int(eclick.xdata), int(eclick.ydata) x1, y1 = int(eclick.xdata), int(eclick.ydata)
x2, y2 = int(erelease.xdata), int(erelease.ydata) x2, y2 = int(erelease.xdata), int(erelease.ydata)
img[y1:y2,x1:x2] = (255,0,0) img[y1:y2+1,x1:x2+1] = (255,0,0)
mask[y1:y2,x1:x2] = True mask[y1:y2+1,x1:x2+1] = True
maskImg[y1:y2+1,x1:x2+1] = 255
ax.imshow(img) ax.imshow(img)
plt.draw() plt.draw()
imgOr = read(input("lien relatif de l'image: "))
img = imgOr.copy()
img = read(input("lien relatif de l'image: "))
img = img.copy()
rep = input("avez vous le mask de l'image ?(y:n)") rep = input("avez vous le mask de l'image ?(y:n)")
yes = ["y","Y","yes","YES","Yes","o","O","oui","OUI"] yes = ["y","Y","yes","YES","Yes","o","O","oui","OUI"]
height, width = img.shape[:2] height, width = img.shape[:2]
if (rep in yes): if (rep in yes):
maskImg = read(input("lien relatife de l'image: ")) maskImg = read(input("lien relatife de l'image: "))
if (len(maskImg.shape) == 3):
maskImg = maskImg[:,:,0]
mask = maskImg == 255 mask = maskImg == 255
img[mask] = (255,0,0) img[mask] = (255,0,0)
else: else:
fig, ax = plt.subplots() fig, ax = plt.subplots()
plt.axis("off")
ax.imshow(img) ax.imshow(img)
mask = np.zeros((height, width), dtype=bool) mask = np.zeros((height, width), dtype=bool)
maskImg = np.zeros((height, width), dtype=np.uint8)
toggle_selector = RectangleSelector(ax, onselect, useblit=True, toggle_selector = RectangleSelector(ax, onselect, useblit=True,
button=[1], minspanx=5, minspany=5, spancoords='pixels', button=[1], minspanx=5, minspany=5, spancoords='pixels',
interactive=True) interactive=True)
@ -42,7 +45,23 @@ plt.title("mask de l'image")
plt.pause(1) plt.pause(1)
plt.title("génération de l'image") plt.title("génération de l'image")
img = doTheInpainting(img,mask,2) res = doTheInpainting(img,mask,2)
plt.imshow(img) plt.ioff()
plt.title("image final")
plt.pause(20) if input("afficher le résultat avant après (y/n): ") in yes:
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.imshow(imgOr)
ax1.set_title('Original')
ax1.axis("off")
ax2.imshow(res)
ax2.set_title('Result')
ax2.axis("off")
plt.show()
if input("enregister le mask (y/n):") in yes:
save_path = input("Entrez le chemin d'enregistrement du mask: ")
plt.imsave(save_path, maskImg, cmap='gray')
if input("enregister l'image produite (y/n):") in yes:
save_path = input("Entrez le chemin d'enregistrement de l'image: ")
plt.imsave(save_path, res)

Loading…
Cancel
Save