I have a number of images that were cropped and their backgrounds removed. I've tried to write a function that will search the original unmodified image for the cropped image so that I can restore the cropped image's original size and position. One confounding factor is that the cropped image has a line drawn through it in addition to the background being removed, so even with masking there can't be an exact match, so I'm attempting to find the best match.
I would appreciate any advice on how I can improve performance. For this to be feasible on my machine, I would need to increase speed by 100x.
import numpy import numexpr as ne from skimage import io def overlay(pix, rawImage): sizex, sizey, _ = pix.shape targetx, targety, _ = rawImage.shape bestMatchScore = float('inf') for x in range(1, targetx - sizex): for y in range(1, targety - sizey): start = time.clock() test = rawImage[x:pix.shape + x, y:pix.shape + y, :] matchScore = numpy.sum(ne.evaluate('sum((test-pix)**2)')) if matchScore < bestMatchScore: bestMatchScore = matchScore xshift, yshift = x,y print(time.clock() - start) background = numpy.full(rawImage.shape, 255) background[xshift: xshift + pix.shape, yshift:pix.shape + yshift, :] = pix return background def main(): pix = io.imread(pathToCropped) rawImage = io.imread(pathToRaw) background = overlay(pix, rawImage) if __name__ == '__main__': main()