将以下代码增加保存图像功能: import matplotlib.pyplot as plt import numpy as np from skimage import io, filters, measure, color from skimage.filters import threshold_otsu from skimage import io, color, filters img = io.imread('leaf.jpg') gray_img = color.rgb2gray(img) thresh = filters.threshold_otsu(gray_img) binary = gray_img > thresh edges = filters.sobel(binary) fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(8, 3)) ax = axes.ravel() ax[0].imshow(img) ax[0].set_title("Original") ax[1].imshow(edges, cmap='gray') ax[1].set_title("Edges") for a in ax: a.axis('off') plt.tight_layout() plt.show()
时间: 2024-02-12 19:09:03 浏览: 195
以下是增加保存图像功能的代码:
```Python
import matplotlib.pyplot as plt
import numpy as np
from skimage import io, filters, measure, color
from skimage.filters import threshold_otsu
# 读取图像
img = io.imread('leaf.jpg')
gray_img = color.rgb2gray(img)
# 图像处理
thresh = filters.threshold_otsu(gray_img)
binary = gray_img > thresh
edges = filters.sobel(binary)
# 绘制图像
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(8, 3))
ax = axes.ravel()
ax[0].imshow(img)
ax[0].set_title("Original")
ax[1].imshow(edges, cmap='gray')
ax[1].set_title("Edges")
for a in ax:
a.axis('off')
# 保存图像
plt.savefig('edges.png')
plt.tight_layout()
plt.show()
```
在代码的最后,我们使用 `plt.savefig()` 函数将绘制的图像保存为文件,文件名为 "edges.png"。这样,在程序运行结束后,我们就可以在当前工作目录中找到保存的图像文件了。
阅读全文