gpt4 book ai didi

python - 使用 scikit-image 和 transform.PolynomialTransform 进行图像变形

转载 作者:太空宇宙 更新时间:2023-11-04 01:12:21 25 4
gpt4 key购买 nike

我附上一个 zip archive包含说明和重现问题所需的所有文件。

(我还没有权限上传图片...)

我有一张带有曲线的图像(zip 存档中的 test2.png)。

我试着扭曲它,使线条笔直。我想到了使用 scikit-image 变换,特别是 transform.PolynomialTransform,因为变换涉及高阶失真。

因此,首先我在 x 中定期测量每条线的精确位置,以定义输入兴趣点(在文件 source_test2.csv 中)。然后我计算相应的所需位置,位于一条直线上(在文件 destination_test2.csv 中)。

图correspondence.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)

提前致谢!

最佳答案

这里有一些错误,主要是与坐标约定有关。例如,如果我们检查绘制原始图像的代码,然后将点击点放在它上面:

plt.imshow(orig, cmap='gray', interpolation='nearest')
plt.plot(source[:,0],source[:,1],'+r')
plt.xlim(0,orig.shape[1])
plt.ylim(0,orig.shape[0])

(我已经删除了目的地点以使其更清晰)然后我们得到以下图像:

the y-axis is inverted!

如您所见,如果我们将 y 轴反转:

source[:,1] = orig.shape[0] - source[:,1]

在绘图之前,我们得到以下信息:

That's much better

所以这是第一个问题(也不要忘记反转目标点),第二个问题与变换本身有关:

t.estimate(destination,source,3)

来自documentation我们看到调用首先获取源点,然后是目标点。因此,应该翻转这些参数的顺序。

最后,点击点的形式为 (x,y),但图像存储为 (y,x),因此我们必须在应用变换之前转置图像,然后再次转置:

img_warped = transform.warp(orig.transpose(), t, order=2, mode='constant',cval=float('nan'))
img_warped = img_warped.transpose()

当您进行这些更改时,您会得到以下扭曲的图像:

correctly warped

这些线不是完全平坦的,但它更有意义。

关于python - 使用 scikit-image 和 transform.PolynomialTransform 进行图像变形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26995537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com