如何用skimage(Python)实际保存图像 [英] How are images actually saved with skimage (Python)

查看:1730
本文介绍了如何用skimage(Python)实际保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在申请 Zhang - 使用细化算法来磨练我希望稍后跟踪的一些细丝。这需要我输出一个灰度图像,以便使用OpenCV识别对象。

I am currently applying the Zhang-Suen thinning algorithm to hone down on some filaments I would like to later track. This requires me to output a grayscale image in order to identify objects using 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将输出图像写入文件路径时,我得到一个蓝色和红色的类似图像。我唯一的问题是我无法将此蓝/红图像转换为灰度图像!所以从本质上讲,我的输出图像是无用的。如果这是一个微不足道的问题,请原谅我,但是有没有将图像写入文件路径的协议?当我使用这些工具时,我应该注意图像类型(即字节,颜色/灰度,格式)?在此先感谢!

The image plotted using matplotlib is exactly what I want (black and white). When I use either skimage or cv2 to write the output image to a file path, I get a similar image in blue and red. My only problem is that I can't convert this blue/red image to grayscale! So in essence, my output image is useless. Forgive me if this is a trivial question, but is there a protocol for writing images to file paths? What should I be looking out for in terms of image types (ie. bytes, colour/grayscale, format) when I use these tools? Thanks in advance!

推荐答案

您可以设置已保存数据的颜色映射,以便在OpenCV中打开图像时它将是灰度的。

You can set the color map of the saved data so that when you open the image in OpenCV it will be in grey scale.

以下是一些示例数据:

data = np.random.rand(256,256)

您可以保存直接数据

plt.imsave('test.png', data, cmap = plt.cm.gray)

或保存整个

plt.savefig('test2.png')

这篇关于如何用skimage(Python)实际保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆