|
|
|
@ -1,9 +1,11 @@
|
|
|
|
|
from maskedImage import MaskedImage
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
from nnf import Nnf
|
|
|
|
|
import numpy as np
|
|
|
|
|
import threading
|
|
|
|
|
import cv2
|
|
|
|
|
|
|
|
|
|
from maskedImage import MaskedImage
|
|
|
|
|
from nnf import Nnf
|
|
|
|
|
import concurrent.futures
|
|
|
|
|
|
|
|
|
|
def read(file):
|
|
|
|
|
img = plt.imread(file)
|
|
|
|
@ -41,7 +43,7 @@ def doTheInpainting(img,mask,radius):
|
|
|
|
|
upscaled = False
|
|
|
|
|
|
|
|
|
|
vote = np.zeros((newTarget.width, newTarget.height, 4))
|
|
|
|
|
ExpectationStep(targetToSource,False,vote,newSource,upscaled)
|
|
|
|
|
ExpectationStep(targetToSource,vote,newSource,upscaled)
|
|
|
|
|
MaximizationStep(newTarget, vote)
|
|
|
|
|
result = cv2.resize(newTarget.image, (initial.width, initial.height), interpolation=cv2.INTER_AREA)
|
|
|
|
|
plt.imshow(result)
|
|
|
|
@ -79,38 +81,39 @@ def doTheInpainting(img,mask,radius):
|
|
|
|
|
plt.pause(0.01)
|
|
|
|
|
return target.image
|
|
|
|
|
|
|
|
|
|
def ExpectationStep(nnf,sourceToTarget, vote, source, upscale):
|
|
|
|
|
for y in range(nnf.input.height):
|
|
|
|
|
for x in range(nnf.input.width):
|
|
|
|
|
xp, yp, dp = nnf.field[y,x]
|
|
|
|
|
w = MaskedImage.similarity[dp]
|
|
|
|
|
for dy in range(-nnf.patchSize,nnf.patchSize):
|
|
|
|
|
for dx in range(-nnf.patchSize,nnf.patchSize):
|
|
|
|
|
if sourceToTarget:
|
|
|
|
|
xs = x+dx
|
|
|
|
|
ys = y+dy
|
|
|
|
|
xt = xp+dx
|
|
|
|
|
yt = yp+dy
|
|
|
|
|
else:
|
|
|
|
|
def ExpectationStep(nnf, vote, source, upscale):
|
|
|
|
|
def ExpectationStepForNb(nb):
|
|
|
|
|
hei = nnf.input.height//7
|
|
|
|
|
for y in range(nb*hei,(nb+1)*hei if nb != 7 else nnf.input.height):
|
|
|
|
|
for x in range(nnf.input.width):
|
|
|
|
|
xp, yp, dp = nnf.field[y,x]
|
|
|
|
|
w = MaskedImage.similarity[dp]
|
|
|
|
|
for dy in range(-nnf.patchSize,nnf.patchSize):
|
|
|
|
|
for dx in range(-nnf.patchSize,nnf.patchSize):
|
|
|
|
|
xs = xp+dx
|
|
|
|
|
ys = yp+dy
|
|
|
|
|
xt = x+dx
|
|
|
|
|
yt = y+dy
|
|
|
|
|
if not 0<=xs<nnf.input.width:
|
|
|
|
|
continue
|
|
|
|
|
if not 0<=ys<nnf.input.height:
|
|
|
|
|
continue
|
|
|
|
|
if not 0<=xt<nnf.input.width:
|
|
|
|
|
continue
|
|
|
|
|
if not 0<=yt<nnf.input.height:
|
|
|
|
|
continue
|
|
|
|
|
if upscale:
|
|
|
|
|
weightedCopy(source,2*xs,2*ys,vote,2*xt,2*yt,w)
|
|
|
|
|
weightedCopy(source,2*xs+1,2*ys,vote,2*xt+1,2*yt,w)
|
|
|
|
|
weightedCopy(source,2*xs,2*ys+1,vote,2*xt,2*yt+1,w)
|
|
|
|
|
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)
|
|
|
|
|
if not 0<=xs<nnf.input.width:
|
|
|
|
|
continue
|
|
|
|
|
if not 0<=ys<nnf.input.height:
|
|
|
|
|
continue
|
|
|
|
|
if not 0<=xt<nnf.input.width:
|
|
|
|
|
continue
|
|
|
|
|
if not 0<=yt<nnf.input.height:
|
|
|
|
|
continue
|
|
|
|
|
if upscale:
|
|
|
|
|
weightedCopy(source,2*xs,2*ys,vote,2*xt,2*yt,w)
|
|
|
|
|
weightedCopy(source,2*xs+1,2*ys,vote,2*xt+1,2*yt,w)
|
|
|
|
|
weightedCopy(source,2*xs,2*ys+1,vote,2*xt,2*yt+1,w)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
pool = concurrent.futures.ThreadPoolExecutor(max_workers=8)
|
|
|
|
|
for i in range(8):
|
|
|
|
|
pool.submit(ExpectationStepForNb,i)
|
|
|
|
|
pool.shutdown(wait=True)
|
|
|
|
|
|
|
|
|
|
def weightedCopy(src,xs,ys,vote,xd,yd,w):
|
|
|
|
|
if src.mask[ys,xs]:
|
|
|
|
|