2010-05-14 38 views
5

如何通過2個圖像的相位相關性(使用fft)確定旋轉角度?在http://en.wikipedia.org/wiki/Phase_correlation中給出的算法返回線性移位,而不是角度。它還提到圖像必須轉換爲對數極座標來計算旋轉。這個轉換是如何在python中實現的?和轉換後做相同的步驟的算法舉行?相位相關

回答

4

登錄極性變換實際上是旋轉和尺度不變..旋轉對應於y軸的移位和縮放對應於x軸移位在數極座標變換

所以簡單的步驟是尋找在圖像x如下圖像Y:在圖像Y

  1. 查找圖像x(直角座標系中使用階段的相關性)

  2. 計算日誌x和y的極性變換(這是一個整體的其他問題,請參考s),請確保以兩張圖像中的相同特徵爲中心。

  3. x和y的查找FFT,說F(X)和F(Y)的F

  4. 查找相位相關(x)和F(y)時,調用它ř

  5. 查找R的IFFT(反FFT)。R的峯值對應於Y軸上的旋轉偏差和X軸上與原始圖像的縮放偏差。

參考文獻:

  1. http://etd.lsu.edu/docs/available/etd-07072005-113808/unrestricted/Thunuguntla_thesis.pdf
2

我一直在努力了一段時間同樣的問題。我在週末寫這篇文章。這不是最乾淨的代碼,但我只是一個物理學家,而不是程序員...

相位相關本身很簡單:使用您最喜歡的卷積算法來卷積兩幅圖像。峯值位置爲您提供旋轉/縮放比例差異。它很好地解釋了維基百科(在問題中提到的鏈接)。

我的問題是我找不到一個好的對數極座標轉換器,所以我寫了一個。這不是傻瓜式的,但它完成了工作。任何人都願意重寫它,使其更清晰,請這樣做!

import scipy as sp 
from scipy import ndimage 
from math import * 

def logpolar(input,silent=False): 
    # This takes a numpy array and returns it in Log-Polar coordinates. 

    if not silent: print("Creating log-polar coordinates...") 
    # Create a cartesian array which will be used to compute log-polar coordinates. 
    coordinates = sp.mgrid[0:max(input.shape)*2,0:360] 
    # Compute a normalized logarithmic gradient 
    log_r = 10**(coordinates[0,:]/(input.shape[0]*2.)*log10(input.shape[1])) 
    # Create a linear gradient going from 0 to 2*Pi 
    angle = 2.*pi*(coordinates[1,:]/360.) 

    # Using scipy's map_coordinates(), we map the input array on the log-polar 
    # coordinate. Do not forget to center the coordinates! 
    if not silent: print("Interpolation...") 
    lpinput = ndimage.interpolation.map_coordinates(input, 
              (log_r*sp.cos(angle)+input.shape[0]/2., 
              log_r*sp.sin(angle)+input.shape[1]/2.), 
              order=3,mode='constant') 

    # Returning log-normal... 
    return lpinput 

警告:此代碼是爲灰度圖像設計的。通過在每個單獨的顏色框上循環使用map_coordinates(),它可以很容易地適應於處理彩色圖像。

編輯:現在,執行關聯的代碼很簡單。在將腳本導入這兩個圖像作爲imagetarget,請執行下列操作:

# Conversion to log-polar coordinates 
lpimage = logpolar(image) 
lptarget = logpolar(target) 

# Correlation through FFTs  
Fcorr = np.fft.fft2(lpimage)*np.conj(np.fft.fft2(lptarget)) 
correlation = np.fft.ifft2(Fcorr) 

數組correlation應包含一個峯,座標尺寸差和角度差。此外,而不是使用FFT的,你可以簡單地使用numpy的的np.correlate()功能:

# Conversion to log-polar coordinates 
lpimage = logpolar(image) 
lptarget = logpolar(target) 

# Correlation 
correlation = np.correlate(lpimage,lptarget) 
+0

你可以發佈完整的源代碼(不僅是logpolar)嗎? – mrgloom 2012-12-13 07:23:13

+0

對不起,我遲到了一段時間......我正在編輯我的答案。 – PhilMacKay 2013-01-25 17:14:47

+0

嘿菲爾,我試着運行你的函數,但我得到一個運行時錯誤。你介意幫我嗎? http://stackoverflow.com/questions/16654083/ – 2013-05-20 16:40:02