deuxième test pour patchMatch et implémentation knn

test
Ludovic CASTIGLIA 4 months ago
parent abdfaf7304
commit d83e36d227

@ -0,0 +1,90 @@
from matplotlib.widgets import RectangleSelector
import matplotlib.pyplot as plt
import numpy as np
def doKnn(img,x1,y1,x2,y2):
def getNotInBoundNeighbour(neighbour, x1,y1,x2,y2):
mask = np.logical_or(
np.logical_or(neighbour[:, 0] < y1, neighbour[:, 0] > y2),
np.logical_or(neighbour[:, 1] < x1, neighbour[:, 1] > x2)
)
return neighbour[mask]
def neighbourReelPixel(x,y):
tNeighbour = np.copy(neighbour)
tNeighbour = tNeighbour + np.array([y,x])
return tNeighbour
def getAvgPixelFromNeighbour(neighbour):
return np.mean(img[neighbour[:,0],neighbour[:,1]], axis=0)
neighbour = np.array([[-1,-1],[-1,0],[0,-1],[-1,1],[1,-1],[0,1],[1,0],[1,1]])
x1c = x1
y1c = y1
x2c = x2
y2c = y2
while x1 != x2 and y1 != y2:
for x in range(x1,x2):
currentNeighbour1 = neighbourReelPixel(x,y1)
currentNeighbour2 = neighbourReelPixel(x,y2)
currentNeighbour1 = getNotInBoundNeighbour(currentNeighbour1,x1,y1,x2,y2)
currentNeighbour2 = getNotInBoundNeighbour(currentNeighbour2,x1,y1,x2,y2)
currentColor1 = getAvgPixelFromNeighbour(currentNeighbour1)
currentColor2 = getAvgPixelFromNeighbour(currentNeighbour2)
img[y1,x] = currentColor1
img[y2,x] = currentColor2
for y in range(y1,y2):
currentNeighbour1 = neighbourReelPixel(x1,y)
currentNeighbour2 = neighbourReelPixel(x2,y)
currentNeighbour1 = getNotInBoundNeighbour(currentNeighbour1,x1,y1,x2,y2)
currentNeighbour2 = getNotInBoundNeighbour(currentNeighbour2,x1,y1,x2,y2)
currentColor1 = getAvgPixelFromNeighbour(currentNeighbour1)
currentColor2 = getAvgPixelFromNeighbour(currentNeighbour2)
img[y,x1] = currentColor1
img[y,x2] = currentColor2
x1 += 1
x2 -= 1
y1 += 1
y2 -= 1
for x in range(x1c, x2c):
for y in range(y1c, y2c):
currentNeighbour = neighbourReelPixel(x, y)
currentNeighbour = getNotInBoundNeighbour(currentNeighbour,0,0,0,0)
currentColor = getAvgPixelFromNeighbour(currentNeighbour)
img[y, x] = currentColor
img[y1:y2,x1:x2]
return img
img = plt.imread('boat.png')
if len(img.shape) == 2:
img = np.stack((img,)*3, axis=-1)
def onselect(eclick, erelease):
x1, y1 = eclick.xdata, eclick.ydata
x2 = x1 + 150
x2, y2 = erelease.xdata, erelease.ydata
img_copy = np.copy(img)
res = doKnn(img_copy,int(x1),int(y1),int(x2),int(y2))
ax.imshow(res)
plt.draw()
print("drawed")
fig, ax = plt.subplots()
ax.imshow(img)
toggle_selector = RectangleSelector(ax, onselect, useblit=True,
button=[1], minspanx=5, minspany=5, spancoords='pixels',
interactive=True)
plt.axis('off')
plt.show()

@ -51,7 +51,7 @@ def patch_match(img, patch_size=3, iterations=1):
# Load the image using matplotlib
img = plt.imread('/home/UCA/lucastigli/patchMatch/boat.png')
img = plt.imread('boat.png')
def onselect(eclick, erelease):
@ -60,7 +60,6 @@ def onselect(eclick, erelease):
x2, y2 = erelease.xdata, erelease.ydata
avg_color = np.mean(img, axis=(0, 1))
img_copy = np.copy(img)
img_copy[int(y1):int(y2), int(x1):int(x2)] = avg_color
res = patch_match(img_copy)

@ -5,12 +5,19 @@ import numpy as np
def patchMatch(img,x1,y1,x2,y2,patchSize=20, iterations=1000):
height, width, _ = img.shape
img_copy = np.copy(img)
xx1 = max(0, x1-patchSize)
yy1 = max(0, y1-patchSize)
xx2 = min(width, x2+patchSize)
yy2 = min(height, y2+patchSize)
mask = np.ones((height, width), dtype=bool)
mask[yy1:yy2, xx1:xx2] = False
mask[y1:y2, x1:x2] = True
img[int(y1):int(y2), int(x1):int(x2)] = np.mean(img[~mask], axis=(0, 1))
img_copy = np.copy(img)
div = 10
if (xx2-xx1 < patchSize or yy2-yy1 < patchSize):
return img
@ -26,7 +33,7 @@ def patchMatch(img,x1,y1,x2,y2,patchSize=20, iterations=1000):
return np.sum((patch1 - patch2) ** 2)
def gradientDescent(x, y, bestPatch, bestDistance):
neighbors = [(-1,0), (1,0), (0,-1), (0,1), (-1,-1), (-1,1), (1,-1), (1,1)]
neighbors = [(-div,0), (div,0), (0,-div), (0,div), (-div,-div), (-div,div), (div,-div), (div,div)]
patch = img[y:y + patchSize, x:x + patchSize]
hasChanged = True
while hasChanged:
@ -46,8 +53,8 @@ def patchMatch(img,x1,y1,x2,y2,patchSize=20, iterations=1000):
return bestPatch, bestDistance
for x in range(xx1,xx2-patchSize,int(patchSize/10)):
for y in range(yy1,yy2-patchSize,int(patchSize/10)):
for x in range(xx1,xx2-patchSize,int(patchSize/div)):
for y in range(yy1,yy2-patchSize,int(patchSize/div)):
px, py = getRandomPatch()
bestPatch = [px, py]
bestDistance = distance(img[y:y+patchSize,x:x+patchSize], img[py:py+patchSize,px:px+patchSize])
@ -79,10 +86,7 @@ def onselect(eclick, erelease):
x2 = x1 + 150
x2, y2 = erelease.xdata, erelease.ydata
avg_color = np.mean(img, axis=(0, 1))
img_copy = np.copy(img)
img_copy[int(y1):int(y2), int(x1):int(x2)] = avg_color
res = patchMatch(img_copy,int(x1),int(y1),int(x2),int(y2))
ax.imshow(res)
plt.draw()
Loading…
Cancel
Save