我想从图像转换为图像轮廓的子集。我试过这个:
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-')
答案 0 :(得分:1)
轮廓是曲线,您试图将曲线绘制为图像。
相反,使用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"
修复了这个。)