Ad
How To Find Correlation Between Two Images Using Numpy
This is inspired by this question.
I'm trying to find correlation between two grayscale images using Numpy. Using SciPy's correlate2d
we can find this. I have found Numpy's corrcoef
but results are different when I compared with correlate2d
. Hence this question- Is there correlate2d
equivalent in Numpy?
Ad
Answer
As far as I can tell, this produces the same result as scipy.correlate2d()
, where img1
and img2
are 2d arrays representing greyscale (i.e. single-channel) images:
import numpy as np
pad = np.max(img1.shape) // 2
fft1 = np.fft.fft2(np.pad(img1, pad))
fft2 = np.fft.fft2(np.pad(img2, pad))
prod = fft1 * fft2.conj()
result_full = np.fft.fftshift(np.fft.ifft2(prod))
corr = result_full.real[1+pad:-pad+1, 1+pad:-pad+1]
The single-pixel cropping adjustment is not very elegant but that's FFTs for you: fiddly.
I just want to say that scipy
is perfectly fine to use and I strongly recommend it. Having said that, this approach does seem to be a lot faster for the single case I tried.
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad