我正在尝试使用
在这个例子中,我有一个简单的波长梯度,我试图将其“拉直”成列。(你可能会问为什么我要找轮廓和插值,但那是因为我实际上将这个代码应用于更复杂的用例。我只是想为这个简单的例子复制所有代码,以产生相同奇怪的输出。)
任何原因导致我的输出图像只有输入图像被扭曲成正方形和嵌入内部?我正在使用Python 2.7.12和matplotlib 1.5.1。以下是代码。
这个的输出结果如下所示: 非常感谢您的帮助!
skimage.PiecewiseAffineTransform
和skimage.warp
来扭曲一张图片。我有一组控制点(true
)映射到一个新的控制点集合(ideal
),但扭曲并没有返回我期望的结果。在这个例子中,我有一个简单的波长梯度,我试图将其“拉直”成列。(你可能会问为什么我要找轮廓和插值,但那是因为我实际上将这个代码应用于更复杂的用例。我只是想为这个简单的例子复制所有代码,以产生相同奇怪的输出。)
任何原因导致我的输出图像只有输入图像被扭曲成正方形和嵌入内部?我正在使用Python 2.7.12和matplotlib 1.5.1。以下是代码。
import matplotlib.pyplot as plt
import numpy as np
from skimage import measure, transform
true = np.array([range(i,i+10) for i in range(20)])
ideal = np.array([range(10)]*20)
# Find contours of ideal and true images and create list of control points for warp
true_control_pts = []
ideal_control_pts = []
for lam in ideal[0]:
try:
# Get the isowavelength contour in the true and ideal images
tc = measure.find_contours(true, lam)[0]
ic = measure.find_contours(ideal, lam)[0]
nc = np.ones(ic.shape)
# Use the y coordinates of the ideal contour
nc[:, 0] = ic[:, 0]
# Interpolate true contour onto ideal contour y axis so there are the same number of points
nc[:, 1] = np.interp(ic[:, 0], tc[:, 0], tc[:, 1])
# Add the control points to the appropriate list
true_control_pts.append(nc.tolist())
ideal_control_pts.append(ic.tolist())
except (IndexError,AttributeError):
pass
true_control_pts = np.array(true_control_pts)
ideal_control_pts = np.array(ideal_control_pts)
length = len(true_control_pts.flatten())/2
true_control_pts = true_control_pts.reshape(length,2)
ideal_control_pts = ideal_control_pts.reshape(length,2)
# Plot the original image
image = np.array([range(i,i+10) for i in range(20)]).astype(np.int32)
plt.figure()
plt.imshow(image, origin='lower', interpolation='none')
plt.title('Input image')
# Warp the actual image given the transformation between the true and ideal wavelength maps
tform = transform.PiecewiseAffineTransform()
tform.estimate(true_control_pts, ideal_control_pts)
out = transform.warp(image, tform)
# Plot the warped image!
fig, ax = plt.subplots()
ax.imshow(out, origin='lower', interpolation='none')
plt.title('Should be parallel lines')
这个的输出结果如下所示: 非常感谢您的帮助!
subplot
。 首先要尝试的是使用plt.figure
/plt.imshow
绘制变形图像,而不是使用plt.subplots
/ax.imshow
。 另外,请编辑您的问题以包含您的“import”语句? 谢谢! 还有,使用的Python版本是什么? - cxw