Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8504641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:05:32+00:00 2026-06-11T02:05:32+00:00

I have been recently trying to find a fast and efficient way to perform

  • 0

I have been recently trying to find a fast and efficient way to perform cross correlation check between two arrays using Python language. After some reading, I found these two options:

  1. The NumPy.correlate() method, which is too slow when it comes to large arrays.
  2. The cv.MatchTemplate() method, which seems to be much faster.

For obvious reasons, I chose the second option. I tried to execute the following code:

import scipy
import cv

image = cv.fromarray(scipy.float32(scipy.asarray([1,2,2,1])),allowND=True)
template = cv.fromarray(scipy.float32(scipy.asarray([2,2])),allowND=True)
result = cv.fromarray(scipy.float32(scipy.asarray([0,0,0])),allowND=True)
cv.MatchTemplate(image,template,result,cv.CV_TM_CCORR)

Even though this code suppose to be very simple, it throws the next error:

OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /builddir/build/BUILD/OpenCV-2.1.0/src/cxcore/cxarray.cpp, line 2476
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv.error: Unrecognized or unsupported array type

After a few hours of frustrating tries, I am still stuck! Does anybody have any suggestion?

BTW, this is my Python version output:

Python 2.7 (r27:82500, Sep 16 2010, 18:03:06) 
[GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Thank you all!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:05:34+00:00Added an answer on June 11, 2026 at 2:05 am

    You’re unlikely to get much faster than using an fft based correlation method.

    import numpy
    from scipy import signal
    
    data_length = 8192
    
    a = numpy.random.randn(data_length)
    b = numpy.zeros(data_length * 2)
    
    b[data_length/2:data_length/2+data_length] = a # This works for data_length being even
    
    # Do an array flipped convolution, which is a correlation.
    c = signal.fftconvolve(b, a[::-1], mode='valid') 
    
    # Use numpy.correlate for comparison
    d = numpy.correlate(a, a, mode='same')
    
    # c will be exactly the same as d, except for the last sample (which 
    # completes the symmetry)
    numpy.allclose(c[:-1], d) # Should be True
    

    Now for a time comparison:

    In [12]: timeit b[data_length/2:data_length/2+data_length] = a; c = signal.fftconvolve(b, a[::-1], mode='valid')
    100 loops, best of 3: 4.67 ms per loop
    
    In [13]: timeit d = numpy.correlate(a, a, mode='same')
    10 loops, best of 3: 69.9 ms per loop
    

    If you can cope with a circular correlation, you can remove the copy. The time difference will increase as data_length increases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been recently trying to deploy a C# application on a computer that
I've recently been trying to port a C++ application. I believe I have all
Recently I have been trying to optimize my tables, mainly because I've learned alot
I recently downloaded the Android Support Package and have been using it trying to
I'm a vim user and have recently been trying out emacs for fun. I
I have recently begun the process of trying to learn Ruby/Rails and have been
Recently, I have been trying to get the built-in Windows 7 narrator/screen reader to
I have recently been trying to upgrade my app form Rails 2.3.8 to newly-releases
I have been recently digging into Mobile Programming, I practically tried out the J2ME
I have been recently testing my app in a strict mode to detect disk

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.