Skip to main content
 首页 » 编程设计

python之通过python中的相位相关进行平移/旋转

2024年11月01日16mate10pro

我有两张图片,一张是原始图片,另一张是我修改过的,它向上平移并向左移动了一点,然后旋转了 90 度(因此图片的形状也被调换了)。

现在我想确定修改后的图片从原始图片平移了多少像素(或任何距离单位),以及相对于原始图片的旋转度数。相位相关应该通过首先将坐标转换为对数极坐标,然后做一些事情来解决这个问题,以便最终得到一个相关矩阵。我应该从那个矩阵中找到峰值,并且 (x,y) 组合会以某种方式揭示平移和旋转。这个链接解释得更好: Phase correlation

这是我的以下代码:

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") 
target = load_image("C:/images/testing_otherimage.jpg") 
 
# 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) 

我现在遇到的问题是这段代码将作为输出给出:

Traceback (most recent call last): 
  File "./phase.py", line 44, in <module> 
    lpimage = logpolar(image) 
  File "./phase.py", line 24, in logpolar 
    order=3,mode='constant') 
  File "C:\Python27\lib\site-packages\scipy\ndimage\interpolation.py", line 295, in map_coordinates 
    raise RuntimeError('invalid shape for coordinate array') 
RuntimeError: invalid shape for coordinate array 

由于我对整个相位相关过程中到底发生了什么只有非常肤浅的了解,所以我不清楚问题出在哪里。我试图查看输入是否有问题,所以我在加载图像后立即添加了 save_image(image,"C:/testing.jpg") 以查看来自的 numpy 数组是否有问题我的图像。果然,我转换为 np 数组的图像无法转换回图像。这是我得到的错误:

  Traceback (most recent call last): 
  File "./phase.py", line 41, in <module> 
    save_image(image,"C:/testing.jpg") 
  File "./phase.py", line 36, in save_image 
    img = Image.fromarray( np.asarray( np.clip(npdata,0,255), dtype="uint8"), "L" ) 
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 1917, in fromarray 
    raise ValueError("Too many dimensions.") 
ValueError: Too many dimensions. 

查看原始文档并没有给我太多关于问题可能的灵感。我不认为将图像转换为 numpy 数组的代码是错误的,因为我已经使用 print type(image) 测试了类型并且结果看起来是合法的。但是我无法将其转换回图像。我将不胜感激。

请您参考如下方法:

我认为问题在于您正在尝试将 3D 图像数组(R、G、B、A?)输入到您的函数中。而输入仅采用二维数组。尝试使用单个 channel 来确定转换。例如。

image = load_image("/path/to/image")[:,:,0]