加速python中的傅立叶相关变换计算(OpenCV)

时间:2014-02-28 11:20:37

标签: python opencv python-2.7 image-processing fft

我有一个图像,我需要计算一个与傅里叶相关的变换,称为短时傅立叶变换(用于额外的数学信息检查:http://en.wikipedia.org/wiki/Short-time_Fourier_transform)。

为了做到这一点,我需要:

(1)在图像的起始像素处放置一个窗口(x,y)=(M / 2,M / 2)

(2)使用此窗口截断图像

(3)计算截断图像的FFT,保存结果。

(4)逐渐向右滑动窗口

(5)转到步骤3,直到窗口到达图像的末尾

但是我需要实时执行上述计算... 但它很慢!!!

无论如何都要加快前面提到的过程吗?

我还包括我的代码:

        height, width = final_frame.shape
        M=2
        for j in range(M/2, height-M/2):
            for i in range(M/2, width-M/2):
                face_win=final_frame[j-M/2:j+M/2, i-M/2:i+M/2]

                #these steps are perfomed in order to speed up the FFT calculation process
                height_win, width_win = face_win.shape
                fftheight=cv2.getOptimalDFTSize(height_win)
                fftwidth=cv2.getOptimalDFTSize(width_win)
                right = fftwidth - width_win
                bottom = fftheight - height_win
                bordertype = cv2.BORDER_CONSTANT 
                nimg = cv2.copyMakeBorder(face_win,0,bottom,0,right,bordertype, value = 0)

                dft = cv2.dft(np.float32(face_win),flags = cv2.DFT_COMPLEX_OUTPUT)
                dft_shift = np.fft.fftshift(dft)
                magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))

1 个答案:

答案 0 :(得分:3)

当然,你的大部分时间都花在了FFT和其他转换代码上,但我对其他部分进行了简单的优化。

更改

  • 每个循环的帧大小计算都是相同的,所以将它们移出(〜nil改进)
  • 从uint8到float32的类型强制可以在整个图像上执行一次,而不是转换每个帧。 (小但可衡量的改进)
  • 如果窗口大小已经与最佳大小相同(我想如果你将M保持为2的幂,则总是如此),那么就不要做边框复制。只需按原样使用face_win视图即可。 (小但可衡量的改进)

总改进26s - > 22S。它并不多,但确实存在。

独立代码(只需添加1024x768.jpg

import time
import cv2
import numpy as np

# image loading for anybody else who wants to use this
final_frame = cv2.imread('1024x768.jpg')
final_frame = cv2.cvtColor(final_frame, cv2.COLOR_BGR2GRAY)
final_frame_f32 = final_frame.astype(np.float32)  # moved out of the loop

# base data
M = 4
height, width = final_frame.shape

# various calculations moved out of the loop
m_half = M//2
height_win, width_win = [2 * m_half] * 2  # can you even use odd values for M?
fftheight = cv2.getOptimalDFTSize(height_win)
fftwidth = cv2.getOptimalDFTSize(width_win)
bordertype = cv2.BORDER_CONSTANT
right = fftwidth - width_win
bottom = fftheight - height_win

start = time.time()
for j in range(m_half, height-m_half):
    for i in range(m_half, width-m_half):
        face_win = final_frame_f32[j-m_half:j+m_half, i-m_half:i+m_half]
        # only copy for border if necessary
        if (fftheight, fftwidth) == (height_win, width_win):
            nimg = face_win
        else:
            nimg = cv2.copyMakeBorder(face_win, 0, bottom, 0, right, bordertype, value=0)
        dft = cv2.dft(nimg, flags=cv2.DFT_COMPLEX_OUTPUT)
        dft_shift = np.fft.fftshift(dft)
        magnitude_spectrum = 20 * np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))
elapsed = time.time() - start

print elapsed

的错误

  • 我在上面的代码中解决了这些问题,但我没有编辑你的原文,因为你可能已经打算这样做了
  • 你计算nimg然后使用dft中的原始face_win
  • 明确地说,我将M/2等更改为M//2