You can use correlation. You need to set the black values ββto -1 and your white values ββto 1 (or vice versa) so that you know the value of the correlation peak and that it only happens with the correct letter.
The following code does what I think you want.
import numpy from scipy import signal
This outputs (array([37]), array([84]))
, the location of the offsets set in the code.
You will most likely find that if the size of your message times your large array size is greater than about Nlog (N), where N corresponds to the size of the large array you are looking for (for each dimension), then you will probably get acceleration using an fft-based algorithm such as scipy.signal.fftconvolve
(bearing in mind that you need to flip each axis of one of the data sets if you use convolution rather than correlation - flipud
and fliplr
). The only modification would be the assignment c:
c = signal.fftconvolve(a, numpy.fliplr(numpy.flipud(b)), 'valid')
Comparison of timings in size above:
In [5]: timeit c = signal.fftconvolve(a, numpy.fliplr(numpy.flipud(b)), 'valid') 100 loops, best of 3: 6.78 ms per loop In [6]: timeit c = signal.correlate(a, b, 'valid') 10 loops, best of 3: 151 ms per loop
source share