Computational Complexity of Correlation in Time vs Multiplication in Frequency space

I am working with 2d correlation for image processing techniques (pattern recognition etc…). I was wondering if there is a theoretical approach on how to tell when to use multiplication in frequency space over correlation in time space. For sizes of 2x frequency space is obviously faster but how about small, prime sizes like e.g. 11?

I’ll assume this is being done on a conventional CPU, one core, executing one simple thread, no fancy hardware. If there is more than that going on, it can probably be accounted for with adjustments to the reasoning for a simpler system. Not much more can be said without either a specific system to discuss, or a whole textbook or research paper to cover a range of possibilities.

I wouldn’t worry about power-of-two sizes. It doesn’t matter. FFT algorithms with the butterfly units and all that exist for factors of 3, or any small number, not just 2. There are clever algorithms for prime-sized data series, too. I don’t like quoting Wikipedia on this due to its impermanent nature, but anyway:

there are FFTs with O(N log N) complexity for all N, even for prime N

Implementations of FFTs for arbitrary N can be found in the GPL’d library FFTW.

The only trustworthy way in terms of serious engineering is to build and measure, but we certainly can get an idea from theory, to see relationships between variables. We need estimates of how many arithmetic operations are involved for each method.

Multiplying is still slower than addition on most CPUs, even if the difference has shrunk tremendously over the years, so let’s just count multiplications. Accounting also for addition takes a bit more thinking and keeping track of stuff.

A straightforward convolution, actually multiplying and adding using the convolution kernel, repeating for each output pixel, needs W²·K² multiplications, where W is the number of pixels along one side of the image (assuming square for simplicity), and K is the size of the convolution kernel, as pixels along one side. It takes K² multiplications to compute one output pixel using the kernel and same-size portion of the input image. Repeat for all output pixels, which number the same as in the input image.

(Nmults)direct = W²·K²

To do the job in Fourier space, we must Fourier transform the image. This is done by applying an FFT to each column separately, and then to each row. The FFT for N data points takes about 2N·log(N) multiplications; we want N to be W, the length of one column or row. All logarithms here are base two.

There are W rows and W columns, so after all the FFTs are done, we have done 2W·(2W·log(W)) multiplications. Double that, because after we multiply by the Fourier transform of the kernel, we have to inverse-Fourier the data to get back to sensible image. That’s 8W²·log(W). Of course, multiplying by the Fourier transform of the kernel has to be done, another W² multiplications. (Done once, not once per output pixel, per row or anything.) These are complex multiplications, so that’s 4W² real multiplications.

So, unless I goofed up (and I probably did) we have

(Nmults)Fourier = 4W²·(2·log(W) + 1)

When do we want to do things the direct way? When K is sufficiently small to make W²·K² smaller than 4W²·(2·log(W) + 1). A common factor of W² is easily factored out. We can probably drop the “+1” since we’re dealing with idealized estimates. The +1 is likely lost in errors relative to actual implementations, from not counting additions, loop overheads and so on. That leaves:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>< 8·log(W)
</code>
<code>K² < 8·log(W) </code>
K² < 8·log(W)

This is the approximate condition for choosing a direct approach over a frequency space approach.

Note that correlation of two same-size images is just like convolving with a kernel of size K = W. Fourier space is always the way to do it.

This can be refined and argued over to account for overhead, pipelining of opcodes, float vs. fixed-point, and thrown out the window with GPGPU and specialized hardware.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật