【发布时间】:2014-11-20 00:40:41
【问题描述】:
我附上了一个zip archive,其中包含说明和重现问题所需的所有文件。
(我还没有上传图片的权限...)
我有一张带有曲线的图片(zip 存档中的 test2.png)。
我尝试扭曲它,使线条笔直。 我想到了使用 scikit-image 变换,特别是 transform.PolynomialTransform,因为该变换涉及高阶失真。
所以首先我在 x 中定期测量每条线的精确位置,以定义输入兴趣点(在文件 source_test2.csv 中)。 然后我计算相应的所需位置,位于一条直线上(在文件destination_test2.csv 中)。
图对应.png显示它的样子。
接下来,我只需使用 3 阶多项式调用 transform.PolynomialTransform()。 它找到了解决方案,但是当我使用 transform.warp() 应用它时,结果很疯狂,如文件 Crazy_Warped.png 中所示
谁能告诉我我做错了什么? 我没有运气就尝试了2阶多项式...... 我设法为子图像(仅前 400 列)进行了良好的转换。 在像我这样的情况下,transform.PolynomialTransform() 完全不稳定吗?
这是完整的代码:
import numpy as np
import matplotlib.pyplot as plt
import asciitable
import matplotlib.pylab as pylab
from skimage import io, transform
# read image
orig=io.imread("test2.png",as_grey=True)
# read tables with reference points and their desired transformed positions
source=asciitable.read("source_test2.csv")
destination=asciitable.read("destination_test2.csv")
# format as numpy.arrays as required by scikit-image
# (need to add 1 because I started to count positions from 0...)
source=np.column_stack((source["x"]+1,source["y"]+1))
destination=np.column_stack((destination["x"]+1,destination["y"]+1))
# Plot
plt.imshow(orig, cmap='gray', interpolation='nearest')
plt.plot(source[:,0],source[:,1],'+r')
plt.plot(destination[:,0],destination[:,1],'+b')
plt.xlim(0,orig.shape[1])
plt.ylim(0,orig.shape[0])
# Compute the transformation
t = transform.PolynomialTransform()
t.estimate(destination,source,3)
# Warping the image
img_warped = transform.warp(orig, t, order=2, mode='constant',cval=float('nan'))
# Show the result
plt.imshow(img_warped, cmap='gray', interpolation='nearest')
plt.plot(source[:,0],source[:,1],'+r')
plt.plot(destination[:,0],destination[:,1],'+b')
plt.xlim(0,img_warped.shape[1])
plt.ylim(0,img_warped.shape[0])
# Save as a file
io.imsave("warped.png",img_warped)
提前致谢!
【问题讨论】:
-
当我运行你的代码时,我的
'warped.png'看起来和你的大不相同。 -
对不起!!!我使用三阶多项式 t.estimate(destination,source,3) 在 zip 存档中生成文件,而上面的代码使用二阶多项式 t.estimate(destination,source,2)。我刚刚编辑了上面的代码,使其保持一致。但无论如何,没有一个能给出好的结果......
标签: python image transformation distortion scikit-image