我有两张图片,一张是原始的,另一张是我修改过的,它向上平移了一点,然后旋转了 90 度(所以图片的形状也被转置了)。
现在我想确定修改后的图片从原始图片转换了多少像素(或任何距离单位),以及相对于原始图片的旋转度数。相位相关应该通过首先将坐标转换为对数极坐标来解决这个问题,然后做一些事情,以便最终得到一个相关矩阵。我应该从那个矩阵中找到峰值,(x,y) 组合将以某种方式揭示平移和旋转。这个链接解释得更好: 相位相关
这是我拥有的以下代码:
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)
并且结果看起来是合法的。但是我无法将其转换回图像。我能得到的任何帮助将不胜感激。