From 7a5e1081bccb18676d45d87e66745ecd6817a631 Mon Sep 17 00:00:00 2001 From: Ludovic CASTIGLIA Date: Sun, 9 Feb 2025 14:13:03 +0100 Subject: [PATCH] opti et simplification --- function.py | 4 ---- maskedImage.py | 4 ++-- nnf.py | 4 ++-- selection.py | 39 +++++++++++++++++++++++++++++---------- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/function.py b/function.py index 2012305..4eb70b0 100644 --- a/function.py +++ b/function.py @@ -21,16 +21,13 @@ def doTheInpainting(img,mask,radius): newTarget = None for emloop in range(1,iterEM+1): if (newTarget != None): - sourceToTarget.output = newTarget 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) targetToSource.field[y,x] = (x,y,0) - sourceToTarget.minimize(iterNnf) targetToSource.minimize(iterNnf) upscaled = False @@ -44,7 +41,6 @@ def doTheInpainting(img,mask,radius): upscaled = False vote = np.zeros((newTarget.width, newTarget.height, 4)) - ExpectationStep(sourceToTarget,True,vote,newSource,upscaled) ExpectationStep(targetToSource,False,vote,newSource,upscaled) MaximizationStep(newTarget, vote) result = cv2.resize(newTarget.image, (initial.width, initial.height), interpolation=cv2.INTER_AREA) diff --git a/maskedImage.py b/maskedImage.py index 6feb19d..f4dfe1e 100644 --- a/maskedImage.py +++ b/maskedImage.py @@ -3,7 +3,7 @@ import numpy as np class MaskedImage: 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) + 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): if image is not None: @@ -26,7 +26,7 @@ class MaskedImage: if self.mask[y+dy,x+dx]: return True return False - + def copy(self): return MaskedImage(image=self.image.copy(), mask=self.mask.copy()) diff --git a/nnf.py b/nnf.py index 146a332..8508561 100644 --- a/nnf.py +++ b/nnf.py @@ -68,7 +68,7 @@ class Nnf: zoneRecherche = max(self.output.height,self.output.width) while zoneRecherche>0: 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)) yp = max(0,min(self.output.height-1,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) 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 for dy in range(-patchSize, patchSize): yks, ykt = ys + dy, yt + dy diff --git a/selection.py b/selection.py index 5191155..fc6eb29 100644 --- a/selection.py +++ b/selection.py @@ -6,27 +6,30 @@ import numpy as np def onselect(eclick, erelease): x1, y1 = int(eclick.xdata), int(eclick.ydata) x2, y2 = int(erelease.xdata), int(erelease.ydata) - img[y1:y2,x1:x2] = (255,0,0) - mask[y1:y2,x1:x2] = True + img[y1:y2+1,x1:x2+1] = (255,0,0) + mask[y1:y2+1,x1:x2+1] = True + maskImg[y1:y2+1,x1:x2+1] = 255 ax.imshow(img) plt.draw() - - -img = read(input("lien relatif de l'image: ")) -img = img.copy() +imgOr = read(input("lien relatif de l'image: ")) +img = imgOr.copy() rep = input("avez vous le mask de l'image ?(y:n)") yes = ["y","Y","yes","YES","Yes","o","O","oui","OUI"] height, width = img.shape[:2] if (rep in yes): maskImg = read(input("lien relatife de l'image: ")) + if (len(maskImg.shape) == 3): + maskImg = maskImg[:,:,0] mask = maskImg == 255 img[mask] = (255,0,0) else: fig, ax = plt.subplots() + plt.axis("off") ax.imshow(img) mask = np.zeros((height, width), dtype=bool) + maskImg = np.zeros((height, width), dtype=np.uint8) toggle_selector = RectangleSelector(ax, onselect, useblit=True, button=[1], minspanx=5, minspany=5, spancoords='pixels', interactive=True) @@ -42,7 +45,23 @@ plt.title("mask de l'image") plt.pause(1) plt.title("génération de l'image") -img = doTheInpainting(img,mask,2) -plt.imshow(img) -plt.title("image final") -plt.pause(20) +res = doTheInpainting(img,mask,2) +plt.ioff() + +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)