3
\$\begingroup\$

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.

code:

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[0] + x, y:pix.shape[1] + 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[0], 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()

cropped

raw

\$\endgroup\$
  • \$\begingroup\$ have you profiled your code? that's always a good place to start. \$\endgroup\$ – Oscar Smith May 30 '18 at 5:26
  • 2
    \$\begingroup\$ You need to completely rethink your algorithm. Read stackoverflow.com/questions/297762/… \$\endgroup\$ – Reinderien May 30 '18 at 8:05
  • \$\begingroup\$ "One confounding factor is that the cropped image has a line drawn through it": do you have any guarantees about the position of the line? E.g. if it's guaranteed to run top-left to bottom-right and to be no more than 5 pixels wide, that's useful information. FWIW the image links at the bottom of the question no longer work. \$\endgroup\$ – Peter Taylor Aug 7 '18 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.