|
|
|
@ -4,11 +4,93 @@ import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def doPatchMatch(img,x1,y1,x2,y2,patchSize=17):
|
|
|
|
|
def doPatchMatch(img,x1,y1,x2,y2,patchSize=17,nbRadomPatch=10):
|
|
|
|
|
|
|
|
|
|
def dist(patchValue1,patchValue2):
|
|
|
|
|
def getPatchFromCoord(x,y):
|
|
|
|
|
patch = np.array([[i, j] for i in range(patchSize) for j in range(patchSize)])
|
|
|
|
|
patch[:,0] = patch[:,0] + x
|
|
|
|
|
patch[:,1] = patch[:,1] + y
|
|
|
|
|
return patch
|
|
|
|
|
|
|
|
|
|
def distance(patchValue1,patchValue2):
|
|
|
|
|
mask = np.all(patchValue1 == [-1, -1, -1, -1], axis=-1)
|
|
|
|
|
return np.sum((patchValue1[~mask] - patchValue2[~mask]) ** 2)
|
|
|
|
|
|
|
|
|
|
def getBestNeigbourPatch(xy,ogValue,ogDist,step):
|
|
|
|
|
x, y = xy
|
|
|
|
|
|
|
|
|
|
dist = -1
|
|
|
|
|
|
|
|
|
|
xt, yt = x+step, y
|
|
|
|
|
if (0 <= xt <= width - patchSize and 0 <= yt <= height - patchSize):
|
|
|
|
|
patch = getPatchFromCoord(xt,yt)
|
|
|
|
|
patchValue = patchToValue(patch)
|
|
|
|
|
dist = distance(ogValue,patchValue)
|
|
|
|
|
|
|
|
|
|
xt, yt = x-step, y
|
|
|
|
|
if (0 <= xt <= width - patchSize and 0 <= yt <= height - patchSize):
|
|
|
|
|
tpatch = getPatchFromCoord(xt,yt)
|
|
|
|
|
tpatchValue = patchToValue(tpatch)
|
|
|
|
|
tdist = distance(ogValue,tpatchValue)
|
|
|
|
|
if tdist < dist or dist == -1:
|
|
|
|
|
dist = tdist
|
|
|
|
|
patch = tpatch
|
|
|
|
|
patchValue = tpatchValue
|
|
|
|
|
|
|
|
|
|
xt, yt = x, y+step
|
|
|
|
|
if (0 <= xt <= width - patchSize and 0 <= yt <= height - patchSize):
|
|
|
|
|
tpatch = getPatchFromCoord(xt,yt)
|
|
|
|
|
tpatchValue = patchToValue(tpatch)
|
|
|
|
|
tdist = distance(ogValue,tpatchValue)
|
|
|
|
|
if tdist < dist or dist == -1:
|
|
|
|
|
dist = tdist
|
|
|
|
|
patch = tpatch
|
|
|
|
|
patchValue = tpatchValue
|
|
|
|
|
|
|
|
|
|
xt, yt = x, y-step
|
|
|
|
|
if (0 <= xt <= width - patchSize and 0 <= yt <= height - patchSize):
|
|
|
|
|
tpatch = getPatchFromCoord(xt,yt)
|
|
|
|
|
tpatchValue = patchToValue(tpatch)
|
|
|
|
|
tdist = distance(ogValue,tpatchValue)
|
|
|
|
|
if tdist < dist or dist == -1:
|
|
|
|
|
dist = tdist
|
|
|
|
|
patch = tpatch
|
|
|
|
|
patchValue = tpatchValue
|
|
|
|
|
if dist == -1:
|
|
|
|
|
return False, None, None, None
|
|
|
|
|
return dist < ogDist, patch, patchValue, dist
|
|
|
|
|
|
|
|
|
|
def getTheBestPatch(addr,ogValue):
|
|
|
|
|
patchs = []
|
|
|
|
|
patchsValue = []
|
|
|
|
|
dists = []
|
|
|
|
|
for i in range(nbRadomPatch):
|
|
|
|
|
x,y = getRandomPatch()
|
|
|
|
|
patch = getPatchFromCoord(x,y)
|
|
|
|
|
patchValue = patchToValue(patch)
|
|
|
|
|
dist = distance(ogValue,patchValue)
|
|
|
|
|
patchs.append(patch)
|
|
|
|
|
patchsValue.append(patchValue)
|
|
|
|
|
dists.append(dist)
|
|
|
|
|
|
|
|
|
|
minIdx = np.argmin(np.array(dist))
|
|
|
|
|
patch = patchs[minIdx]
|
|
|
|
|
patchValue = patchsValue[minIdx]
|
|
|
|
|
|
|
|
|
|
ogDist = dists[minIdx]
|
|
|
|
|
foundNew = True
|
|
|
|
|
step = patchSize*5
|
|
|
|
|
while foundNew:
|
|
|
|
|
foundNew, tpatch, tpatchValue, tdist = getBestNeigbourPatch(addr[0],ogValue,ogDist,step)
|
|
|
|
|
if (foundNew):
|
|
|
|
|
patch = tpatch
|
|
|
|
|
patchValue = tpatchValue
|
|
|
|
|
ogDist = tdist
|
|
|
|
|
else:
|
|
|
|
|
step = int(step/2)
|
|
|
|
|
foundNew = step > 0
|
|
|
|
|
return patch, patchValue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def patchToValue(patch):
|
|
|
|
|
return img[patch[0][1]:patch[len(patch)-1][1], patch[0][0]:patch[len(patch)-1][0]]
|
|
|
|
@ -62,15 +144,9 @@ def doPatchMatch(img,x1,y1,x2,y2,patchSize=17):
|
|
|
|
|
|
|
|
|
|
def loop(perimiter):
|
|
|
|
|
x,y = getRandomFromPerimiter(perimiter)
|
|
|
|
|
addr = np.array([[i, j] for i in range(patchSize) for j in range(patchSize)]) - semiPatch
|
|
|
|
|
addr[:,0] = addr[:,0] + x
|
|
|
|
|
addr[:,1] = addr[:,1] + y
|
|
|
|
|
addr = getPatchFromCoord(x,y)
|
|
|
|
|
ogValue = patchToValue(addr)
|
|
|
|
|
px,py = getRandomPatch()
|
|
|
|
|
patch = np.array([[i, j] for i in range(patchSize) for j in range(patchSize)])
|
|
|
|
|
patch[:,0] = patch[:,0] + px
|
|
|
|
|
patch[:,1] = patch[:,1] + py
|
|
|
|
|
patchValue = patchToValue(patch)
|
|
|
|
|
patch,patchValue = getTheBestPatch(addr,ogValue)
|
|
|
|
|
applyPatch(patch,addr)
|
|
|
|
|
perimiter = removeAndAddFromPerimiter(perimiter,addr)
|
|
|
|
|
return perimiter
|
|
|
|
@ -82,12 +158,18 @@ def doPatchMatch(img,x1,y1,x2,y2,patchSize=17):
|
|
|
|
|
semiPatch = int(patchSize/2)
|
|
|
|
|
height, width, _ = img.shape
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
perimiter = initializePermimiter()
|
|
|
|
|
it = 0
|
|
|
|
|
|
|
|
|
|
# perimiter = loop(perimiter)
|
|
|
|
|
# perimiter = loop(perimiter)
|
|
|
|
|
# perimiter = loop(perimiter)
|
|
|
|
|
# for coord in perimiter:
|
|
|
|
|
# img[coord[1], coord[0]] = [1,1,1,1]
|
|
|
|
|
# img[img == -1] = 0
|
|
|
|
|
|
|
|
|
|
while len(perimiter)> 0:
|
|
|
|
|
it += 1
|
|
|
|
|
perimiter = loop(perimiter)
|
|
|
|
|