from matplotlib.widgets import RectangleSelector import matplotlib.pyplot as plt import numpy as np def doPatchMatch(img,x1,y1,x2,y2,patchSize=17): def dist(patchValue1,patchValue2): return np.sum((patchValue1 - patchValue2) ** 2) def patchToValue(patch): return img[patch[0][1]:patch[len(patch)-1][1], patch[0][0]:patch[len(patch)-1][0]] def getRandomPatch(): rx = np.random.randint(0, width - patchSize) ry = np.random.randint(0, height - patchSize) return rx, ry def initializePermimiter(): perimeter = [] for x in range(x1, x2 + 1): perimeter.append((x, y1)) perimeter.append((x, y2)) for y in range(y1 + 1, y2): perimeter.append((x1, y)) perimeter.append((x2, y)) img[y1:y2+1, x1:x2+1] = -1 return np.array(perimeter) def removeAndAddFromPerimiter(perimiter, addr): p= [] npAddr = np.array(addr) for coord in perimiter: if not np.any(np.all(npAddr == np.array(coord), axis=1)): p.append(coord) perimiter = p p1 = patchSize+2 for dx in range(-1, p1): for dy in range(-1, p1): if (dx!=-1 and dx!=p1-1 and dy != -1 and dy != p1-1): continue nx, ny = addr[0,0] + dx, addr[0,1] + dy if 0 <= nx < width and 0 <= ny < height and img[ny, nx][0] == -1: if len(perimiter) == 0: perimiter.append([nx, ny]) continue if not np.any(np.all(perimiter == np.array([int(nx), int(ny)]), axis=1)): perimiter.append([nx, ny]) return perimiter def applyPatch(patch,addr): for i in range(len(addr)): if img[addr[i, 1], addr[i, 0]][0] == -1: img[addr[i, 1], addr[i, 0]] = img[patch[i, 1],patch[i, 0]] def getRandomFromPerimiter(perimiter): return perimiter[np.random.randint(len(perimiter))] 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 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) applyPatch(patch,addr) perimiter = removeAndAddFromPerimiter(perimiter,addr) return perimiter semiPatch = int(patchSize/2) height, width, _ = img.shape perimiter = initializePermimiter() it = 0 # 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) if (it == 1000): it = 0 print(len(perimiter)) 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, y2 = erelease.xdata, erelease.ydata print("drawing") img_copy = np.copy(img) res = doPatchMatch(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()