【问题标题】:find peak from correlation matrix in python从python中的相关矩阵中找到峰值
【发布时间】:2013-11-06 13:53:58
【问题描述】:

这里解释了我的主要问题: translation/rotation through phase correlation in python

具体来说,我想知道如何从相关矩阵中找到峰值,以及如何解释该值。从链接中,为感兴趣的人详细解释了整个问题,但主要要知道的是,我试图找出一张图像相对于另一张图像被平移和旋转多远(这是一个稍微修改的版本另一张图片)。我正在尝试使用相位相关来实现这一点,这给了我一个相关矩阵(类型为 numpy 数组)作为结果。我尝试在相关矩阵上使用 argmax(),它给了我一个数字 (215),这对我来说没有任何意义。我期待两个数字,其中一个应该表示平移的偏移量,另一个表示一个图像相对于另一个图像的旋转。

简而言之:如何在相关矩阵中找到峰值(在 Python 中)?

import scipy as sp
from scipy import ndimage
from PIL import Image
from math import *
import numpy as np

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

def load_image( infilename ) :
    img = Image.open( infilename )
    img.load()
    data = np.asarray( img, dtype="int32" )
    return data

def save_image( npdata, outfilename ) :
    img = Image.fromarray( np.asarray( np.clip(npdata,0,255), dtype="uint8"), "L" )
    img.save( outfilename )

image = load_image("C:/images/testing_image1.jpg")[:,:,0] 
target = load_image("C:/images/testing_otherimage.jpg")[:,:,0] 

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

# Correlation through FFTs
Fcorr = np.fft.fft(lpimage)*np.fft.fft(lptarget)
correlation = np.fft.ifft(Fcorr)

max = np.argmax(correlation)

print max

【问题讨论】:

    标签: python correlation phase


    【解决方案1】:

    这里的脚本可以满足您的要求:http://www.lfd.uci.edu/~gohlke/code/imreg.py.html。它使用对数极坐标图像结合 FFT 提供平移、旋转和尺度不变的图像配准。

    【讨论】:

      猜你喜欢
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 2014-04-05
      • 2017-10-06
      • 2021-12-16
      • 2022-11-02
      • 1970-01-01
      相关资源
      最近更新 更多