如何使用skimage(Python)保存图像?

7

我目前正在应用张孙细化算法,以便找到我想要跟踪的一些纤维。这要求我输出一个灰度图像,以便使用OpenCV识别对象。

import matplotlib
import matplotlib.pyplot as plt
import skimage.io as io
"load image data"
Img_Original =  io.imread( './data/test1.bmp')      # Gray image, rgb images   need pre-conversion

"Convert gray images to binary images using Otsu's method"
from skimage.filter import threshold_otsu
Otsu_Threshold = threshold_otsu(Img_Original)   
BW_Original = Img_Original < Otsu_Threshold    # must set object region as 1, background region as 0 !

#...
"Apply the algorithm on images"
BW_Skeleton = zhangSuen(BW_Original)
# BW_Skeleton = BW_Original
"Display the results"
fig, ax = plt.subplots(1, 2)
ax1, ax2 = ax.ravel()
ax1.imshow(BW_Original, cmap=plt.cm.gray)
ax1.set_title('Original binary image')
ax1.axis('off')
ax2.imshow(BW_Skeleton, cmap=plt.cm.gray)
ax2.set_title('Skeleton of the image')
ax2.axis('off')
plt.show()

使用matplotlib绘制的图像是我想要的(黑白)。但是,当我使用skimage或cv2将输出图像写入文件路径时,得到的图像却是蓝色和红色的。我的问题是,我无法将这个蓝色/红色的图像转换为灰度图像!因此,实际上我的输出图像是没有用的。请原谅我如果这是一个琐碎的问题,但是在使用这些工具时,写入文件路径的图像类型(即字节、彩色/灰度、格式)有哪些需要注意的地方?谢谢!
1个回答

11

您可以设置保存数据的颜色映射,这样当您在OpenCV中打开图像时,它将呈现为灰度。

以下是一个示例数据:

data = np.random.rand(256,256)
你可以直接保存数据
plt.imsave('test.png', data, cmap = plt.cm.gray)

或者保存整个图形


plt.savefig('test2.png')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接