Python - 将轮廓转换为图像

Python - convert contours to image

我想将数组中的一部分轮廓转换为图像。我试过这个:

z = (plt.contour(data, [0.0, 0.2, 0.4]))
plt.imshow(z)

我收到这个错误

    ...\matplotlib\image.pyc in set_data(self, A)
    428         if (self._A.dtype != np.uint8 and
    429             not np.can_cast(self._A.dtype, np.float)):
--> 430             raise TypeError("Image data can not convert to float")
    431 
    432         if (self._A.ndim not in (2, 3) or

TypeError: Image data can not convert to float

是否有解决此问题的方法,或其他将轮廓保存到图像的方法?

* 已更新 *

下面是一个完整的笔记本风格的例子:

# preliminaries
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

# create data - from a classic Matplotlib example
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

# plot data as image
# image is displayed correctly
fig1 = plt.figure(figsize=(6, 6))
ax = fig1.add_subplot(1, 1, 1)
ax.set_xticks([])
ax.set_yticks([])
plt.imshow(Z, cmap='bone_r')

# plot subset of data as contours
# contours are displayed correctly
fig2 = plt.figure(figsize=(6, 6))
ax = fig2.add_subplot(1, 1, 1)
z = (plt.contour(Z, [0.0, 0.2, 0.4]))

# overlay - this gives the error message above
fig3 = plt.figure(figsize=(6, 6))
ax = fig3.add_subplot(1, 1, 1)
ax.set_xticks([])
ax.set_yticks([])
plt.imshow(Z, cmap='bone_r')
plt.imshow(z)
plt.show()

已更新 2

我使用下面的代码捕获了所需 z 级别的所有轮廓。查看已接受的答案和评论。

for collection in z.collections:
  paths = collection.get_paths()
  for path in paths:
    line=(path.vertices)`  
    plt.plot(line[:,0], line[:,1], 'k-')

等高线是一条曲线,而您正试图将曲线 绘制为 图像。

相反,使用 z.collections[index].get_paths()[0].vertices 来获取曲线。然后,您可以将此曲线绘制为 图像上的一条线。不过,我也想知道您是否只想在曲线上放置法线轮廓,所以这里我将包括这两个示例...

正常等高线图:

修改后的轮廓(此处右移),直接绘制在图像上

# preliminaries
import matplotlib.pyplot as plt
import numpy as np

# create data - from a classic Matplotlib example
X = np.arange(-5, 5, 0.25)
Y = np.arange(-4, 6, 0.25) # test with asymmetric range since y-axis origin can sometimes lead to problems
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

# plot data as image
fig1 = plt.figure(figsize=(6, 6))

ax = fig1.add_subplot(1, 1, 1)
ax.set_xticks([])
ax.set_yticks([])
plt.imshow(Z, cmap='bone_r')

# If you just want to plot the contous on the image, just don't
#    create a new figure, or z = ax.contour(Z, [0.0, 0.2, 0.4])
#    if the "ax" variable name is unique
# plot subset of data as contours
#fig2 = plt.figure(figsize=(6, 6))

#ax = fig2.add_subplot(1, 1, 1)
z = (plt.contour(Z, [0.0, 0.2, 0.4]))

# overlay - this gives error message
fig3 = plt.figure(figsize=(6, 6))

ax = fig3.add_subplot(1, 1, 1)
ax.set_xticks([])
ax.set_yticks([])

plt.imshow(Z, cmap='bone_r')
# If you want to plot the controus yourself, you can do it like this...
index = 2  # look at the first contour
line = z.collections[index].get_paths()[0].vertices
line = np.copy(line)  # make a copy so manipulations don't change the original contour
line[:,0] += 1.0  # shift it to the right (as an example of something we can do only easily with the data for the contour line
plt.plot(line[:,0], line[:,1], 'r-')

plt.show()

请注意,在所有这些中,您可能还想使用 origin="lower",具体取决于您的目的(也就是说,请注意,当我移动 y 轴时,我将范围向上移动,所以中心应该在图像中向下移动,但是图像中的 y 轴传统上是倒置的,"lower" 修复了这个问题。