Compute mean squared, absolute deviation and custom similarity measure - Python/NumPy

后端 未结 2 870
逝去的感伤
逝去的感伤 2020-12-19 11:09

I have a large image as an 2D array (let\'s assume that it is a 500 by 1000 pixels gray scale image). And I have one small image (let\'s say is it 15 by 15 pixels). I would

2条回答
  •  攒了一身酷
    2020-12-19 12:08

    Are you referring to a Cross Correlation operation?

    However, if you strictly want to check similarity with a squared deviation, you can use template matching in skimage, which uses a faster implementation of cross correlation. Example here : http://scikit-image.org/docs/dev/auto_examples/plot_template.html

    Otherwise, you can use correlate2d to achieve this as follows : 1. Perform a cross correlation on a zero-mean signal (meaning both signals/images should be centered about zero) 2. Check for local maxima scipy.signal.argrelmax or (if you think there would only be a single match) look for a global maxima using np.argmax

    Here is an example (lifted off from the documentation), you can replace np.argmax with signal.argrelmax if necessary for your purpose

    from scipy import signal
    from scipy import misc
    lena = misc.lena() - misc.lena().mean()
    template = np.copy(lena[235:295, 310:370]) # right eye
    template -= template.mean()
    lena = lena + np.random.randn(*lena.shape) * 50 # add noise
    corr = signal.correlate2d(lena, template, boundary='symm', mode='same')
    y, x = np.unravel_index(np.argmax(corr), corr.shape) # find the match
    

    Source :

    https://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.signal.correlate2d.html

    https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.argrelmax.html#scipy.signal.argrelmax

提交回复
热议问题